簡體   English   中英

使用 Spring 引導下載.xls 文件 y Apache POI 不起作用

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

我想實現一種方法,該方法從數據庫中下載帶有表記錄的.xls 文件。 我在沒有任何模板的情況下專注於后端,問題是當我運行應用程序和相應的方法時,下載沒有開始並且屏幕上“出現”記錄

在此處輸入圖像描述

客戶服務:

@Override
    public ByteArrayInputStream exportData() throws Exception {
        String[] columnas = {"Número 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-Dispotion", "attachment; filename=clientes.xls");
    return ResponseEntity.ok().headers(headers).body(new InputStreamResource(stream));
}

實體客戶:

@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;
    //Getters and Setters ignored

使用Content-Disposition代替Content-Dispotion作為 Header

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM