使用Spring Boot和Apache POI下载 .xls 文件无法正常工作。

huangapple go评论117阅读模式
英文:

Download .xls file using Spring boot y Apache POI does not work

问题

  1. 我想实现一个方法该方法从数据库下载一个包含表格记录的 .xls 文件我正在工作中没有任何模板以便专注于后端问题是当我运行应用程序和相应的方法时下载没有开始记录会显示在屏幕上
  2. ClienteService
  3. @Override
  4. public ByteArrayInputStream exportData() throws Exception {
  5. String[] columnas = {"Núm. Cliente", "Nombre", "Apellido", "Dirección", "Activo"};
  6. Workbook workbook = new HSSFWorkbook();
  7. ByteArrayOutputStream stream = new ByteArrayOutputStream();
  8. Sheet sheet = workbook.createSheet("Clientes");
  9. Row row = sheet.createRow(0);
  10. for (int i = 0; i < columnas.length; i++) {
  11. Cell cell = row.createCell(i);
  12. cell.setCellValue(columnas[i]);
  13. }
  14. List<E01_cliente> clientes = (List<E01_cliente>) clienteRepository.findAll();
  15. int initRow = 1;
  16. for (E01_cliente c : clientes) {
  17. row = sheet.createRow(initRow);
  18. row.createCell(0).setCellValue(c.getNro_cliente());
  19. row.createCell(1).setCellValue(c.getNombre());
  20. row.createCell(2).setCellValue(c.getApellido());
  21. row.createCell(3).setCellValue(c.getDireccion());
  22. row.createCell(4).setCellValue(c.isActivo());
  23. initRow++;
  24. }
  25. workbook.write(stream);
  26. workbook.close();
  27. return new ByteArrayInputStream(stream.toByteArray());
  28. }
  29. Controller 的方法
  30. @GetMapping("/descargar")
  31. public ResponseEntity<InputStreamResource> exportData() throws Exception {
  32. ByteArrayInputStream stream = clienteService.exportData();
  33. HttpHeaders headers = new HttpHeaders();
  34. headers.add("Content-Disposition", "attachment; filename=clientes.xls");
  35. return ResponseEntity.ok().headers(headers).body(new InputStreamResource(stream));
  36. }
  37. Entity Cliente
  38. @Entity
  39. public class E01_cliente {
  40. @Id
  41. @GeneratedValue(strategy = GenerationType.IDENTITY)
  42. private int nro_cliente;
  43. private String nombre;
  44. private String apellido;
  45. private String direccion;
  46. private boolean activo;
  47. @OneToMany(mappedBy = "cliente")
  48. @JsonIgnore
  49. private List<E01_factura> facturas;
  50. // 省略了 Getter 和 Setter
  51. }
英文:

I would like to implement a method that downloads an .xls file with the records of a table from a database. I am working without any template to focus on the backend, the problem is that when I run the application and the corresponding method, the download does not start and the records "appears" on the screen

使用Spring Boot和Apache POI下载 .xls 文件无法正常工作。

ClienteService:

  1. @Override
  2. public ByteArrayInputStream exportData() throws Exception {
  3. String[] columnas = {&quot;N&#250;mero Cliente&quot;, &quot;Nombre&quot;, &quot;Apellido&quot;, &quot;Direcci&#243;n&quot;, &quot;Activo&quot;};
  4. Workbook workbook = new HSSFWorkbook();
  5. ByteArrayOutputStream stream = new ByteArrayOutputStream();
  6. Sheet sheet = workbook.createSheet(&quot;Clientes&quot;);
  7. Row row = sheet.createRow(0);
  8. for(int i=0; i&lt;columnas.length; i++){
  9. Cell cell = row.createCell(i);
  10. cell.setCellValue(columnas[i]);
  11. }
  12. List&lt;E01_cliente&gt; clientes = (List&lt;E01_cliente&gt;) clienteRepository.findAll();
  13. int initRow = 1;
  14. for(E01_cliente c : clientes){
  15. row = sheet.createRow(initRow);
  16. row.createCell(0).setCellValue(c.getNro_cliente());
  17. row.createCell(1).setCellValue(c.getNombre());
  18. row.createCell(2).setCellValue(c.getApellido());
  19. row.createCell(3).setCellValue(c.getDireccion());
  20. row.createCell(4).setCellValue(c.isActivo());
  21. initRow++;
  22. }
  23. workbook.write(stream);
  24. workbook.close();
  25. return new ByteArrayInputStream(stream.toByteArray());
  26. }

Method of Controller:

  1. @GetMapping(&quot;/descargar&quot;)
  2. public ResponseEntity&lt;InputStreamResource&gt; exportData() throws Exception {
  3. ByteArrayInputStream stream = clienteService.exportData();
  4. HttpHeaders headers = new HttpHeaders();
  5. headers.add(&quot;Content-Dispotion&quot;, &quot;attachment; filename=clientes.xls&quot;);
  6. return ResponseEntity.ok().headers(headers).body(new InputStreamResource(stream));
  7. }

Entity Cliente:

  1. @Entity
  2. public class E01_cliente {
  3. @Id
  4. @GeneratedValue(strategy = GenerationType.IDENTITY)
  5. private int nro_cliente;
  6. private String nombre;
  7. private String apellido;
  8. private String direccion;
  9. private boolean activo;
  10. @OneToMany(mappedBy = &quot;cliente&quot;)
  11. @JsonIgnore
  12. private List&lt;E01_factura&gt; facturas;
  13. //Getters and Setters ignored

答案1

得分: 1

使用 Content-Disposition 替代 Content-Dispotion 作为头部

  1. headers.add("Content-Disposition", "attachment; filename=clientes.xls");
英文:

Use Content-Disposition instead of Content-Dispotion as Header

  1. headers.add(&quot;Content-Disposition&quot;, &quot;attachment; filename=clientes.xls&quot;);

huangapple
  • 本文由 发表于 2020年5月30日 23:00:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/62104279.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定