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è)參考,也希望大家多多支持腳本之家。
您可能感興趣的文章:
相關(guān)文章
一次mybatis連接查詢遇到的坑實(shí)戰(zhàn)記錄
這篇文章主要給大家介紹了關(guān)于一次mybatis連接查詢遇到的坑的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-12-12
java依賴jave-all-deps實(shí)現(xiàn)視頻格式轉(zhuǎn)換
jave-all-deps是一款基于FFmpeg庫的Java音視頻編解碼庫,本文主要介紹了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ì)大家有所幫助。一起跟隨小編過來看看吧2021-03-03
Java和Ceylon對(duì)象的構(gòu)造和驗(yàn)證
這篇文章主要為大家詳細(xì)介紹了Java和Ceylon對(duì)象的構(gòu)造和驗(yàn)證,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-11-11

