JAVA使用poi实现excel转html

本文介绍如何利用Java的POI库将Excel文件转换为HTML格式,包括HtmlHelper、XSSFHtmlHelper和HSSFHtmlHelper三个关键类的使用,以及ExcelToHtml的转换流程。
摘要由CSDN通过智能技术生成
  <!-- 针对2007以上版本的库 -->
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>4.1.2</version>
        </dependency>
        <!-- 针对2003版本的库 -->
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-scratchpad</artifactId>
            <version>4.1.2</version>
        </dependency>
        <dependency>
            <groupId>fr.opensagres.xdocreport</groupId>
            <artifactId>fr.opensagres.poi.xwpf.converter.xhtml</artifactId>
            <version>2.0.2</version>
        </dependency>

        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>4.1.2</version>
        </dependency>

 HtmlHelper

package com.luding.common.utils;


import java.util.Formatter;
import org.apache.poi.ss.usermodel.CellStyle;

public interface HtmlHelper {
    void colorStyles(CellStyle var1, Formatter var2);
}

XSSFHtmlHelper 

package com.luding.common.utils;


import java.util.Formatter;

import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFColor;

public class XSSFHtmlHelper implements HtmlHelper {
    public XSSFHtmlHelper() {
    }

    @Override
    public void colorStyles(CellStyle style, Formatter out) {
        XSSFCellStyle cs = (XSSFCellStyle) style;
        this.styleColor(out, "background-color", cs.getFillForegroundXSSFColor());
        this.styleColor(out, "text-color", cs.getFont().getXSSFColor());
    }

    private void styleColor(Formatter out, String attr, XSSFColor color) {
        if (color != null && !color.isAuto()) {
            byte[] rgb = color.getRGB();
            if (rgb != null) {
                out.format("  %s: #%02x%02x%02x;%n", attr, rgb[0], rgb[1], rgb[2]);
                byte[] argb = color.getARGB();
                if (argb != null) {
                    out.format("  %s: rgba(0x%02x, 0x%02x, 0x%02x, 0x%02x);%n", attr, argb[3], argb[0], argb[1], argb[2]);
                }
            }
        }
    }
}

HSSFHtmlHelper

 

package com.luding.common.utils;


import java.util.Formatter;

import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFPalette;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.hssf.util.HSSFColor.HSSFColorPredefined;
import org.apache.poi.ss.usermodel.CellStyle;

public class HSSFHtmlHelper implements HtmlHelper {
    private final HSSFWorkbook wb;
    private final HSSFPalette colors;
    private static final HSSFColor HSSF_AUTO;

    public HSSFHtmlHelper(HSSFWorkbook wb) {
        this.wb = wb;
        this.colors = wb.getCustomPalette();
    }

    @Override
    public void colorStyles(CellStyle style, Formatter out) {
        HSSFCellStyle cs = (HSSFCellStyle) style;
        out.format("  /* fill pattern = %d */%n", cs.getFillPattern().getCode());
        this.styleColor(out, "background-color", cs.getFillForegroundColor());
        this.styleColor(out, "color", cs.getFont(this.wb).getColor());
        this.styleColor(out, "border-left-color", cs.getLeftBorderColor());
        this.styleColor(out, "border-right-color", cs.getRightBorderColor());
        this.styleColor(out, "border-top-color", cs.getTopBorderColor());
        this.styleColor(out, "border-bottom-color", cs.getBottomBorderColor());
    }

    private void styleColor(Formatter out, String attr, short index) {
        HSSFColor color = this.colors.getColor(index);
        if (index != HSSF_AUTO.getIndex() && color != null) {
            short[] rgb = color.getTriplet();
            out.format("  %s: #%02x%02x%02x; /* index = %d */%n", attr, rgb[0], rgb[1], rgb[2], index);
        } else {
            out.format("  /* %s: index = %d */%n", attr, index);
        }

    }

    static {
        HSSF_AUTO = HSSFColorPredefined.AUTOMATIC.getColor();
    }
}

 

ExcelToHtml 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

非ban必选

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值