最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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)文章

最新評(píng)論

堆龙德庆县| 迭部县| 曲阜市| 丁青县| 潍坊市| 班戈县| 漳浦县| 广河县| 广丰县| 青海省| 汽车| 宁海县| 芦山县| 波密县| 都安| 哈尔滨市| 宣武区| 乌什县| 杭锦旗| 浑源县| 宁陕县| 天水市| 兴山县| 淳安县| 乐东| 宜宾市| 平舆县| 淮北市| 宁德市| 历史| 镇巴县| 营山县| 图们市| 穆棱市| 外汇| 崇阳县| 海门市| 高碑店市| 景洪市| 临颍县| 仲巴县|