generate html excel

package com.sun.test.htmlexcel;

import lombok.Data;

@Data
public class CrossRangeCellMeta {
/*
* @param firstRowIndex 起始行
* @param firstColIndex 起始列
* @param rowSpan 合并行数目
* @param colSpan 合并列数目
*/
int lastRow;
int firstRow;
int lastCol;
int firstCol;

public CrossRangeCellMeta(int lastRow,int firstRow,int lastCol,int firstCol){
this.lastRow=lastRow;
this.firstRow=firstRow;
this.lastCol=lastCol;
this.firstCol=firstCol;
}
}

=======================================================================================================================

package com.sun.test.htmlexcel;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFCellUtil;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.util.CellRangeAddress;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

public class table2Excel {

public static HSSFWorkbook table2Excel(String tableHtml) throws DocumentException{
HSSFWorkbook wb=new HSSFWorkbook();
HSSFSheet sheet =wb.createSheet();
List<CrossRangeCellMeta> crossRowEleMetaLs=new ArrayList<CrossRangeCellMeta>();
Document data =DocumentHelper.parseText(tableHtml);
System.out.println("="+data);
int rowIndex = 0;
// 生成表头
Element thead = data.getRootElement().element("thead");
HSSFCellStyle titleStyle =getTitleStyle(wb);
int ls=0;//列数
if(thead!=null){
List<Element> trLs =thead.elements("tr");
System.out.println("="+trLs.size());
for(Element trEle :trLs){
HSSFRow row =sheet.createRow(rowIndex);
List<Element> thLs = trEle.elements("th");
thLs.forEach(e->System.out.println(e.attribute(1)));
ls=thLs.size();
makeRowCell(thLs, rowIndex, row, 0, titleStyle, crossRowEleMetaLs);
rowIndex++;
}
}
// 生成表体
Element tbody = data.getRootElement().element("tbody");
HSSFCellStyle contentStyle = getContentStyle(wb);
if (tbody != null) {
List<Element> trLs = tbody.elements("tr");
for (Element trEle : trLs) {
HSSFRow row = sheet.createRow(rowIndex);
List<Element> thLs = trEle.elements("th");
int cellIndex = makeRowCell(thLs, rowIndex, row, 0, titleStyle, crossRowEleMetaLs);
List<Element> tdLs = trEle.elements("td");
makeRowCell(tdLs, rowIndex, row, cellIndex, contentStyle, crossRowEleMetaLs);
rowIndex++;
}
}
// 合并表头
for (CrossRangeCellMeta crcm : crossRowEleMetaLs) {
sheet.addMergedRegion(new CellRangeAddress(crcm.getFirstRow(), crcm.getLastRow(), crcm.getFirstCol(), crcm.getLastCol()));
setRegionStyle(sheet, new CellRangeAddress(crcm.getFirstRow(), crcm.getLastRow(), crcm.getFirstCol(), crcm.getLastCol()),contentStyle);
}
for(int i=0;i<ls;i++){
sheet.autoSizeColumn(i, true);//设置列宽
}

return wb;
}
/**
* 获得内容样式
*
* @param wb
* @return
*/
private static HSSFCellStyle getContentStyle(HSSFWorkbook wb) {
short fontSize = 12;
String fontName = "宋体";
HSSFCellStyle style = wb.createCellStyle();
style.setBorderBottom((short) 1);
style.setBorderTop((short) 1);
style.setBorderLeft((short) 1);
style.setBorderRight((short) 1);
HSSFFont font = wb.createFont();
font.setFontName(fontName);
font.setFontHeightInPoints(fontSize);
style.setFont(font);
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);//水平居中
style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);//垂直居中

return style;
}
/**
* 设置合并单元格的边框样式
*
* @param sheet
* @param region
* @param cs
*/
public static void setRegionStyle(HSSFSheet sheet, CellRangeAddress region, HSSFCellStyle cs) {
for (int i = region.getFirstRow(); i <= region.getLastRow(); i++) {
HSSFRow row = HSSFCellUtil.getRow(i, sheet);
for (int j = region.getFirstColumn(); j <= region.getLastColumn(); j++) {
HSSFCell cell = HSSFCellUtil.getCell(row, (short) j);
cell.setCellStyle(cs);
}
}
}

/ **
* Production line content
*
* @return index of the last column Cell

* @param TDLS td set or TH
* @param rowIndex line number
* @param row POI row object
* @param startCellIndex
* @param CellStyle Style
* @param crossRowEleMetaLs interbank set of metadata
* @return
* /
Private static int makeRowCell (List <the element> TDLS, int rowIndex, HSSFRow Row, int startCellIndex, HSSFCellStyle CellStyle,
List <CrossRangeCellMeta> crossRowEleMetaLs) {
int I = startCellIndex;
for (int = 0 eleIndex ; eleIndex <tdLs.size (); I ++, eleIndex ++) {
int = captureCellSize getCaptureCellSize (rowIndex, I, crossRowEleMetaLs);
the while (captureCellSize> 0) {
for (int j = 0; j < captureCellSize; j++) {// 当前行跨列处理(补单元格)
row.createCell(i);
i++;
}
captureCellSize = getCaptureCellSize(rowIndex, i, crossRowEleMetaLs);
}
Element thEle = tdLs.get(eleIndex);
String val = thEle.getTextTrim();
if (StringUtils.isBlank(val)) {
Element e = thEle.element("a");
if (e != null) {
val = e.getTextTrim();
}
}
HSSFCell c = row.createCell(i);
if (NumberUtils.isNumber(val)) {
c.setCellValue(Double.parseDouble(val));
c.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
} else {
c.setCellValue(val);
}
NumberUtils.toInt rowSpan = int (thEle.attributeValue ( "rowspan"),. 1);
int = colSpan NumberUtils.toInt (thEle.attributeValue ( "colspan"),. 1);
c.setCellStyle (CellStyle);
IF (rowSpan>. 1 || colSpan> 1) {// interbank presence or across columns
crossRowEleMetaLs.add (new new CrossRangeCellMeta (rowIndex, I, rowSpan, colSpan));
}
IF (colSpan>. 1) {// current row across the column processing (fill cell )
for (int. 1 = J; J <colSpan; J ++) {
I ++;
row.createCell (I);
}
}
}
return I;
}
/ **
* obtained cell occupied by rowSpan
*
* @param rowIndex line number
* @param colIndex column number
* @param crossRowEleMetaLs across the ranks metadata
* @return the current row of a column need to occupy the cell
* /
private static int getCaptureCellSize(int rowIndex, int colIndex, List<CrossRangeCellMeta> crossRowEleMetaLs) {
int captureCellSize = 0;
for (CrossRangeCellMeta crossRangeCellMeta : crossRowEleMetaLs) {
if (crossRangeCellMeta.getFirstRow() < rowIndex && crossRangeCellMeta.getLastRow() >= rowIndex) {
if (crossRangeCellMeta.getFirstCol() <= colIndex && crossRangeCellMeta.getLastCol() >= colIndex) {
captureCellSize = crossRangeCellMeta.getLastCol() - colIndex + 1;
}
}
}
return captureCellSize;
}
/**
* 获得标题样式
* @param ws
* @return
*/
public static HSSFCellStyle getTitleStyle(HSSFWorkbook ws){
short titlebackgroundcolor =HSSFColor.GREY_25_PERCENT.index;
short fontSize = 12;
String fontName = "宋体";
HSSFCellStyle style =ws.createCellStyle();
style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
style.setBorderBottom(HSSFCellStyle.BORDER_THIN); //下边框
style.setBorderLeft(HSSFCellStyle.BORDER_THIN);//左边框
style.setBorderTop(HSSFCellStyle.BORDER_THIN);//上边框
style.setBorderRight(HSSFCellStyle.BORDER_THIN);//右边框
style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
style.setFillForegroundColor(titlebackgroundcolor);// 背景色
HSSFFont font =ws.createFont();
font.setFontName(fontName);
font.setFontHeight(HSSFFont.SYMBOL_CHARSET);
font.setFontHeightInPoints(fontSize);
style.setFont(font);
return style;
}

public static void main(String[] args) throws DocumentException {

File f=new File("");
System.out.println(f.getAbsolutePath());
String s="'1.0'";
String g="' UTF-8 '";
HSSFWorkbook wb=table2Excel(" <table><thead><tr><th>xingming</th></tr> </thead></table>");
// HSSFWorkbook wb=table2Excel("D:/代码/test/test/src/main/resources/test.xml");
}
}

Guess you like

Origin www.cnblogs.com/sunjinwei/p/12033384.html