1 packagecom.topcheer.html;2
3 importjava.io.File;4 importjava.io.FileInputStream;5 importjava.io.FileOutputStream;6 importjava.io.IOException;7 importjava.io.InputStream;8 importjava.io.PrintStream;9 importjava.text.DecimalFormat;10 importjava.text.SimpleDateFormat;11 importjava.util.Date;12 importjava.util.HashMap;13 importjava.util.List;14 importjava.util.Map;15
16 importorg.apache.poi.POIXMLDocumentPart;17 importorg.apache.poi.hssf.usermodel.HSSFCellStyle;18 importorg.apache.poi.hssf.usermodel.HSSFClientAnchor;19 importorg.apache.poi.hssf.usermodel.HSSFDataFormat;20 importorg.apache.poi.hssf.usermodel.HSSFDateUtil;21 importorg.apache.poi.hssf.usermodel.HSSFFont;22 importorg.apache.poi.hssf.usermodel.HSSFPalette;23 importorg.apache.poi.hssf.usermodel.HSSFPicture;24 importorg.apache.poi.hssf.usermodel.HSSFPictureData;25 importorg.apache.poi.hssf.usermodel.HSSFShape;26 importorg.apache.poi.hssf.usermodel.HSSFSheet;27 importorg.apache.poi.hssf.usermodel.HSSFWorkbook;28 importorg.apache.poi.hssf.util.HSSFColor;29 importorg.apache.poi.ss.usermodel.Cell;30 importorg.apache.poi.ss.usermodel.CellStyle;31 importorg.apache.poi.ss.usermodel.PictureData;32 importorg.apache.poi.ss.usermodel.Row;33 importorg.apache.poi.ss.usermodel.Sheet;34 importorg.apache.poi.ss.usermodel.Workbook;35 importorg.apache.poi.ss.usermodel.WorkbookFactory;36 importorg.apache.poi.ss.util.CellRangeAddress;37 importorg.apache.poi.xssf.usermodel.XSSFCellStyle;38 importorg.apache.poi.xssf.usermodel.XSSFClientAnchor;39 importorg.apache.poi.xssf.usermodel.XSSFColor;40 importorg.apache.poi.xssf.usermodel.XSSFDrawing;41 importorg.apache.poi.xssf.usermodel.XSSFFont;42 importorg.apache.poi.xssf.usermodel.XSSFPicture;43 importorg.apache.poi.xssf.usermodel.XSSFShape;44 importorg.apache.poi.xssf.usermodel.XSSFSheet;45 importorg.apache.poi.xssf.usermodel.XSSFWorkbook;46 importorg.openxmlformats.schemas.drawingml.x2006.spreadsheetDrawing.CTMarker;47 /**
48 * Excel转html49 *50 */
51 public classExcelToHtml {52
53 private static String UPLOAD_FILE="E:/";54
55
56 /**
57 * 测试58 *59 *@paramargs60 */
61 public static voidexcelToHtml(String path,String htmlPositon) {62 InputStream is = null;63 String htmlExcel = null;64 String[] str = path.split("/");65 String fileName = str[str.length-1];66 try{67 File sourcefile = newFile(path);68 is = newFileInputStream(sourcefile);69 Workbook wb = WorkbookFactory.create(is);//此WorkbookFactory在POI-3.10版本中使用需要添加dom4j
70 if (wb instanceofXSSFWorkbook) {71 XSSFWorkbook xWb =(XSSFWorkbook) wb;72 htmlExcel = ExcelToHtml.getExcelInfo(xWb, true);73 } else if (wb instanceofHSSFWorkbook) {74 HSSFWorkbook hWb =(HSSFWorkbook) wb;75 htmlExcel = ExcelToHtml.getExcelInfo(hWb, true);76 }77 writeFile(htmlExcel,htmlPositon,fileName);78 } catch(Exception e) {79 e.printStackTrace();80 } finally{81 try{82 is.close();83 } catch(IOException e) {84 e.printStackTrace();85 }86 }87 }88
89 @SuppressWarnings("resource")90 private static voidwriteFile(String content,String htmlPath, String fileName){91 File file2 = newFile(htmlPath);92 StringBuilder sb = newStringBuilder();93 try{94 file2.createNewFile();//创建文件
95
96 sb.append("
"+fileName+"");97 sb.append("102 PrintStream printStream = new PrintStream(newFileOutputStream(file2));103
104 printStream.println(sb.toString());//将字符串写入文件
105
106 } catch(IOException e) {107
108 e.printStackTrace();109 }110
111 }112
113
114 /**
115 * 程序入口方法116 *117 *@paramfilePath118 * 文件的路径119 *@paramisWithStyle120 * 是否需要表格样式 包含 字体 颜色 边框 对齐方式121 *@return
122 *
125 * 字符串126 */127 public String readExcelToHtml(String filePath, booleanisWithStyle) {128
129 InputStream is = null;130 String htmlExcel = null;131 try{132 File sourcefile = newFile(filePath);133 is = newFileInputStream(sourcefile);134 Workbook wb =WorkbookFactory.create(is);135 if (wb instanceofXSSFWorkbook) {136 XSSFWorkbook xWb =(XSSFWorkbook) wb;137 htmlExcel =ExcelToHtml.getExcelInfo(xWb, isWithStyle);138 } else if (wb instanceofHSSFWorkbook) {139 HSSFWorkb