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

Java遍歷讀取Excel固定的單元格實現(xiàn)方式

 更新時間:2025年09月11日 09:41:31   作者:小碼農(nóng)的  
使用ApachePOI讀取Excel需先添加依賴,再編寫核心代碼處理數(shù)據(jù),如圖所示,總結個人經(jīng)驗,供參考并支持腳本之家

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; // 總學科數(shù)量
        int startRow = 4; // 數(shù)據(jù)起始行(假設從第4行開始)

        // 縱向遍歷每個學生
        for (int rowIndex = startRow; rowIndex <= sheet.getLastRowNum(); rowIndex++) {
            //存放數(shù)據(jù)的實體類
            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);

            // 遍歷橫向的內容
            for (int itemIndex = 0; itemIndex < totalSubjects; itemIndex++) {
                //獲取第一行 橫向的內容
                Row itemRow = sheet.getRow(0);
                //獲取第一行 橫向的第5列內容
                Cell itemCell = itemRow.getCell(itemIndex + 5); // 學科成績所在的單元格,加2是因為ID和姓名在前兩列
                String item = new DataFormatter().formatCellValue(itemCell);
                System.out.println("細則ID:" + item);
                scoreDetail.setAssId(Integer.parseInt(item));

                //獲取第二行 橫向的內容
                Row catalogRow = sheet.getRow(1);
                Cell catalogCell = catalogRow.getCell(itemIndex + 5); // 學科成績所在的單元格,加2是因為ID和姓名在前兩列
                String catalog = new DataFormatter().formatCellValue(catalogCell);
                System.out.println("細則目錄ID :" + catalog);
                scoreDetail.setCatalogId(Integer.parseInt(catalog));


                //獲取第三行 橫向的內容
                Row scoreRow = sheet.getRow(2);
                Cell scoreCell = scoreRow.getCell(itemIndex + 5); // 學科成績所在的單元格,加2是因為ID和姓名在前兩列
                String score = new DataFormatter().formatCellValue(scoreCell);
                System.out.println("細則分值:" + score);
                scoreDetail.setRuleScore(Integer.parseInt(score));
                
                 //跟隨縱向遍歷的順序 獲取縱向行上所在列的內容
                Row gradeRow = sheet.getRow(rowIndex);
                Cell gradeCell = gradeRow.getCell(itemIndex + 5); // 學科成績所在的單元格,加2是因為ID和姓名在前兩列
                String grade = new DataFormatter().formatCellValue(gradeCell);
                System.out.println("打分分值:" + grade);
                scoreDetail.setScoreValue(Float.valueOf(grade));
            }

        }
        return success(true);
    }
  • Excel的表數(shù)據(jù)如圖:

總結

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關文章

最新評論

阿拉尔市| 龙游县| 凤山县| 武胜县| 微山县| 敦煌市| 孟州市| 泾源县| 岳西县| 嘉黎县| 延寿县| 和平县| 宝坻区| 盐城市| 无极县| 深水埗区| 榆林市| 南木林县| 灵台县| 开平市| 涪陵区| 博兴县| 江永县| 潮州市| 垫江县| 闵行区| 裕民县| 水富县| 英山县| 吉水县| 翁牛特旗| 玉环县| 聂荣县| 白城市| 巫山县| 广灵县| 庆元县| 宜昌市| 闽侯县| 隆德县| 含山县|