最近項目中須要用到Excel解析與生成Excel,但現有系統中沒有進行解析工具封裝因而便封裝一個Excel工具包,支持JXL工具包與POI工具包解析與生成數據,POI支持2003與2007。 java
Excel解析目前網上有JXL與POI兩個類庫,相比較下JXL功能較少點 比較容易上手。POI功能更強大,支持2003與2007 。目前POI 3.8 版本中提供SXSSFWorkbook類支持大數據量操做 可以避免內存溢出(目前工具包中暫未使用SXSSFWorkbook的例子)。 node
工具包中用到第三方包有:
cglib-nodep-2.1_3.jar (用於Excel解析時生成BeanMap對象)
commons-lang3-3.1.jar
jxl.jar(jxl工具包)
如下都是POI解析用到的包,若是隻是使用2003能夠導入少點包
dom4j-1.6.1.jar
org.apache.poi-osgi-3.8.jar
poi-ooxml-3.8-beta5.jar
poi-ooxml-schemas-3.8-beta5.jar
xmlbeans-2.5.0.jar apache
工具包生成與解析Excel主要依賴於反射與註解,包中申明一個ExcelAnnotation,該註解註釋與須要導出字段上,用於申明標題頭。生成Excel時根據判斷對象是否使用該註解,而後生成Excel。解析時也根據註解進行解析。 app
使用註解 註解與須要生成Excel對象上 dom
public class Excel { @ExcelAnnotation("名稱") public String bookName; @ExcelAnnotation("行數") public int rows; @ExcelAnnotation("建立時間") public Date createTime; @ExcelAnnotation("內容") public Date content; public Date getContent() { return content; } public void setContent(Date content) { this.content = content; } public String getBookName() { return bookName; } public void setBookName(String bookName) { this.bookName = bookName; } public int getRows() { return rows; } public void setRows(int rows) { this.rows = rows; } public Date getCreateTime() { return createTime; } public void setCreateTime(Date createTime) { this.createTime = createTime; } @Override public String toString() { StringBuffer buffer = new StringBuffer(); buffer.append("{[bookName : " + (null == bookName ? "" : bookName) + "]"); buffer.append("[rows : " + rows + "]"); buffer.append("[createTime : " + (null == createTime ? "" : DateTools.format(createTime)) + "]}"); return buffer.toString(); } }JXL生成與解析例子
public class JxlExcelTest { /** * 建立Excel */ public static void testCreateWorkbook() { try { OutputStream os = new FileOutputStream("e://excel//jxlExcel.xls"); List<Object> allExcel = new ArrayList<Object>(10); // 建立對象存放數據 for(int i = 0 ; i<10000; i ++){ Excel excel4 = new Excel(); excel4.setBookName("excel"+i); excel4.setCreateTime(new Date()); excel4.setRows(i); allExcel.add(excel4); } WritableWorkbook book = JxlUtil.createWorkbook("excel23", Excel.class, allExcel, os); book.write(); book.close(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (RowsExceededException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (WriteException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalArgumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalAccessException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (InstantiationException e) { // TODO Auto-generated catch block e.printStackTrace(); } } /** * 解析Excel */ public static void testAnalyticJxlExcel() { try { InputStream input = new FileInputStream("e://excel//jxlExcel.xls"); Workbook workBook = Workbook.getWorkbook(input); List<Object> list = JxlAnalyticalData.AnalyticJxlExcel(workBook, Excel.class); for(Object object : list){ System.out.println((Excel)object); } } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (BiffException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalArgumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalAccessException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (InstantiationException e) { // TODO Auto-generated catch block e.printStackTrace(); } } /** * @param args */ public static void main(String[] args) { long a = System.currentTimeMillis(); // TODO Auto-generated method stub //建立Excle testCreateWorkbook(); // //解析Excel // testAnalyticJxlExcel(); System.out.println("執行耗時 : " + (System.currentTimeMillis() - a) / 1000f + " 秒 "); } }POI解析與生成例子 包括2003 與 2007
public class PoiExcelTest { /** * 生成excel * * @throws BiffException * @throws IOException * @throws IllegalArgumentException * @throws IllegalAccessException * @throws InstantiationException */ public static void createWorkbook() throws BiffException, IOException, IllegalArgumentException, IllegalAccessException, InstantiationException { // 建立2003 OutputStream os = new FileOutputStream("e://excel//poiExcel2003.xls"); // 建立2007 // OutputStream os = new FileOutputStream("e://excel//poiExcel2007.xlsx"); List<Object> allExcel = new ArrayList<Object>(10000); // 建立對象存放數據 for(int i = 0 ; i<10000; i ++){ Excel excel4 = new Excel(); excel4.setBookName("excel"+i); excel4.setCreateTime(new Date()); excel4.setRows(i); allExcel.add(excel4); } // 建立2003 HSSFWorkbook sr = PoiUtil.createWorkbook("pooi", Excel.class, allExcel); // 建立2007 // XSSFWorkbook sr = PoiUtil.createXSSFWorkbook("pooi", Excel.class, // allExcel); sr.write(os); os.flush(); } /** * 解析Excel * * @throws IllegalArgumentException * @throws IllegalAccessException * @throws InstantiationException */ public static void testAnalyticPoiExcel() throws IllegalArgumentException, IllegalAccessException, InstantiationException { try { //2003 InputStream input = new FileInputStream("e://excel//poiExcel2003.xls"); HSSFWorkbook book = new HSSFWorkbook(input); //2007 // InputStream input = new FileInputStream("e://excel//poiExcel2007.xlsx"); // XSSFWorkbook book = new XSSFWorkbook(input); List<Object> poiList = PoiAnalyticalData.AnalyticPoiExcel(book, Excel.class); for (Object object : poiList) { System.out.println((Excel) object); } } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub try { long a = System.currentTimeMillis(); createWorkbook(); // testAnalyticPoiExcel(); System.out.println("執行耗時 : " + (System.currentTimeMillis() - a) / 1000f + " 秒 "); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
代碼下載地址 ide