package com.potevio.telecom.mobilenet; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; import org.apache.poi.xssf.usermodel.XSSFCell; import org.apache.poi.xssf.usermodel.XSSFRow; import org.apache.poi.xssf.usermodel.XSSFSheet; import org.apache.poi.xssf.usermodel.XSSFWorkbook; public class POIParseExcel2007 { /** * POI 解析Excel2007版,生成HTML * @param fileName 文件(含地址) * @return 解析出来的HTML页面String */ public String parseXLS2007ForHtml(String fileName){ StringBuffer content = new StringBuffer(); XSSFWorkbook xwb = null; try{ // 构造 XSSFWorkbook 对象,strPath 传入文件路径 xwb = new XSSFWorkbook(fileName); content.append("<html><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"><title>Parse Excel With POI</title></head><body>"); // 循环工作表Sheet for (int numSheet = 0; numSheet < xwb.getNumberOfSheets(); numSheet++) { XSSFSheet xSheet = xwb.getSheetAt(numSheet); if (xSheet == null) { continue; } content.append("<table border=1 cellspacing=0 cellpadding=1>"); // 循环行Row for (int rowNum = 0; rowNum <= xSheet.getLastRowNum(); rowNum++) { XSSFRow xRow = xSheet.getRow(rowNum); if (xRow == null) { continue; } content.append("<tr align='middle'>"); // 循环列Cell for (int cellNum = 0; cellNum <= xRow.getLastCellNum(); cellNum++) { XSSFCell xCell = xRow.getCell(cellNum); if (xCell == null || "".equals(xCell)) { content.append("<td>").append(" ").append("</td>"); }else if (xCell.getCellType() == XSSFCell.CELL_TYPE_BOOLEAN) { content.append("<td>").append(" ").append(xCell.getBooleanCellValue()).append("</td>"); } else if (xCell.getCellType() == XSSFCell.CELL_TYPE_NUMERIC) { content.append("<td>").append(" ").append(this.doubleToString(xCell.getNumericCellValue())).append("</td>"); } else{ content.append("<td>").append(" ").append(xCell.getStringCellValue()).append("</td>"); } } content.append("</tr>"); } content.append("</table>"); } content.append("</body></html>"); }catch(Exception e){ e.printStackTrace(); System.out.println("POI解析Excel2007错误"); } return content.toString(); } /** * @description 给手机客户端生成EXCEL的查看地址 * @param clientExcelURL 生成的文件地址 * @param file_name_source 需要解析的文件地址 * @return 解析之后产生的文件地址 * */ public String clientExcelUrl(String clientExcelURL,String file_name_source){ OutputStream serverout = null; try { serverout = new FileOutputStream(clientExcelURL); serverout.write(this.parseXLS2007ForHtml(file_name_source).getBytes("UTF-8")); //手机上默认是UTF-8的编码 serverout.flush(); serverout.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }finally{ } return clientExcelURL; } /** * change double variable into string type * @param d * @return */ public String doubleToString(double d){ String str = Double.valueOf(d).toString(); String temp = str; String result = ""; if(str.indexOf("E")>2) result = str.substring(0,1) + temp.substring(2, str.indexOf("E")); else{ if(str.indexOf(".0")>0) result = str.substring(0,str.indexOf(".0")) ; else result = str; } return result; } }
poi的包下载:http://poi.apache.org/download.html
测试用的3.5-final-20090928(共8个包)
贴个测试代码:
package parseExcel2007; import java.io.File; import java.io.FileWriter; import java.io.IOException; public class TestC { public static void main(String[] args) { POIParseExcel2007 parse = new POIParseExcel2007(); FileWriter fw = null; try { String content = ""; String fileName = "F:/excel/测试07表格.xlsx"; content = parse.parseXLS2007ForHtml(fileName); String target = "F:/test_0.html"; File file = new File(target); fw = new FileWriter(file); fw.write(content); fw.flush(); } catch (Exception e) { e.printStackTrace(); } finally { if (fw != null) try { fw.close(); } catch (IOException e) { e.printStackTrace(); } } } }
补充:
POI需要的8个包截图如下: