写着写着就不知道自己定义的方法是干嘛的了

@GetMapping("/export")
    public void export(HttpServletRequest request, HttpServletResponse response) {
        File file = FileUtils.getExcelFile("docx文件名");
        InputStream is = this.getClass().getClassLoader().getResourceAsStream("templates/customized/" + "auditFromBeforeFundPayment" + ".docx");
        try (OutputStream os = new FileOutputStream(file)) {
            XWPFDocument document = new XWPFDocument(is);
            XWPFTable xwpfTable = document.getTables().get(0);
            List<XWPFTableRow> rows = xwpfTable.getRows();
            rows.get(0).getTableCells().get(1).setText("项目名称");
            XWPFTableRow row1 = rows.get(1);
            List<XWPFTableCell> row1Cells = row1.getTableCells();
            row1Cells.get(1).setText("项目编号");
            row1Cells.get(3).setText("协议编号");
            is.close();
            document.write(os);
        } catch (IOException e) {
            throw new BadRequestException(e.getMessage());
        } catch (Exception e) {
            System.out.println(e.getMessage());
            throw new BadRequestException(e.getMessage());
        }
        FileUtils.downloadFile(request, response,file);
    }

这行代码一写上,docx文件下载一直报错,无法正常打开。搞得我有点蒙蔽。

我excel就这么写的啊,都能下载。

然后想着是不是content-type或者啥的没有定义好?搞错了?然后一看代码,没这个玩意的代码啊。

蒙蔽,想了下,是不是因为第一行代码的fileName中没有加上docx,这个时候蒙蔽了,给忘了方法是干什么的。

最后花了差不多20分钟,突然想到进去方法里面看看,发现getExcelFile是用来下载xlsx文件的。修改下方法。

原文地址:https://www.cnblogs.com/woyujiezhen/p/13868081.html