[alibaba/easyexcel]无法打开excel文件

2024-05-11 157 views
6
    public static void exportExcel(HttpServletResponse response, String fileName, Class head, List list){
        try {
            response.setContentType("application/octet-stream;charset=UTF-8");
            response.setHeader("Content-Disposition", "attachment;filename="+ URLEncoder.encode(fileName, "UTF-8"));
            EasyExcel.write(response.getOutputStream(), head).sheet("sheet").doWrite(list);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

@Data
public class ExportScrapDTO implements Serializable {
    @ExcelProperty(value = "条码号", index = 0)
    private String barcode;

    @ExcelProperty(value = "题名", index = 1)
    private String title;

    @ExcelProperty(value = "装订卷期", index = 2)
    private String bindingDate;

    @NumberFormat("#.##")
    @ExcelProperty(value = "价格", index = 3)
    private BigDecimal price;

    @ExcelProperty(value = "issn", index = 4)
    private String issn;

    @ExcelProperty(value = "统一刊号", index = 5)
    private String periodicalNumber;

    @ExcelProperty(value = "邮发代号", index = 6)
    private String mailCode;

    @ExcelProperty(value = "分类号", index = 7)
    private String classNumber;

    @ExcelProperty(value = "书次号", index = 8)
    private String bookNumber;

    @ExcelProperty(value = "索取号", index = 9)
    private String requestNumber;

    @ExcelProperty(value = "出版社", index = 10)
    private String publisher;

    @ExcelProperty(value = "出版年份", index = 11)
    private String publicationYear;

    @ExcelProperty(value = "馆藏地", index = 12)
    private String holdingSite;

    @ExcelProperty(value = "馆藏状态", index = 13)
    private String holdingStatus;

    @ExcelProperty(value = "报废时间", index = 14)
    private String updateTime;
}

异常提示 没有异常显示 建议描述 返回正常 前端其他接口也是这样 就这个导出excel文件 打不开 其他都是正常的

回答

7

sevice代码补充 public void exportScraps(String queryKey, String queryValue, String startDate, String endDate, Integer collectionId, Long orgId, HttpServletResponse response ) { List list = getPeriodicalBindings(queryKey, queryValue, startDate, endDate, HoldingStatusEnum.GETRIDOF_SCRAP_STATUS.getIndex(), collectionId, orgId); List results = new ArrayList<>(); list.forEach(k ->{ ExportScrapDTO item = new ExportScrapDTO(); BeanUtils.copyProperties(k, item); results.add(item); }); String fileName = "期刊报废清单.xls"; ExportExcelUtils.exportExcel(response, fileName, ExportScrapDTO.class, results); }

controller 代码补充 @ApiOperation("导出期刊报费列表") @GetMapping("/exportScraps") public void exportBindings(@RequestParam(value = "queryKey", required = false) String queryKey, @RequestParam(value = "queryValue", required = false) String queryValue, @RequestParam(value = "startDate", required = false) String startDate, @RequestParam(value = "endDate", required = false) String endDate, @RequestParam(value = "collectionId", required = false) Integer collectionId, HttpServletResponse response ){ periodicalBindingService.exportScraps(queryKey, queryValue, startDate, endDate, collectionId, this.libraryId, response); }

6

前端问题

8

我也是这样, image 点,是 也修复不好啊//?



public static File writeWithFile(String fileName,
                                     Class<?> modelClazz,
                                     List<?> data,
                                     Integer sheetNo,
                                     Integer tableNo,
                                     String sheetName,
                                     String suffix) {

        String myFileName = fileTempPath() + (StringUtils.isNotBlank(fileName) ? fileName : TABLE_NAME)
                + (StringUtils.isNotBlank(suffix) ? suffix : SUFFIX);
        File myFile = new File(myFileName);
        Objects.requireNonNull(modelClazz);
        write(myFile, modelClazz)
                .sheet(sheetName.isBlank() ? Fields.SHEET_NAME : sheetName)
                .sheetNo(Objects.isNull(sheetNo) ? SHEET_NO : sheetNo)
                .table(Objects.isNull(tableNo) ? TABLE_NO : tableNo)
                .doWrite(data);
        return myFile;
    }
`

点击文件属性,解除锁定,就能打开了。。