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

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

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

问题

我想实现一个方法该方法从数据库下载一个包含表格记录的 .xls 文件我正在工作中没有任何模板以便专注于后端问题是当我运行应用程序和相应的方法时下载没有开始记录会显示在屏幕上

ClienteService

@Override
public ByteArrayInputStream exportData() throws Exception {
    String[] columnas = {"Núm. Cliente", "Nombre", "Apellido", "Dirección", "Activo"};

    Workbook workbook = new HSSFWorkbook();
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    Sheet sheet = workbook.createSheet("Clientes");
    Row row = sheet.createRow(0);

    for (int i = 0; i < columnas.length; i++) {
        Cell cell = row.createCell(i);
        cell.setCellValue(columnas[i]);
    }

    List<E01_cliente> clientes = (List<E01_cliente>) clienteRepository.findAll();
    int initRow = 1;
    for (E01_cliente c : clientes) {
        row = sheet.createRow(initRow);
        row.createCell(0).setCellValue(c.getNro_cliente());
        row.createCell(1).setCellValue(c.getNombre());
        row.createCell(2).setCellValue(c.getApellido());
        row.createCell(3).setCellValue(c.getDireccion());
        row.createCell(4).setCellValue(c.isActivo());
        initRow++;
    }

    workbook.write(stream);
    workbook.close();
    return new ByteArrayInputStream(stream.toByteArray());
}

Controller 的方法

@GetMapping("/descargar")
public ResponseEntity<InputStreamResource> exportData() throws Exception {
    ByteArrayInputStream stream = clienteService.exportData();
    HttpHeaders headers = new HttpHeaders();
    headers.add("Content-Disposition", "attachment; filename=clientes.xls");
    return ResponseEntity.ok().headers(headers).body(new InputStreamResource(stream));
}

Entity Cliente

@Entity
public class E01_cliente {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int nro_cliente;
    private String nombre;
    private String apellido;
    private String direccion;
    private boolean activo;

    @OneToMany(mappedBy = "cliente")
    @JsonIgnore
    private List<E01_factura> facturas;
    // 省略了 Getter 和 Setter
}
英文:

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:

@Override
public ByteArrayInputStream exportData() throws Exception {
String[] columnas = {&quot;N&#250;mero Cliente&quot;, &quot;Nombre&quot;, &quot;Apellido&quot;, &quot;Direcci&#243;n&quot;, &quot;Activo&quot;};
Workbook workbook = new HSSFWorkbook();
ByteArrayOutputStream stream = new ByteArrayOutputStream();
Sheet sheet = workbook.createSheet(&quot;Clientes&quot;);
Row row = sheet.createRow(0);
for(int i=0; i&lt;columnas.length; i++){
Cell cell = row.createCell(i);
cell.setCellValue(columnas[i]);
}
List&lt;E01_cliente&gt; clientes = (List&lt;E01_cliente&gt;) clienteRepository.findAll();
int initRow = 1;
for(E01_cliente c : clientes){
row = sheet.createRow(initRow);
row.createCell(0).setCellValue(c.getNro_cliente());
row.createCell(1).setCellValue(c.getNombre());
row.createCell(2).setCellValue(c.getApellido());
row.createCell(3).setCellValue(c.getDireccion());
row.createCell(4).setCellValue(c.isActivo());
initRow++;
}
workbook.write(stream);
workbook.close();
return new ByteArrayInputStream(stream.toByteArray());
}

Method of Controller:

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

Entity Cliente:

@Entity
public class E01_cliente {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int nro_cliente;
private String nombre;
private String apellido;
private String direccion;
private boolean activo;
@OneToMany(mappedBy = &quot;cliente&quot;)
@JsonIgnore
private List&lt;E01_factura&gt; facturas;
//Getters and Setters ignored

答案1

得分: 1

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

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

Use Content-Disposition instead of Content-Dispotion as Header

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:

确定