JAVA如何讀取Excel數(shù)據(jù)
1.創(chuàng)建Maven項(xiàng)目在pom文件中添加依賴(lài)
<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類(lèi)型的數(shù)據(jù),因此需要把它轉(zhuǎn)化為純String類(lèi)型,這樣就不會(huì)報(bào)錯(cuò)了。報(bào)錯(cuò)的地方類(lèi)似于這樣。
//獲取單元格XSSFCell cell = row.getCell(0);//獲取單元格數(shù)據(jù)String cellValue = cell.getStringCellValue();
在number類(lèi)型轉(zhuǎn)化為String類(lèi)型的過(guò)程中造成了Cannot get a STRING value from a NUMERIC cell這樣的問(wèn)題,因此需要在讀取excel單元格數(shù)據(jù)轉(zhuǎn)化之前設(shè)置單元格類(lèi)型為String,代碼如下。
//獲取單元格XSSFCell cell = row.getCell(0);//設(shè)置單元格類(lèi)型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$Factoryat 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)注好吧啦網(wǎng)其它相關(guān)文章!
相關(guān)文章:
1. 微博金粉是什么意思 微博鐵粉等級(jí)要滿(mǎn)足什么條件2. 百度地圖搜索雨中城的彩蛋是什么?搜索雨中城的彩蛋介紹3. 釘釘群視頻會(huì)議怎么弄4. 天貓投影儀怎么調(diào)焦距5. 小紅書(shū)怎么發(fā)原圖尺寸6. qq瀏覽器禁止訪問(wèn)的網(wǎng)站怎么打開(kāi) 禁止訪問(wèn)怎么解除取消7. qq音樂(lè)怎么發(fā)布自己的音樂(lè)8. 點(diǎn)淘邀請(qǐng)新人可以賺多少錢(qián) 點(diǎn)淘邀請(qǐng)一個(gè)新人多少錢(qián)9. qq錢(qián)包注銷(xiāo)后qq賬號(hào)會(huì)不會(huì)消失10. 小紅書(shū)怎么刪除自己給別人的評(píng)論