import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
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;
/**
* @param filePath excel源文件文件的路径
* @param htmlPositon 生成的html文件的路径
* @param isWithStyle 是否需要表格样式 包含 字体 颜色 边框 对齐方式
*/
public static String readExcelToHtml(String filePath ,String htmlPositon, 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) { //03版excel处理方法
XSSFWorkbook xWb = (XSSFWorkbook) wb;
htmlExcel = FileConverter.getExcelInfo(xWb,isWithStyle);
}else if(wb instanceof HSSFWorkbook){ //07及10版以后的excel处理方法
HSSFWorkbook hWb = (HSSFWorkbook) wb;
htmlExcel = FileConverter.getExcelInfo(hWb,isWithStyle);
}
writeFile(htmlExcel,htmlPositon);
} catch (Exception e) {
e.printStackTrace();
}finally{
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return htmlPositon;
}
private static String getExcelInfo(Workbook wb,boolean isWithStyle){
StringBuffer sb = new StringBuffer();
Sheet sheet = wb.getSheetAt(0);//获取第一个Sheet的内容
int lastRowNum = sheet.getLastRowNum();
Map map[] = getRowSpanColSpanMap(sheet);
sb.append("
Row row = null; //兼容
Cell cell = null; //兼容
for (int rowNum = sheet.getFirstRowNum(); rowNum <= lastRowNum; rowNum++) {
row = sheet.getRow(rowNum);
if (row == null) {
sb.append("
");continue;
}
sb.append("
");int lastColNum = row.getLastCellNum();
for (int colNum = 0; colNum < lastColNum; colNum++) {
cell = row.getCell(colNum);