JavaWeb使用POI操作Excel文件實(shí)例
1.為項(xiàng)目添加POI
POI官網(wǎng)鏈接
點(diǎn)進(jìn)去之后下載(上邊的是編譯好的類,下邊的是源代碼)
解壓文件夾,把下面三個(gè)文件復(fù)制到WebComtent>WEB-INF>lib文件夾下
再把這三個(gè)文件復(fù)制到Tomcat的lib文件夾下,否則Tomcat會(huì)因?yàn)檎也坏筋惗鴪?bào)錯(cuò)(這個(gè)地方郁悶了一上午)
讀取“.xls”格式使用 import org.apache.poi.hssf.usermodel.*;包的內(nèi)容,例如:HSSFWorkbook
讀取“.xlsx”格式使用 import org.apache.poi.xssf.usermodel.*; 包的內(nèi)容,例如:XSSFWorkbook
讀取兩種格式使用 import org.apache.poi.ss.usermodel.* 包的內(nèi)容,例如:Workbook
由于我是讀取xslx文件所以使用以上幾個(gè)jar文件。
注意:
上圖中的兩個(gè)文件夾中也有我們需要的jar文件,具體是哪幾個(gè)忘記了(當(dāng)然為了保險(xiǎn)也可以把所有的都放進(jìn)WebContent>WEN-INF>lib下再BuildPath進(jìn)項(xiàng)目),沒關(guān)系,一會(huì)運(yùn)行的過程中會(huì)報(bào)錯(cuò),根據(jù)錯(cuò)誤信息再去找到相關(guān)的jar文件BuildPath進(jìn)去就好,注意還要再Tomcat>lib下放置一份副本。
2.讀取Excel文件
官方教程:鏈接
類庫:鏈接
直接看代碼吧,不難懂。
//遍歷一個(gè)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的數(shù)量 System.out.println("num of sheet is : "+sheetCount); //遍歷每個(gè)sheet for(int i=0;i<sheetCount;i++) { XSSFSheet sheet = workBook.getSheetAt(i); //獲取總行數(shù) 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); //獲取總列數(shù) 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");//整數(shù),小數(shù),日期 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 = "錯(cuò)誤,不能為公式"; break; case BLANK: //System.out.println("celltype is Blank");//空白 cellValue = cell.getStringCellValue(); break; case ERROR: //System.out.println("celltype is Error"); cellValue = "錯(cuò)誤"; break; default: //System.out.println("celltype : default"); cellValue = "錯(cuò)誤"; 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文件實(shí)例,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
應(yīng)用啟動(dòng)數(shù)據(jù)初始化接口CommandLineRunner和Application詳解
這篇文章主要介紹了應(yīng)用啟動(dòng)數(shù)據(jù)初始化接口CommandLineRunner和Application詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-12-12Java多線程實(shí)現(xiàn)異步調(diào)用的方法
本文給大家分享java多線程實(shí)現(xiàn)異步調(diào)用的方法,感興趣的朋友跟著腳本之家小編一起學(xué)習(xí)吧2015-09-09SpringBoot集成FastDFS依賴實(shí)現(xiàn)文件上傳的示例
這篇文章主要介紹了SpringBoot集成FastDFS依賴實(shí)現(xiàn)文件上傳,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-05-05