JavaWeb使用POI操作Excel文件實(shí)例
1.為項(xiàng)目添加POI
POI官網(wǎng)鏈接

點(diǎn)進(jìn)去之后下載(上邊的是編譯好的類(lèi),下邊的是源代碼)

解壓文件夾,把下面三個(gè)文件復(fù)制到WebComtent>WEB-INF>lib文件夾下

再把這三個(gè)文件復(fù)制到Tomcat的lib文件夾下,否則Tomcat會(huì)因?yàn)檎也坏筋?lèi)而報(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)目),沒(méi)關(guān)系,一會(huì)運(yùn)行的過(guò)程中會(huì)報(bào)錯(cuò),根據(jù)錯(cuò)誤信息再去找到相關(guān)的jar文件BuildPath進(jìn)去就好,注意還要再Tomcat>lib下放置一份副本。
2.讀取Excel文件
官方教程:鏈接
類(lèi)庫(kù):鏈接
直接看代碼吧,不難懂。

//遍歷一個(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ì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
Java 中的 getDeclaredMethods() 方法(使用與原理)
文章介紹了Java反射機(jī)制中的`getDeclaredMethods()`方法,詳細(xì)講解了其使用方法、原理、注意事項(xiàng)以及實(shí)際應(yīng)用場(chǎng)景,幫助讀者更好地理解和應(yīng)用這一強(qiáng)大的工具,感興趣的朋友一起看看吧2024-12-12
Java報(bào)錯(cuò)Java.text.ParseException的解決方法匯總
在Java開(kāi)發(fā)的復(fù)雜世界中,錯(cuò)誤處理是開(kāi)發(fā)者必須面對(duì)的關(guān)鍵挑戰(zhàn)之一,其中,Java.text.ParseException就像一個(gè)隱藏在代碼叢林中的陷阱,常常讓開(kāi)發(fā)者們陷入困惑,本文給大家介紹了Java報(bào)錯(cuò)Java.text.ParseException的解決方法,需要的朋友可以參考下2024-10-10
Spring Boot 緩存 Cache 入門(mén)詳解
本文主要介紹SpringBoot緩存的入門(mén)知識(shí),包括緩存的必要性、常見(jiàn)的緩存策略、SpringCache的注解使用、SpringBoot與緩存的集成以及Ehcache和Redis的示例,同時(shí),還提供了緩存面試問(wèn)題的思路和答案,幫助讀者更好地理解和掌握SpringBoot緩存的相關(guān)內(nèi)容,感興趣的朋友一起看看吧2025-03-03
簡(jiǎn)單實(shí)現(xiàn)java數(shù)獨(dú)游戲
這篇文章主要教大家如何簡(jiǎn)單實(shí)現(xiàn)java數(shù)獨(dú)游戲,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-12-12
Spring-全面詳解(學(xué)習(xí)總結(jié))
這篇文章主要介紹了詳解Spring框架入門(mén),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧,希望能給你帶來(lái)幫助2021-07-07

