根据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>
相关推荐
信看12 小时前
NMEA-GNSS-RTK 定位html小工具
前端·javascript·html
bug总结15 小时前
前端开发中为什么要使用 URL().origin 提取接口根地址
开发语言·前端·javascript·vue.js·html
喝汽水的猫^17 小时前
Java实现Excel 导出(多 Sheet、复杂格式)
java·excel
小费的部落19 小时前
Excel 在Sheet3中 匹配Sheet1的A列和Sheet2的A列并处理空内容
java·前端·excel
缺点内向20 小时前
如何在 C# 中重命名 Excel 工作表并设置标签颜色
开发语言·c#·excel
精神病不行计算机不上班1 天前
[Java Web]在IDEA中完整实现Servlet的示例
java·servlet·tomcat·html·intellij-idea·web
顾安r1 天前
12.17 脚本工具 自动化全局跳转
linux·前端·css·golang·html
苏打水com1 天前
第十七篇:Day49-51 前端工程化进阶——从“手动”到“自动化”(对标职场“提效降本”需求)
前端·javascript·css·vue.js·html
CodeCraft Studio1 天前
国产化Word处理组件Spire.DOC教程:使用Python将文件自动化批量附加到 Word文档
python·自动化·word·spire.doc·文档自动化·word文档中添加附件·文档批量处理
东方佑1 天前
使用Python实现Word文档与JSON格式双向转换:完整教程与代码解析
python·json·word