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)文章
Java 快速排序(QuickSort)原理及實(shí)現(xiàn)代碼
這篇文章主要介紹了Java 快速排序(QuickSort)原理及實(shí)現(xiàn)代碼,有需要的朋友可以參考一下2014-01-01
基于Spring Web Jackson對(duì)RequestBody反序列化失敗的解決
這篇文章主要介紹了基于Spring Web Jackson對(duì)RequestBody反序列化失敗的解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-09-09
MybatisPlus使用Mybatis的XML的動(dòng)態(tài)SQL的功能實(shí)現(xiàn)多表查詢
本文主要介紹了MybatisPlus使用Mybatis的XML的動(dòng)態(tài)SQL的功能實(shí)現(xiàn)多表查詢,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-11-11
詳解Spring Boot實(shí)戰(zhàn)之Rest接口開(kāi)發(fā)及數(shù)據(jù)庫(kù)基本操作
本篇文章主要介紹了Spring Boot實(shí)戰(zhàn)之Rest接口開(kāi)發(fā)及數(shù)據(jù)庫(kù)基本操作,具有一定的參考價(jià)值,有興趣的可以了解一下2017-07-07
Guava事件總線應(yīng)用場(chǎng)景最佳實(shí)踐
這篇文章主要為大家介紹了Guava事件總線應(yīng)用場(chǎng)景最佳實(shí)踐,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-12-12

