根据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>
相关推荐
それども1 小时前
Apache POI XSSFWorkbook 和 SXSSFWorkbook 的区别
apache·excel
公子如琢1 小时前
在word方框中打√符号☑☒
word
松涛和鸣6 小时前
49、智能电源箱项目技术栈解析
服务器·c语言·开发语言·http·html·php
—Miss. Z—8 小时前
Power Query数据分类整合
excel
智航GIS11 小时前
10.5 PyQuery:jQuery 风格的 Python HTML 解析库
python·html·jquery
开开心心就好12 小时前
系统管理工具,多功能隐私清理文件粉碎工具
java·网络·windows·r语言·电脑·excel·symfony
sinat_3751122615 小时前
abap excel上传
excel·上传·sap·abap
·云扬·16 小时前
【实操教程】Excel文件转CSV并导入MySQL的完整步骤
android·mysql·excel
城数派16 小时前
2019-2025年各区县逐月新房房价数据(Excel/Shp格式)
大数据·数据分析·excel
Elieal16 小时前
EasyExcel 实现 Excel 导入导出
java·excel