1.為項目添加POI
POI官網鏈接
點進去之后下載(上邊的是編譯好的類,下邊的是源代碼)
解壓文件夾,把下面三個文件復制到WebComtent>WEB-INF>lib文件夾下
再把這三個文件復制到Tomcat的lib文件夾下,否則Tomcat會因為找不到類而報錯(這個地方郁悶了一上午)
讀取“.xls”格式使用 import org.apache.poi.hssf.usermodel.*;包的內容,例如:HSSFWorkbook
讀取“.xlsx”格式使用 import org.apache.poi.xssf.usermodel.*; 包的內容,例如:XSSFWorkbook
讀取兩種格式使用 import org.apache.poi.ss.usermodel.* 包的內容,例如:Workbook
由于我是讀取xslx文件所以使用以上幾個jar文件。
注意:
上圖中的兩個文件夾中也有我們需要的jar文件,具體是哪幾個忘記了(當然為了保險也可以把所有的都放進WebContent>WEN-INF>lib下再BuildPath進項目),沒關系,一會運行的過程中會報錯,根據錯誤信息再去找到相關的jar文件BuildPath進去就好,注意還要再Tomcat>lib下放置一份副本。
2.讀取Excel文件
官方教程:鏈接
類庫:鏈接
直接看代碼吧,不難懂。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
|
//遍歷一個Excel文件<br>private void getExcelData(File file) { System.out.println( "now in getExcelData" ); System.out.println( "get file name:" +file.getName().toString()); XSSFWorkbook workBook= null ; try { workBook = new XSSFWorkbook(file); int sheetCount = workBook.getNumberOfSheets(); //Sheet的數量 System.out.println( "num of sheet is : " +sheetCount); //遍歷每個sheet for ( int i= 0 ;i<sheetCount;i++) { XSSFSheet sheet = workBook.getSheetAt(i); //獲取總行數 int rowCount = sheet.getPhysicalNumberOfRows(); System.out.println( "num of row : " + rowCount); System.out.println( "i now in sheet : " + i); //遍歷每一行 for ( int r = 0 ; r < rowCount; r++) { XSSFRow row = sheet.getRow(r); //獲取總列數 int cellCount = row.getPhysicalNumberOfCells(); //遍歷每一列 for ( int c = 0 ; c < cellCount; c++) { XSSFCell cell = row.getCell(c); String cellValue = null ; switch (cell.getCellTypeEnum()) { case STRING: //System.out.println("celltype is string"); cellValue = cell.getStringCellValue(); break ; case NUMERIC: //System.out.println("celltype is Number");//整數,小數,日期 cellValue = String.valueOf(cell.getNumericCellValue()); break ; case BOOLEAN: //System.out.println("celltype is Boolean"); cellValue = String.valueOf(cell.getBooleanCellValue()); break ; case FORMULA: //System.out.println("celltype is Formula");//公式 cellValue = "錯誤,不能為公式" ; break ; case BLANK: //System.out.println("celltype is Blank");//空白 cellValue = cell.getStringCellValue(); break ; case ERROR: //System.out.println("celltype is Error"); cellValue = "錯誤" ; break ; default : //System.out.println("celltype : default"); cellValue = "錯誤" ; break ; } System.out.println(cellValue.toString()); } } } } catch (IOException e) { System.out.println( "File Error IOException : " +e.getMessage()); } catch (Exception e) { // TODO: handle exception } finally { try { workBook.close(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); System.out.println( "workBook.close()&fileInputStream.close() Error : " +e.getMessage()); } System.out.println( "Try Catch : finally" ); } System.out.println( "hi feipeng8848 getExcelData is done" ); } |
以上所述是小編給大家介紹的JavaWeb使用POI操作Excel文件實例,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對服務器之家網站的支持!
原文鏈接:http://www.cnblogs.com/feipeng8848/archive/2017/04/28/6781822.html