根据html导出excel和word

把html字符串转成base64编码传入,直接传入html各种标签后端无法直接接收

接收htmlbase64编码然后通过浏览器下载文件

java 复制代码
@Override
    public void htmlToExcel(ExportPo po, HttpServletResponse response)throws Exception {
        String fileName=po.getFileName()+".xlsx";
        log.info("开始导出"+fileName);
        byte[] decodedBytes = Base64.getDecoder().decode(po.getHtmlBaseCode64());
        ByteArrayInputStream inputStream = new ByteArrayInputStream(decodedBytes);
        Workbook workbook = new Workbook(inputStream);
        response.setContentType("application/octet-stream");
        response.setHeader(HttpHeaders.CONTENT_DISPOSITION,
                "attachment; filename=" + URLEncoder.encode(fileName, "UTF-8"));
        try (ServletOutputStream outputStream = response.getOutputStream()){
            workbook.save(outputStream, SaveFormat.XLSX);
            outputStream.flush();
        }
        log.info(fileName+"导出完成");
    }

    @Override
    public void htmlToWord(ExportPo po, HttpServletResponse response) throws Exception {
        String fileName=po.getFileName()+".docx";
        log.info("开始导出"+fileName);
        byte[] decodedBytes = Base64.getDecoder().decode(po.getHtmlBaseCode64());
        ByteArrayInputStream inputStream = new ByteArrayInputStream(decodedBytes);
        Document doc = new Document(inputStream);
        response.setContentType("application/octet-stream");
        response.setHeader(HttpHeaders.CONTENT_DISPOSITION,
                "attachment; filename=" + URLEncoder.encode(fileName, "UTF-8"));
        try (ServletOutputStream outputStream = response.getOutputStream()){
            doc.save(outputStream, com.aspose.words.SaveFormat.DOCX);
            outputStream.flush();
        }
        log.info(fileName+"导出完成");
    }

读取本地html文件转出后保存到本地

java 复制代码
   private static void htmlToExcel() throws Exception {
        String htmlFile = "C:\\Users\\lenovo\\Desktop\\111.html";
        String excelFile = "C:\\Users\\lenovo\\Desktop\\test2.xlsx";
        Workbook workbook=new Workbook(htmlFile);
        workbook.save(excelFile);
    }

    private static void htmlToWord() throws Exception {
        String htmlFile = "C:\\Users\\lenovo\\Desktop\\111.html";
        String wordFile = "C:\\Users\\lenovo\\Desktop\\保供简报.docx";
        Document doc = new Document(htmlFile);
        doc.save(wordFile, SaveFormat.DOCX);
    }
}
java 复制代码
<dependency>
            <groupId>com.aspose</groupId>
            <artifactId>aspose-cells</artifactId>
            <scope>system</scope>
            <version>25.10</version>
            <systemPath>${project.basedir}/src/main/resources/lib/aspose-cells-25.10.jar</systemPath>
        </dependency>
        <dependency>
            <groupId>com.aspose</groupId>
            <artifactId>aspose-words</artifactId>
            <scope>system</scope>
            <version>19.3</version>
            <systemPath>${project.basedir}/src/main/resources/lib/aspose-words-19.3.jar</systemPath>
        </dependency>
相关推荐
寒山李白7 小时前
解决 python-docx 生成的 Word 文档打开时弹出“无法读取内容“警告
python·word·wps·文档·docx·qoder
DFT计算杂谈13 小时前
VASP官方教程 TRIQS DFT+DMFT计算教程
运维·css·自动化·html·css3
We་ct13 小时前
React 性能优化精讲
前端·javascript·react.js·性能优化·前端框架·html·浏览器
实战编程15 小时前
Temu 插件导出 Excel 图片问题总结(SheetJS / ExcelJS)
excel
Data-Miner17 小时前
用DeepSeek V4做表:数以轻舟Agent让做Excel表像聊天一样简单
microsoft·excel
AI进化营-智能译站19 小时前
ROS2 C++开发系列01:在ROS2上编写第一个C++ hello word
开发语言·c++·ai·word
蜡台19 小时前
使用 html javascript 实现 金币落袋效果
前端·javascript·html
ZC跨境爬虫20 小时前
跟着 MDN 学 HTML day_7:(进阶文本语义标签全覆盖)
前端·javascript·css·ui·html
ZC跨境爬虫20 小时前
跟着 MDN 学 HTML day_6:(HTML文本语义标签全解+lang属性)
前端·ui·html·edge浏览器
Eiceblue2 天前
使用 C# 将 Excel 转换为 Markdown 表格(含批量转换示例)
开发语言·c#·excel