利用poi将Html中table转为Excel

该博客介绍了如何使用Java的POI库将HTML中的表格转换为Excel文件。作者提供了从HTML设计器导出的表格样式,详细讲解了所需的依赖、工具文件的创建,并给出了关键代码示例。文章末尾分享了相关项目的源码链接。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

需求:之前做了一个web端的excel设计器,现需要将设计好的表格获取html代码,将html转换为excel文件进行输出

一:最终效果:

html设计器制作的表格


最终生成的excel表格:


注意点:table中样式要按照标准格式去写:例如

style="font-size: 12px;border: 1px solid #000000"

二:引入依赖

pom文件:

<!-- https://mvnrepository.com/artifact/org.apache.poi/poi -->
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>3.14</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/dom4j/dom4j -->
        <dependency>
            <groupId>dom4j</groupId>
            <artifactId>dom4j</artifactId>
            <version>1.6.1</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 -->
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.8.1</version>
        </dependency>复制代码

三:创建工具文件

函数作用大家看注释就可以

CrossRangeCellMeta.java

package com.example.demo;

/**
 * @Description:
 * @Author: zy
 * @CreateDate: 2019/4/10 14:30
 */
public class CrossRangeCellMeta {

    public CrossRangeCellMeta(int firstRowIndex, int firstColIndex, int rowSpan, int colSpan) {
        super();
        this.firstRowIndex = firstRowIndex;
        this.firstColIndex = firstColIndex;
        this.rowSpan = rowSpan;
        this.colSpan = colSpan;
    }

    private int firstRowIndex;
    private int firstColIndex;
    private int rowSpan;// 跨越行数
    private int colSpan;// 跨越列数

    int getFirstRow() {
        return firstRowIndex;
    }

    int getLastRow() {
        return firstRowIndex + rowSpan - 1;
    }

    int getFirstCol() {
        return firstColIndex;
    }

    int getLastCol() {
        return firstColIndex + colSpan - 1;
    }

    int getColSpan(){
        return colSpan;
    }
}
复制代码

ConvertHtml2Excel.java

package com.example.demo;

import java.io.File;
import java.io.FileOutputStream;
import java.util.*;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.formula.FormulaParseException;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.ss.util.RegionUtil;
import org.dom4j.*;


/**
 * @Description:
 * @Author: zy
 * @CreateDate: 2019/4/10 14:30
 */
public class ConvertHtml2Excel {

    private static final Map<String, Short> CSS_TO_POI_STYLE = new HashMap<>();

    //像素转excel中高度转换率
    private static final double PX_TO_EXCEL_HEIGHT = 15;

    //像素转excel中宽度转换率
    private static final double PX_TO_EXCEL_WIDTH = 37.5;

    ConvertHtml2Excel() {
        CSS_TO_POI_STYLE.put("center", HSSFCellStyle.ALIGN_CENTER);
        CSS_TO_POI_STYLE.put("left", HSSFCellStyle.ALIGN_LEFT);
        CSS_TO_POI_STYLE.put("start", HSSFCellStyle.ALIGN_LEFT);
        CSS_TO_POI_STYLE.put("right", HSSFCellStyle.ALIGN_RIGHT);
        CSS_TO_POI_STYLE.put("end", HSSFCellStyle.ALIGN_RIGHT);
        CSS_TO_POI_STYLE.put("top", HSSFCellStyle.VERTICAL_TOP);
        CSS_TO_POI_STYLE.put("middle", HSSFCellStyle.VERTICAL_CENTER);
        CSS_TO_POI_STYLE.put("bottom", HSSFCellStyle.VERTICAL_BOTTOM);
        CSS_TO_POI_STYLE.put("solid", CellStyle.BORDER_THIN);
        CSS_TO_POI_STYLE.put("dashed", CellStyle.BORDER_DASHED);
        CSS_TO_POI_STYLE.put("double", CellStyle.BORDER_DOUBLE);
    }

    public static void main(String[] args) {
        new ConvertHtml2Excel();
        //生成的前端html的table代码
        String htmlStr = "<table><tbody><tr style=\"height: 25px;\"><td style=\"width: 68px;\">单位名称</td><td style=\"width: 66px;text-align: center;\" rowspan=\"1\" colspan=\"9\">深圳********有限公司</td><td style=\"width: 100px; display: none; text-align: center;\"></td><td style=\"width: 100px; display: none; text-align: center;\"></td><td style=\"width: 123px; display: none; text-align: center;\"></td><td style=\"width: 100px; display: none; text-align: center;\"></td><td style=\"width: 100px; display: none; text-align: center;\"></td><td style=\"width: 100px; display: none; text-align: center;\"></td><td style=\"width: 100px; display: none; text-align: center;\"></td><td style=\"width: 100px; text-align: center; display: none;\"></td></tr><tr style=\"height: 25px;\"><td style=\"width: 68px; vertical-align: middle; text-align: start;\">主标题</td><td style=\"width: 66px; text-align: center;\" rowspan=\"1\" colspan=\"9\">应收款明细表</td><td style=\"width: 100px; display: none; text-align: center;\"></td><td style=\"width: 100px; display: none; text-align: center;\"></td><td style=\"width: 123px; display: none; text-align: center;\"></td><td style=\"width: 100px; display: none; text-align: center;\"></td><td style=\"width: 100px; display: none; text-align: center;\"></td><td style=\"width: 100px; display: none; text-align: center;\"></td><td style=\"width: 100px; display: none; text-align: center;\"></td><td style=\"width: 100px; display: none; text-align: center;\"></td></tr><tr style=\"height: 25px;\"><td style=\"width: 68px; vertical-align: middle; text-align: start;\">副标题</td><td style=\"width: 66px; text-align: center;\" rowspan=\"1\" colspan=\"9\">统计月份(2019-01至2019-03)</td><td style=\"width: 100px; display: none;\"></td><td style=\"width: 100px; display: none;\"></td><td style=\"width: 123px; display: none;\"></td><td style=\"width: 100px; display: none;\"></td><td style=\"width: 100px; display: none;\"></td><td style=\"width: 100px; display: none;\"></td><td style=\"width: 100px; display: none;\"></td><td style=\"width: 100px; display: none;\"></td></tr><tr style=\"height: 25px;\"><td style=\"width: 68px; vertical-align: middle; text-align: start;\">附注</td><td style=\"width: 66px;\" rowspan=\"1\" colspan=\"3\">物业名称:豪龙大厦</td><td style=\"width: 100px; display: none;\"></td><td style=\"width: 100px; display: none;\"></td><td style=\"width: 123px;\"></td><td style=\"width: 100px;\"><
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值