简体   繁体   English

导出 spring 到 excel

[英]export spring to excel

The problem looks old, the news is that I found working, but only with Firefox. That is the reason I put the question again.问题看起来很老,消息是我发现可以工作,但只能使用 Firefox。这就是我再次提出问题的原因。 Any answer will be appreciated.任何答案将不胜感激。 Thanks!谢谢!

The error message as below:错误信息如下:

ERR_RESPONSE_HEADERS_MULTIPLE_CONTENT_DISPOSITION: getOutputStream() has already been called for this response ERR_RESPONSE_HEADERS_MULTIPLE_CONTENT_DISPOSITION:已经为此响应调用了 getOutputStream()

Cannot suppress a null exception.无法抑制 null 异常。 Self-suppression not permitted Fail to save: an error occurs while saving the package: The part /docProps/core.xml failed to be saved in the stream with marshaller.不允许自抑 Fail to save: an error occurs while saving the package: The part /docProps/core.xml failed to be saved in the stream with marshaller.

The pom:聚甲醛:

<dependency>    
    <groupId>org.apache.poi<groupId>    
    <artifactId>poi-ooxml<artifactId>    
   <version>5.1.0<version>
<dependency>

//controller //控制器

@Controller
@RequestMapping("/show")
public class ShowCPEController {
CPE cpe;

@Autowired
private CpeService cpeService;

@GetMapping("/export-to-excel")
public void exportIntoExcelFile(HttpServletResponse response) throws IOException {
    response.setContentType("application/octet-stream");
    DateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd_HH:mm:ss");
    String currentDateTime = dateFormatter.format(new Date());

    String headerKey = "Content-Disposition";
    String headerValue = "attachment; filename=CPE, "+mainProiect.getBuilding().getTitular()+ currentDateTime + ".xlsx";
    response.setHeader(headerKey, headerValue);

    List <CPE> listOfCpe = cpeService.findAll();

    cpe = cpeService.findByProject(mainProiect.getProjectId());
    mainProiect.setCpe(cpe);
    

    
    CPEtoExcelGenerator generator = new CPEtoExcelGenerator();
    generator.generateExcelFile(response);
}

}

// excel class // excel class

public CPEtoExcelGenerator(){
    workbook = new XSSFWorkbook();
}


public void generateExcelFile(HttpServletResponse response) throws IOException {

    DefineSheet();
    Line1_2();
    Line5();
    Line2127(23, "A", 2, A);
    Line59();
    Line62();
    DrawHomeplate();

    setBorders(CellRangeAddress.valueOf("B4:Ap6"), BorderStyle.THIN);
    setBorders(CellRangeAddress.valueOf("B8:Ap8"), BorderStyle.THIN);
    setBorders(CellRangeAddress.valueOf("A1:AP61"), BorderStyle.MEDIUM);

    try (ServletOutputStream outputStream = response.getOutputStream()) {
      workbook.write(outputStream);
      workbook.close();

  outputStream.flush();
  outputStream.close();
} catch (IOException e) {
  logger.log(Level.SEVERE, "excel ouput err: ", e);
  
}

} }

put already,but posibble not in the correct place:已经放了,但可能不在正确的地方:

response.getOutputStream().close();
response.getOutputStream().flush();

Wish the app to work in any browser as well as with Firefox希望该应用程序可以在任何浏览器以及 Firefox 中运行

I found with very big chance the good answer on below link (search so much also before the answer release).我很有可能在下面的链接上找到了好的答案(在答案发布之前也搜索了很多)。 Tt is because of a stupid comma inside the file name. Tt 是因为文件名中有一个愚蠢的逗号。 As removed, everything OK!如删除,一切OK!

enter link description here 在此处输入链接描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM