You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用Java Apache POI将HTML表格数据渲染到Excel单元格

基于Apache POI + Jsoup实现HTML表格渲染到Excel的解决方案

核心原理

直接将完整HTML字符串作为单元格值写入Excel时,程序只会将其识别为普通文本展示。正确的逻辑是先解析HTML表格的节点结构,再将行、单元格的内容和样式映射为Excel的对应结构写入。

前置依赖

首先引入所需的第三方包,Maven配置如下:

<!-- Apache POI 相关依赖 -->
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml</artifactId>
    <version>5.2.5</version>
</dependency>
<!-- Jsoup 用于解析HTML节点 -->
<dependency>
    <groupId>org.jsoup</groupId>
    <artifactId>jsoup</artifactId>
    <version>1.17.2</version>
</dependency>

具体实现步骤

  • 用Jsoup解析HTML字符串,提取<table>根节点
  • 遍历表格下的<tr>行节点,逐个对应创建Excel的Row对象
  • 遍历每行下的<td>单元格节点,逐个对应创建Excel的Cell对象,写入<td>的文本内容
  • 可选:将HTML表格的样式(边框、宽度、字体等)转换为POI的CellStyle应用到对应单元格/行/列
  • 可选:处理<td>的colspan、rowspan属性,调用POI的CellRangeAddress实现单元格合并

完整代码示例

import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.FileOutputStream;
import java.io.IOException;

public class HtmlTableToExcel {
    public static void main(String[] args) throws IOException {
        // 你的HTML表格字符串,Jsoup会自动处理转义字符
        String htmlStr = "<table border=\"1\" cellpadding=\"1\" cellspacing=\"1\" style=\"width:500px;\">\n" +
                "<tbody>\n" +
                "<tr>\n" +
                "<td>details</td>\n" +
                "<td>testing</td>\n" +
                "</tr>\n" +
                "<tr><td>1</td>\n" +
                "<td>220</td>\n" +
                "</tr>\n" +
                "<tr><td>3</td>\n" +
                "<td>4</td>\n" +
                "<td>10</td>\n" +
                "</tr></tbody>\n" +
                "</table>";

        // 1. 解析HTML获取表格节点
        Document doc = Jsoup.parse(htmlStr);
        Element table = doc.selectFirst("table");
        Elements rows = table.select("tr");

        // 2. 创建Excel工作簿和工作表
        Workbook workbook = new XSSFWorkbook();
        Sheet sheet = workbook.createSheet("HTML表格转换结果");

        // 可选:创建通用单元格样式,对应HTML的border属性
        CellStyle borderStyle = workbook.createCellStyle();
        borderStyle.setBorderBottom(BorderStyle.THIN);
        borderStyle.setBorderTop(BorderStyle.THIN);
        borderStyle.setBorderLeft(BorderStyle.THIN);
        borderStyle.setBorderRight(BorderStyle.THIN);

        // 3. 逐行写入Excel
        for (int i = 0; i < rows.size(); i++) {
            Element rowEle = rows.get(i);
            Elements cells = rowEle.select("td");
            Row excelRow = sheet.createRow(i);
            for (int j = 0; j < cells.size(); j++) {
                Element cellEle = cells.get(j);
                Cell excelCell = excelRow.createCell(j);
                excelCell.setCellValue(cellEle.text());
                excelCell.setCellStyle(borderStyle);
                // 可选:根据HTML的style设置列宽等
                sheet.setColumnWidth(j, 15 * 256);
            }
        }

        // 4. 输出Excel文件
        try (FileOutputStream fos = new FileOutputStream("html_to_excel.xlsx")) {
            workbook.write(fos);
        } finally {
            workbook.close();
        }
    }
}

常见场景补充处理

  • 如果需要保留HTML中的富文本格式(比如加粗、斜体、颜色),可以解析<td>内的标签,通过POI的XSSFRichTextString实现富文本写入
  • 如果存在单元格合并需求,需要先遍历所有<td>收集colspan、rowspan参数,统一调用sheet.addMergedRegion(new CellRangeAddress(startRow, endRow, startCol, endCol))实现合并
  • 如果HTML中存在<th>表头标签,可单独提取设置表头样式

内容的提问来源于stack exchange,提问作者Ramaiah C

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.25 12:06:02