Java遍歷讀取Excel固定的單元格實(shí)現(xiàn)方式
更新時(shí)間:2025年09月11日 09:41:31 作者:小碼農(nóng)的
使用ApachePOI讀取Excel需先添加依賴,再編寫核心代碼處理數(shù)據(jù),如圖所示,總結(jié)個(gè)人經(jīng)驗(yàn),供參考并支持腳本之家
Java遍歷讀取Excel固定的單元格
1、使用Apache POI讀取Excel
首先引入依賴
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
</dependency>2、核心代碼
@Transactional
@PreAuthorize("@ss.hasPermi('system:user:import')")
@PostMapping("/importChange")
public AjaxResult importChange(MultipartFile file) throws Exception
{
//獲取表格
Workbook sheets = WorkbookFactory.create(file.getInputStream());
Sheet sheet = sheets.getSheetAt(0);
int totalSubjects = 12; // 總學(xué)科數(shù)量
int startRow = 4; // 數(shù)據(jù)起始行(假設(shè)從第4行開始)
// 縱向遍歷每個(gè)學(xué)生
for (int rowIndex = startRow; rowIndex <= sheet.getLastRowNum(); rowIndex++) {
//存放數(shù)據(jù)的實(shí)體類
BasAssessScoreDetail scoreDetail = new BasAssessScoreDetail();
//從第4行開始讀取
Row row = sheet.getRow(rowIndex);
Cell cellName = row.getCell(4);//獲取第4行第4列的人員
Cell cellId = row.getCell(3);//獲取第4行第3列的人員id
String staffName = cellName.getStringCellValue();
System.out.println("人員姓名:" + staffName);
String staffId = cellId.getStringCellValue();
System.out.println("人員id:" + staffId);
// 遍歷橫向的內(nèi)容
for (int itemIndex = 0; itemIndex < totalSubjects; itemIndex++) {
//獲取第一行 橫向的內(nèi)容
Row itemRow = sheet.getRow(0);
//獲取第一行 橫向的第5列內(nèi)容
Cell itemCell = itemRow.getCell(itemIndex + 5); // 學(xué)科成績(jī)所在的單元格,加2是因?yàn)镮D和姓名在前兩列
String item = new DataFormatter().formatCellValue(itemCell);
System.out.println("細(xì)則ID:" + item);
scoreDetail.setAssId(Integer.parseInt(item));
//獲取第二行 橫向的內(nèi)容
Row catalogRow = sheet.getRow(1);
Cell catalogCell = catalogRow.getCell(itemIndex + 5); // 學(xué)科成績(jī)所在的單元格,加2是因?yàn)镮D和姓名在前兩列
String catalog = new DataFormatter().formatCellValue(catalogCell);
System.out.println("細(xì)則目錄ID :" + catalog);
scoreDetail.setCatalogId(Integer.parseInt(catalog));
//獲取第三行 橫向的內(nèi)容
Row scoreRow = sheet.getRow(2);
Cell scoreCell = scoreRow.getCell(itemIndex + 5); // 學(xué)科成績(jī)所在的單元格,加2是因?yàn)镮D和姓名在前兩列
String score = new DataFormatter().formatCellValue(scoreCell);
System.out.println("細(xì)則分值:" + score);
scoreDetail.setRuleScore(Integer.parseInt(score));
//跟隨縱向遍歷的順序 獲取縱向行上所在列的內(nèi)容
Row gradeRow = sheet.getRow(rowIndex);
Cell gradeCell = gradeRow.getCell(itemIndex + 5); // 學(xué)科成績(jī)所在的單元格,加2是因?yàn)镮D和姓名在前兩列
String grade = new DataFormatter().formatCellValue(gradeCell);
System.out.println("打分分值:" + grade);
scoreDetail.setScoreValue(Float.valueOf(grade));
}
}
return success(true);
}- Excel的表數(shù)據(jù)如圖:

總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
您可能感興趣的文章:
- 使用Apache POI在Java中實(shí)現(xiàn)Excel單元格的合并
- Java使用FastExcel實(shí)現(xiàn)合并單元格
- java中實(shí)現(xiàn)excel合并單元格詳細(xì)代碼實(shí)例
- Java通過(guò)cellstyle屬性設(shè)置Excel單元格常用樣式的全面總結(jié)講解
- java實(shí)現(xiàn)讀取帶合并單元格的Excel
- Java?EasyExcel實(shí)現(xiàn)合并相同內(nèi)容單元格與動(dòng)態(tài)標(biāo)題功能
- java使用EasyExcel實(shí)現(xiàn)合并單元格
相關(guān)文章
一次mybatis連接查詢遇到的坑實(shí)戰(zhàn)記錄
這篇文章主要給大家介紹了關(guān)于一次mybatis連接查詢遇到的坑的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-12-12
Nexus配置npm私有倉(cāng)庫(kù)全過(guò)程
本文介紹企業(yè)內(nèi)網(wǎng)環(huán)境下使用Nexus搭建私有npm倉(cāng)庫(kù),通過(guò)代理緩存和統(tǒng)一管理提升依賴下載效率與安全性,解決公共倉(cāng)庫(kù)網(wǎng)絡(luò)依賴問(wèn)題,并指導(dǎo)401認(rèn)證配置2025-08-08
java依賴jave-all-deps實(shí)現(xiàn)視頻格式轉(zhuǎn)換
jave-all-deps是一款基于FFmpeg庫(kù)的Java音視頻編解碼庫(kù),本文主要介紹了java依賴jave-all-deps實(shí)現(xiàn)視頻格式轉(zhuǎn)換,具有一定的參考價(jià)值,感興趣的可以了解一下2024-07-07
java 定義長(zhǎng)度為0的數(shù)組/空數(shù)組案例
這篇文章主要介紹了java 定義長(zhǎng)度為0的數(shù)組/空數(shù)組案例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-03-03
Java和Ceylon對(duì)象的構(gòu)造和驗(yàn)證
這篇文章主要為大家詳細(xì)介紹了Java和Ceylon對(duì)象的構(gòu)造和驗(yàn)證,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-11-11

