package com.sinosoft.pageOffice.util;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFDataFormat;
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
import org.apache.poi.hssf.usermodel.HSSFFont;
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.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFColor;
import org.apache.poi.xssf.usermodel.XSSFFont;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
/**
* excel转html工具类
* @author pengju
*
*/
public class PoiExcelToHtmlUtil {
public void produceHtmlFromExcel(String rootPath,String name){//name带后缀名
String path = rootPath+"//"+name;
// String path = "D://poi-test//excelToHtml//德联易控CE平台对接开发计划V1.1.xlsx";
String[] strs = name.split("\\.");
String fileName = strs[0];
InputStream is = null;
String htmlExcel = null;
InputStream inputStream = null;
FileOutputStream outputStream = null;
try {
File sourcefile = new File(path);
is = new FileInputStream(sourcefile);
Workbook wb = WorkbookFactory.create(is);// 此WorkbookFactory在POI-3.10版本中使用需要添加dom4j
if (wb instanceof XSSFWorkbook) {
XSSFWorkbook xWb = (XSSFWorkbook) wb;
htmlExcel = PoiExcelToHtmlUtil.getExcelInfo(xWb, true);
} else if (wb instanceof HSSFWorkbook) {
HSSFWorkbook hWb = (HSSFWorkbook) wb;
htmlExcel = PoiExcelToHtmlUtil.getExcelInfo(hWb, true);
}
inputStream = new ByteArrayInputStream(htmlExcel.getBytes());
outputStream = new FileOutputStream(new File(rootPath+"//"+fileName+".html"));
int temp = 0;
while((temp = inputStream.read())!=-1){
outputStream.write(temp);
}
outputStream.flush();
System.out.println(htmlExcel);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
outputStream.close();
inputStream.close();
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 测试
*
* @param args
*
/*
public static void main(String[] args) {
String path = "D://poi-test//excelToHtml//德联易控CE平台对接开发计划V1.1.xlsx";
InputStream is = null;
String htmlExcel = null;
InputStream inputStream = null;
FileOutputStream outputStream = null;
try {
File sourcefile = new File(path);
is = new FileInputStream(sourcefile);
Workbook wb = WorkbookFactory.create(is);// 此WorkbookFactory在POI-3.10版本中使用需要添加dom4j
if (wb instanceof XSSFWorkbook) {
XSSFWorkbook xWb = (XSSFWorkbook) wb;
htmlExcel = PoiExcelToHtmlUtil.getExcelInfo(xWb, true);
} else if (wb instanceof HSSFWorkbook) {
HSSFWorkbook hWb = (HSSFWorkbook) wb;
htmlExcel = PoiExcelToHtmlUtil.getExcelInfo(hWb, true);
}
inputStream = new ByteArrayInputStream(htmlExcel.getBytes());
outputStream = new FileOutputStream(new File("D://poi-test//excelToHtml//德联易控CE平台对接开发计划V1.1.html"));
int temp = 0;
while((temp = inputStream.read())!=-1){
outputStream.write(temp);
}
outputStream.flush();
System.out.println(htmlExcel);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
outputStream.close();
inputStream.close();
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}*/
/**
* 程序入口方法
*
* @param filePath
* 文件的路径
* @param isWithStyle
* 是否需要表格样式 包含 字体 颜色 边框 对齐方式
* @return
* <table>
* ...
* </table>
* 字符串
*/
public String readExcelToHtml(String filePath, boolean isWithStyle) {
InputStream is = null;
String htmlExcel = null;
try {
File sourcefile = new File(filePath);
is = new FileInputStream(sourcefile);
Workbook wb = WorkbookFactory.create(is);
if (wb instanceof XSSFWorkbook) {
XSSFWorkbook xWb = (XSSFWorkbook) wb;
htmlExcel = PoiExcelToHtmlUtil.getExcelInfo(xWb, isWithStyle);
} else if (wb instanceof HSSFWorkbook) {
HSSFWorkbook hWb = (HSSFWorkbook) wb;
htmlExcel = PoiExcelToHtmlUtil.getExcelInfo(hWb, isWithStyle);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return htmlExcel;
}
public static String getExcelInfo(Workbook wb, boolean isWithStyle) {
StringBuffer sb = new StringBuffer();
Sheet sheet = wb.getSheetAt(0);// 获取第一个Sheet的内容
int lastRowNum = sheet.getLastRowNum();
Map<String, String> map[] = getRowSpanColSpanMap(sheet);
sb.append("<table style='border-collapse:collapse;' width='100%'>");
Row row = null; // 兼容
Cell cell = null; // 兼容
for (int rowNum = sheet.getFirstRowNum(); rowNum <= lastRowNum; rowNum++) {
row = sheet.getRow(rowNum);
if (row == null) {
sb.append("<tr><td > </td></tr>");
continue;
}
sb.append("<tr>");
int lastColNum = row.getLastCellNum();
for (int colNum = 0; colNum < lastColNum; colNum++) {
cell = row.getCell(colNum);
if (cell == null) { // 特殊情况 空白的单元格会返回null
sb.append("<td> </td>");
continue;
}
String stringValue = getCellValue(cell);
if (map[0].containsKey(rowNum + "," + colNum)) {
String pointString = map[0].get(rowNum + "," + colNum);
map[0].remove(rowNum + "," + colNum);
int bottomeRow = Integer.valueOf(pointString.split(",")[0]);
int bottomeCol = Integer.valueOf(pointString.split(",")[1]);
int rowSpan = bottomeRow - rowNum + 1;
int colSpan = bottomeCol - colNum + 1;
sb.append("<td rowspan= '" + rowSpan + "' colspan= '" + colSpan + "' ");
} else if (map[1].containsKey(rowNum + "," + colNum)) {
map[1].remove(rowNum + "," + colNum);
continue;
} else {
sb.append("<td ");
}