qq_37916181 2019-09-02 15:25 采纳率: 0%
浏览 299

excel多文件压缩导出问题 导出多个不认识文件

图片说明

excel多文件压缩导出问题 目前只导出了一个 但打开压缩包是这些东西 这是什么原因

public void exportTransactionExcel(HttpServletResponse res,Long id)throws Exception{
        OutputStream out = res.getOutputStream();
        ZipOutputStream zipOutputStream = new ZipOutputStream(out);
        try {
            res.setContentType("application/octet-stream; charset=utf-8");
            res.setHeader("Content-Disposition", "attachment; filename=" + Encodes.urlEncode("xxx.zip"));
            List<List<String>> data = new ArrayList<List<String>>();
            ExportExcelUtils eeu = new ExportExcelUtils();
            File file=new File("C:/Users/MXG/Desktop/xxx.xlsx");
            FileInputStream fis=new FileInputStream(file);
            //Workbook workbook = new XSSFWorkbook(OPCPackage.open(fis));
            XLSTransformer transformer = new XLSTransformer();
            Map<Object, Object> beans=new HashMap<>();
           // Workbook workbook = transformer.transformXLS(new FileInputStream(file), beans);
            Query  query =em.createQuery("From Transaction where 1=1"+((id!=null&&id>0)?" and id="+id:"") );
            List<Transaction> transactionList =query.getResultList();
            int j=0;
            for (Transaction transaction : transactionList) {
                Workbook workbook = new XSSFWorkbook(OPCPackage.open(fis));
                 // HSSFSheet mySheet = workbook.createSheet();
                  Sheet mySheet = workbook.getSheetAt(0);
                  //创建第一行,标题行
                  int rowNomber=-1;
                  Row row= mySheet.getRow(2);
                  Cell cell=row.getCell(3);
                  cell.setCellValue("123456");

                //重点开始,创建压缩文件
                ZipEntry z = new ZipEntry(transaction.getServiceName()+j+++ ".xlsx");
                zipOutputStream.putNextEntry(z);
              //写入一个压缩文件
                workbook.write(zipOutputStream);
            }
            zipOutputStream.flush();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //注意关闭顺序,否则可能文件错误
            if (zipOutputStream != null) {
                zipOutputStream.close();
            }
            if (out != null) {
                out.close();
            }
        }
    }
  • 写回答

1条回答

  • 呓语的风 2019-09-02 15:28
    关注

    excel本来就是一个压缩文件

    评论

报告相同问题?

悬赏问题

  • ¥15 matlab有关常微分方程的问题求解决
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?
  • ¥100 求三轴之间相互配合画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable