(POI)Excel格式转Html格式
Demo结构和引⽤的Jar包
源代码(TestDemo.java)
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
l.parsers.DocumentBuilderFactory;
l.transform.OutputKeys;
l.transform.Transformer;
l.transform.TransformerFactory;
l.transform.dom.DOMSource;
l.transform.stream.StreamResult;
import org.apache.verter.ExcelToHtmlConverter;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class TestDemo {
final static String path = "D:\\EclipseWorkspace\\ExcelToHtmlDemo\\ExcelToHtml\\";
final static String file = "TestExcel.xlsx";
private static final String EXCEL_XLS = "xls";
private static final String EXCEL_XLSX = "xlsx";
public static void main(String[] args)
{
try{
InputStream input = new FileInputStream(path +"/"+ file);
HSSFWorkbook excelBook = new HSSFWorkbook();
//判断Excel⽂件将07+版本转换为03版本
dsWith(EXCEL_XLS)){ //Excel 2003
excelBook = new HSSFWorkbook(input);
}
else dsWith(EXCEL_XLSX)){ // Excel 2007/2010
Transform xls = new Transform();
XSSFWorkbook workbookOld = new XSSFWorkbook(input);
ansformXSSF(workbookOld, excelBook);
}
ExcelToHtmlConverter excelToHtmlConverter = new wInstance().newDocumentBuilder().newDocument()); //去掉Excel头⾏
excelToHtmlConverter.setOutputColumnHeaders(false);
//去掉Excel⾏号
excelToHtmlConverter.setOutputRowNumbers(false);
excelToHtmlConverter.processWorkbook(excelBook);
Document htmlDocument = Document();
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
DOMSource domSource = new DOMSource(htmlDocument);
StreamResult streamResult = new StreamResult(outStream);
TransformerFactory tf = wInstance();
Transformer serializer = tf.newTransformer();
serializer.setOutputProperty(OutputKeys.ENCODING, "gb2312");
serializer.setOutputProperty(OutputKeys.INDENT, "yes");
serializer.setOutputProperty(OutputKeys.METHOD, "html");
pdf转ansform(domSource, streamResult);
outStream.close();
//Excel转换成Html
String content = new ByteArray());
System.out.println(content);
}
catch(Exception e) {
e.printStackTrace();
}
}
}
源代码(Transform.java) 将xlsx⽂件转换成xls⽂件。(可以处理合并单元格,边框等格式问题)
package test;
import java.util.HashMap;
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.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.DataFormat;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFFont;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class Transform {
private int lastColumn = 0;
private HashMap<Integer, HSSFCellStyle> styleMap = new HashMap();
public void transformXSSF(XSSFWorkbook workbookOld, HSSFWorkbook workbookNew) { HSSFSheet sheetNew;
XSSFSheet sheetOld;
workbookNew.MissingCellPolicy());
for (int i = 0; i < NumberOfSheets(); i++) {
sheetOld = SheetAt(i);
sheetNew = SheetName());
sheetNew = SheetName());
}
}
private void transform(XSSFWorkbook workbookOld, HSSFWorkbook workbookNew,
XSSFSheet sheetOld, HSSFSheet sheetNew) {
sheetNew.setDisplayFormulas(sheetOld.isDisplayFormulas());
sheetNew.setDisplayGridlines(sheetOld.isDisplayGridlines());
sheetNew.DisplayGuts());
sheetNew.setDisplayRowColHeadings(sheetOld.isDisplayRowColHeadings());
sheetNew.setDisplayZeros(sheetOld.isDisplayZeros());
sheetNew.FitToPage());
sheetNew.HorizontallyCenter());
sheetNew.setMargin(Sheet.BottomMargin,
sheetNew.setMargin(Sheet.FooterMargin,
sheetNew.setMargin(Sheet.HeaderMargin,
sheetNew.setMargin(Sheet.LeftMargin,
sheetNew.setMargin(Sheet.RightMargin,
sheetNew.setMargin(Sheet.TopMargin, Margin(Sheet.TopMargin));
sheetNew.setPrintGridlines(sheetNew.isPrintGridlines());
sheetNew.setRightToLeft(sheetNew.isRightToLeft());
sheetNew.RowSumsBelow());
sheetNew.RowSumsRight());
sheetNew.VerticallyCenter());
HSSFRow rowNew;
for (Row row : sheetOld) {
rowNew = RowNum());
if (rowNew != null)
}
for (int i = 0; i < this.lastColumn; i++) {
sheetNew.setColumnWidth(i, ColumnWidth(i));
sheetNew.setColumnHidden(i, sheetOld.isColumnHidden(i));
}
for (int i = 0; i < NumMergedRegions(); i++) {
CellRangeAddress merged = MergedRegion(i);
sheetNew.addMergedRegion(merged);
}
}
private void transform(XSSFWorkbook workbookOld, HSSFWorkbook workbookNew,
XSSFRow rowOld, HSSFRow rowNew) {
HSSFCell cellNew;
rowNew.Height());
for (Cell cell : rowOld) {
cellNew = ColumnIndex(),
if (cellNew != null)
cellNew);
}
this.lastColumn = Math.max(this.lastColumn, LastCellNum());
}
private void transform(XSSFWorkbook workbookOld, HSSFWorkbook workbookNew,
XSSFCell cellOld, HSSFCell cellNew) {
cellNew.CellComment());
Integer hash = CellStyle().hashCode();
if (this.styleMap != null && !ainsKey(hash)) {
(HSSFCellStyle) ateCellStyle());
}
cellNew.setCellStyle((hash));
switch (CellType()) {
case Cell.CELL_TYPE_BLANK:
break;
case Cell.CELL_TYPE_BOOLEAN:
cellNew.BooleanCellValue());
break;
case Cell.CELL_TYPE_ERROR:
cellNew.ErrorCellValue());
break;
case Cell.CELL_TYPE_FORMULA:
cellNew.CellFormula());
break;
case Cell.CELL_TYPE_NUMERIC:
cellNew.NumericCellValue());
break;
case Cell.CELL_TYPE_STRING:
cellNew.StringCellValue());
break;
default:
System.out.println("transform: Unbekannter Zellentyp "
+ CellType());
}
}
private void transform(XSSFWorkbook workbookOld, HSSFWorkbook workbookNew, Integer hash, XSSFCellStyle styleOld, HSSFCellStyle styleNew) {
styleNew.Alignment());
styleNew.BorderBottom());
styleNew.BorderLeft());
styleNew.BorderRight());
styleNew.BorderTop());
styleNew.ansform(workbookOld, workbookNew,
styleNew.FillBackgroundColor());
styleNew.FillForegroundColor());
styleNew.FillPattern());
styleNew.ansform(workbookNew,
(XSSFFont) Font()));
styleNew.Hidden());
styleNew.Indention());
styleNew.Locked());
styleNew.VerticalAlignment());
styleNew.WrapText());
this.styleMap.put(hash, styleNew);
}
private short transform(XSSFWorkbook workbookOld, HSSFWorkbook workbookNew, short index) {
DataFormat formatOld = ateDataFormat();
DataFormat formatNew = ateDataFormat();
Format(index));
}
private HSSFFont transform(HSSFWorkbook workbookNew, XSSFFont fontOld) {
HSSFFont fontNew = ateFont();
fontNew.Boldweight());
fontNew.CharSet());
fontNew.Color());
fontNew.FontName());
fontNew.FontHeight());
fontNew.Italic());
fontNew.Strikeout());
fontNew.TypeOffset());
fontNew.Underline());
return fontNew;
}
}
发布评论