根据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>
相关推荐
web打印社区1 天前
vue页面打印:printjs实现与进阶方案推荐
前端·javascript·vue.js·electron·html
大三开学菜鸟1 天前
Word 字符数精确统计工具
word
骆驼爱记录1 天前
Word题注编号间距调整4种方法
自动化·word·excel·wps·新人首发
AAA_搬砖达人小郝1 天前
Markdown 一键生成完美 Word(.docx) + PDF 的完整实战方案(JDK 8 环境亲测可用)
pdf·word
放飞自我的Coder1 天前
【Mermaid本地实时渲染 单html本地直接运行】
html·mermaid
共享家95271 天前
打造AI智能”成语接龙“游戏
前端·javascript·人工智能·python·游戏·html
AAA阿giao1 天前
HTML5模块化开发:结构、样式与交互分离
前端·html·html5
顾西爵霞1 天前
HTML 零基础入门:像搭积木一样建网页
前端·html
Eiceblue1 天前
两种实用方法删除 Excel 重复行:Python + VBA
开发语言·python·excel
顾西爵霞1 天前
HTML 进阶:给房子装窗户、通道和前台系统
前端·javascript·html