<!-- 针对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