POI解析Excel2007

Poi解析excel2007代码   收藏代码
  1. package com.potevio.telecom.mobilenet;  
  2.   
  3. import java.io.FileNotFoundException;  
  4. import java.io.FileOutputStream;  
  5. import java.io.IOException;  
  6. import java.io.OutputStream;  
  7.   
  8. import org.apache.poi.xssf.usermodel.XSSFCell;  
  9. import org.apache.poi.xssf.usermodel.XSSFRow;  
  10. import org.apache.poi.xssf.usermodel.XSSFSheet;  
  11. import org.apache.poi.xssf.usermodel.XSSFWorkbook;  
  12.   
  13. public class POIParseExcel2007 {  
  14.       
  15.     /**  
  16.      * POI 解析Excel2007版,生成HTML  
  17.      * @param fileName 文件(含地址)  
  18.      * @return 解析出来的HTML页面String  
  19.      */  
  20.     public String parseXLS2007ForHtml(String fileName){  
  21.         StringBuffer content = new StringBuffer();  
  22.         XSSFWorkbook xwb = null;  
  23.         try{  
  24.             // 构造 XSSFWorkbook 对象,strPath 传入文件路径  
  25.             xwb = new XSSFWorkbook(fileName);  
  26.             content.append("<html><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"><title>Parse Excel With POI</title></head><body>");  
  27.             // 循环工作表Sheet  
  28.             for (int numSheet = 0; numSheet < xwb.getNumberOfSheets(); numSheet++) {  
  29.                 XSSFSheet xSheet = xwb.getSheetAt(numSheet);  
  30.                 if (xSheet == null) {  
  31.                     continue;  
  32.                 }  
  33.                 content.append("<table  border=1 cellspacing=0 cellpadding=1>");  
  34.                   
  35.                 // 循环行Row  
  36.                 for (int rowNum = 0; rowNum <= xSheet.getLastRowNum(); rowNum++) {  
  37.                     XSSFRow xRow = xSheet.getRow(rowNum);  
  38.                     if (xRow == null) {  
  39.                         continue;  
  40.                     }  
  41.                     content.append("<tr align='middle'>");  
  42.                     // 循环列Cell  
  43.                     for (int cellNum = 0; cellNum <= xRow.getLastCellNum(); cellNum++) {  
  44.                         XSSFCell xCell = xRow.getCell(cellNum);  
  45.                         if (xCell == null || "".equals(xCell)) {  
  46.                             content.append("<td>").append("&nbsp;").append("</td>");  
  47.                         }else if (xCell.getCellType() == XSSFCell.CELL_TYPE_BOOLEAN) {  
  48.                             content.append("<td>").append("&nbsp;").append(xCell.getBooleanCellValue()).append("</td>");  
  49.                         } else if (xCell.getCellType() == XSSFCell.CELL_TYPE_NUMERIC) {  
  50.                             content.append("<td>").append("&nbsp;").append(this.doubleToString(xCell.getNumericCellValue())).append("</td>");  
  51.                         } else{  
  52.                             content.append("<td>").append("&nbsp;").append(xCell.getStringCellValue()).append("</td>");  
  53.                         }     
  54.                     }  
  55.                     content.append("</tr>");  
  56.                 }  
  57.                 content.append("</table>");  
  58.             }  
  59.             content.append("</body></html>");  
  60.               
  61.         }catch(Exception e){  
  62.             e.printStackTrace();  
  63.             System.out.println("POI解析Excel2007错误");  
  64.         }  
  65.         return content.toString();  
  66.     }  
  67.       
  68.     /**  
  69.      * @description 给手机客户端生成EXCEL的查看地址  
  70.      * @param clientExcelURL  生成的文件地址  
  71.      * @param file_name_source  需要解析的文件地址  
  72.      * @return  解析之后产生的文件地址  
  73.      *   
  74.      */  
  75.     public String clientExcelUrl(String clientExcelURL,String file_name_source){  
  76.         OutputStream serverout = null;  
  77.         try {  
  78.             serverout = new FileOutputStream(clientExcelURL);  
  79.             serverout.write(this.parseXLS2007ForHtml(file_name_source).getBytes("UTF-8"));   //手机上默认是UTF-8的编码  
  80.             serverout.flush();     
  81.             serverout.close();  
  82.         } catch (FileNotFoundException e) {  
  83.             e.printStackTrace();  
  84.         } catch (IOException e) {  
  85.             e.printStackTrace();  
  86.         }finally{  
  87.               
  88.         }  
  89.         return clientExcelURL;  
  90.     }  
  91.       
  92.     /**  
  93.      * change double variable into string type   
  94.      * @param d  
  95.      * @return  
  96.      */  
  97.     public String doubleToString(double d){   
  98.         String str = Double.valueOf(d).toString();  
  99.         String temp = str;  
  100.         String result = "";  
  101.         if(str.indexOf("E")>2)  
  102.             result = str.substring(0,1) + temp.substring(2, str.indexOf("E"));  
  103.         else{  
  104.             if(str.indexOf(".0")>0)  
  105.                 result = str.substring(0,str.indexOf(".0")) ;  
  106.             else  
  107.                 result = str;  
  108.         }  
  109.         return result;  
  110.     }  
  111. }  

 poi的包下载:http://poi.apache.org/download.html

测试用的3.5-final-20090928(共8个包)

贴个测试代码:

测试代码代码   收藏代码
  1. package parseExcel2007;  
  2.   
  3. import java.io.File;  
  4. import java.io.FileWriter;  
  5. import java.io.IOException;  
  6.   
  7. public class TestC {  
  8.       
  9.     public static void main(String[] args) {  
  10.         POIParseExcel2007 parse = new POIParseExcel2007();  
  11.   
  12.         FileWriter fw = null;  
  13.         try {  
  14.             String content = "";  
  15.             String fileName = "F:/excel/测试07表格.xlsx";  
  16.             content = parse.parseXLS2007ForHtml(fileName);  
  17.             String target = "F:/test_0.html";  
  18.             File file = new File(target);  
  19.             fw = new FileWriter(file);  
  20.             fw.write(content);  
  21.             fw.flush();  
  22.         } catch (Exception e) {  
  23.             e.printStackTrace();  
  24.         } finally {  
  25.             if (fw != null)  
  26.                 try {  
  27.                     fw.close();  
  28.                 } catch (IOException e) {  
  29.                     e.printStackTrace();  
  30.                 }  
  31.         }  
  32.     }  
  33. }  

 补充:

     POI需要的8个包截图如下:

    

猜你喜欢

转载自zzc1684.iteye.com/blog/2154455