JAVA如何讀取Excel數(shù)據(jù)
1.創(chuàng)建Maven項(xiàng)目在pom文件中添加依賴
<dependencies> <!-- 舊的 .xls --> <!--<dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>3.17</version> </dependency>--> <!-- 新的 .xlsx --> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>3.17</version> </dependency> </dependencies>
2.編寫(xiě)代碼
import org.apache.poi.ss.usermodel.*; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import java.io.FileInputStream; import java.io.IOException; public class PoiTest { public static void main(String[] args) throws IOException { FileInputStream is = new FileInputStream("src/main/resources/test.xlsx"); XSSFWorkbook workbook = new XSSFWorkbook(is); //讀取Sheet Sheet sheet = workbook.getSheetAt(0); Row row = sheet.getRow(0); //獲取最大行數(shù) int rownum = sheet.getPhysicalNumberOfRows(); //獲取最大列數(shù) int colnum = row.getPhysicalNumberOfCells(); for (int i = 0; i < rownum; i++) { //獲取第i行數(shù)據(jù) row = sheet.getRow(i); for (int j = 0; j < colnum; j++) { Cell cell = row.getCell(j); cell.setCellType(CellType.STRING); String cellText = cell.getStringCellValue(); System.out.print(cellText + "\t"); } System.out.println(); } } }
3.報(bào)錯(cuò)
3.1 異常解決Cannot get a STRING value from a NUMERIC cell poi
poi導(dǎo)入excel表格數(shù)據(jù)時(shí)報(bào)java.lang.IllegalStateException: Cannot get a STRING value from a NUMERIC cell異常是因?yàn)樵谧x取cell單元格字符串時(shí),有number類型的數(shù)據(jù),因此需要把它轉(zhuǎn)化為純String類型,這樣就不會(huì)報(bào)錯(cuò)了。
報(bào)錯(cuò)的地方類似于這樣。
//獲取單元格 XSSFCell cell = row.getCell(0); //獲取單元格數(shù)據(jù) String cellValue = cell.getStringCellValue();
在number類型轉(zhuǎn)化為String類型的過(guò)程中造成了Cannot get a STRING value from a NUMERIC cell這樣的問(wèn)題,因此需要在讀取excel單元格數(shù)據(jù)轉(zhuǎn)化之前設(shè)置單元格類型為String,代碼如下。
//獲取單元格 XSSFCell cell = row.getCell(0); //設(shè)置單元格類型 cell.setCellType(CellType.STRING); //獲取單元格數(shù)據(jù) String cellValue = cell.getStringCellValue();
3.2 poi導(dǎo)出Excel報(bào)錯(cuò)java.lang.NoClassDefFoundError: org/openxmlformats/schemas/spreadsheetml/x2006/main/CTWorkbook$Factoryat
Exception in thread "main" java.lang.NoClassDefFoundError: org/openxmlformats/schemas/spreadsheetml/x2006/main/CTWorkbook$Factoryat org.apache.poi.xssf.usermodel.XSSFWorkbook.onWorkbookCreate(XSSFWorkbook.java:436)
at org.apache.poi.xssf.usermodel.XSSFWorkbook.<init>(XSSFWorkbook.java:238)
at org.apache.poi.xssf.usermodel.XSSFWorkbook.<init>(XSSFWorkbook.java:229)
at exportexcel.BuildXLSX.main(BuildXLSX.java:35)
Caused by: java.lang.ClassNotFoundException: org.openxmlformats.schemas.spreadsheetml.x2006.main.CTWorkbook$Factory
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 4 more
錯(cuò)誤原因:
未導(dǎo)入poi-ooxml-schema jar包。我使用的是poi-3.15 , 應(yīng)該是poi-ooxml-schemas-3.15.jar包
以上就是JAVA如何讀取Excel數(shù)據(jù)的詳細(xì)內(nèi)容,更多關(guān)于JAVA讀取Excel數(shù)據(jù)的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
SpringBoot 整合Tess4J庫(kù)實(shí)現(xiàn)圖片文字識(shí)別案例詳解
Tess4J是一個(gè)基于Tesseract OCR引擎的Java接口,可以用來(lái)識(shí)別圖像中的文本,說(shuō)白了,就是封裝了它的API,讓Java可以直接調(diào)用,今天給大家分享一個(gè)SpringBoot整合Tess4j庫(kù)實(shí)現(xiàn)圖片文字識(shí)別的小案例2023-10-10springboot獲取微信JSDK簽名信息的實(shí)現(xiàn)示例
本文介紹了如何在Spring Boot應(yīng)用中獲取微信JSDK的簽名信息,包括獲取接口URL、參數(shù)設(shè)置、簽名算法和獲取簽名結(jié)果的步驟,具有一定的參考價(jià)值,感興趣的可以了解一下2023-11-11Java基礎(chǔ)之多線程的三種實(shí)現(xiàn)方式
這篇文章主要介紹了Java基礎(chǔ)之多線程的三種實(shí)現(xiàn)方式,文中有非常詳細(xì)的代碼示例,對(duì)正在學(xué)習(xí)java基礎(chǔ)的小伙伴們有非常好的幫助,需要的朋友可以參考下2021-04-04使用Mybatis-Plus時(shí)的SqlSessionFactory問(wèn)題及處理
這篇文章主要介紹了使用Mybatis-Plus時(shí)的SqlSessionFactory問(wèn)題及處理方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-12-12springboot+Oauth2實(shí)現(xiàn)自定義AuthenticationManager和認(rèn)證path
本篇文章主要介紹了springboot+Oauth2實(shí)現(xiàn)自定義AuthenticationManager和認(rèn)證path,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-09-09