需求:之前做了一个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;\"><