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é)科成績所在的單元格,加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é)科成績所在的單元格,加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é)科成績所在的單元格,加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é)科成績所在的單元格,加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)文章
IDEA在SpringBoot項(xiàng)目使用Maven打包后jar包太小問題及解決
這篇文章主要介紹了IDEA在SpringBoot項(xiàng)目使用Maven打包后jar包太小問題及解決,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-04-04
java中的Arrays這個(gè)工具類你真的會(huì)用嗎(一文秒懂)
這篇文章主要介紹了java中的Arrays這個(gè)工具類你真的會(huì)用嗎,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-06-06
java實(shí)現(xiàn)的計(jì)算器功能示例【基于swing組件】
這篇文章主要介紹了java實(shí)現(xiàn)的計(jì)算器功能,結(jié)合實(shí)例形式分析了java基于swing組件實(shí)現(xiàn)計(jì)算器功能相關(guān)運(yùn)算操作技巧,需要的朋友可以參考下2017-12-12
Java實(shí)現(xiàn)將Doc/Docx格式的Word文檔轉(zhuǎn)換為PDF文件
這篇文章主要為大家詳細(xì)介紹了如何通過Java將Word文檔轉(zhuǎn)換為PDF、PDF/A和密碼保護(hù)的PDF文件,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解下2025-08-08
java代碼如何實(shí)現(xiàn)存取數(shù)據(jù)庫的blob字段
這篇文章主要介紹了java代碼如何實(shí)現(xiàn)存取數(shù)據(jù)庫的blob字段問題,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2025-04-04
Data Source與數(shù)據(jù)庫連接池簡介(JDBC簡介)
DataSource是作為DriverManager的替代品而推出的,DataSource 對象是獲取連接的首選方法,這篇文章主要介紹了Data Source與數(shù)據(jù)庫連接池簡介(JDBC簡介),需要的朋友可以參考下2022-11-11
Spring觀察者模式之事件發(fā)布訂閱實(shí)現(xiàn)和源碼詳解
這篇文章主要介紹了Spring觀察者模式之事件發(fā)布訂閱實(shí)現(xiàn)和源碼詳解,Spring認(rèn)為發(fā)布訂閱主題,其實(shí)可以理解為事件驅(qū)動(dòng)的編碼,先來實(shí)現(xiàn)以下Spring容器中的事件發(fā)布訂閱,需要的朋友可以參考下2024-01-01

