用Java编写Excel文件

问题描述

我正在编写一种用于写入和保存Excel文件的功能。

在第一种情况下,代码可以正常工作,并且可以将有效的.xlsx文件正确保存在Eclipse文件夹中(但是,如果再次使用,它将覆盖以前的文件)。

但是,我希望将文件直接下载到用户的“下载”文件夹,如果再次使用,新文件将以name(1)等名称保存,因此我编写了一些其他代码。看来还可以,但是没有效果-找不到Excel文件。

也许我缺少一些依赖关系,一些标头-我真的无法弄清楚。

第一个代码(有效):

@RequestMapping(value = "/exportToExcel",method=RequestMethod.GET,produces={MEDIA_JSON_UTF8})
public void getExcel(@RequestParam(value="ids") List<Long> ids,HttpServletRequest request,HttpServletResponse response) throws SQLException,IOException {

   List<Issue> list = issueDao.getIssuesByIdsList(ids);
            
   XSSFWorkbook workbook = new XSSFWorkbook();
   XSSFSheet sheet = workbook.createSheet("Issues");
 
   writeHeaderLine(sheet,workbook);
   writeDataLines(list,workbook,sheet);
            
   File currDir = new File(".");
   String path = currDir.getAbsolutePath();
   String fileLocation = path.substring(0,path.length() - 1) + "wycofania.xlsx";
 
   FileOutputStream outputStream = new FileOutputStream(fileLocation);
   workbook.write(outputStream);
   workbook.close();
}

第二个代码(不起作用):

@RequestMapping(value = "/exportToExcel",produces={MEDIA_JSON_UTF8})
public ResponseEntity<InputStreamResource> getExcel(@RequestParam(value="ids") List<Long> ids,IOException {

   HttpHeaders headers = new HttpHeaders();
   ByteArrayInputStream in;

   try {
        
      in = excelService.export(ids);
      headers.add(HttpHeaders.CONTENT_DISPOSITION,"attachment; filename=issues.xlsx");
      headers.add(HttpHeaders.CONTENT_TYPE,"application/octet-stream");    
      return ResponseEntity.ok().headers(headers).body(new InputStreamResource(in));

   } catch (Exception e) {
      log.error(e.getMessage());
      return null;
   }
}


public ByteArrayInputStream export(List<Long> ids) throws IOException {

   List<Issue> list = issueDao.getIssuesByIdsList(ids);
            
   XSSFWorkbook workbook = new XSSFWorkbook();
   XSSFSheet sheet = workbook.createSheet("Issues");
 
   // two lines below work well
   writeHeaderLine(sheet,sheet);
            
   ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
   workbook.write(outputStream);
   workbook.close();
   return new ByteArrayInputStream(outputStream.toByteArray());
}

因此,在第二种情况下,不会下载任何内容。没有异常被抛出。在第二种情况下的响应非常奇怪,可能是编码问题,或者其他原因:

PK�
Q_rels/.rels���J1�����Ͷ��4�E��D����n��LH�u��/j킂�d2�����vu��=˪E���5�\܁ʂ�����(�vs}�~��4��Ǭ
%d�H��:ێF�G
��pQ�1�:���%���[��2���v�@ڹ%�=����4�7N�+s_p)�"�&���[z`{)ȅ����lV�6��S�ҋ1��MB��[Ē@I|��Y��N��Mj~4z$A���J��.��wPKS;�VPK�
Q[Content_Types].xml�SMO1���6��m��1���G%@mgنn�t
¿wZ0F�C8M'��{��v4^��ZBD�]Æ|�*p�k�f
{�>׷��$���;h����//F�u�h�aú��:�%r�����Dm�� �\�@\7By���:e
�������
(...)

编辑:为此功能添加了我的Maven依赖项:

        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>3.11</version>
        </dependency>
        <dependency>
            <groupId>org.apache.xmlbeans</groupId>
            <artifactId>xmlbeans</artifactId>
            <version>3.1.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>ooxml-schemas</artifactId>
            <version>1.4</version>
        </dependency>

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...