一个小需求:给定一些xls文件还有一个带有学生名称的xls单个文件,根据后者xls中提供的学生名称和学生所在班级去前者一堆xls文件中找到相关信息,并将前者一堆xls文件中的信息补充到后者哪个单一的x

<dependency>
            <groupId>org.apache.poigroupId>
            <artifactId>poiartifactId>
            <version>3.17version> 
        dependency>

        
        <dependency>
            <groupId>org.apache.poigroupId>
            <artifactId>poi-ooxmlartifactId>
            <version>5.1.0version> 
        dependency>
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;


public class MergeStudentData {

    public static void main(String[] args) {
        try {
            FileInputStream studentFile = new FileInputStream(new File("C:\\Users\\86150\\Desktop\\18班学生(1)(1).xls"));
            HSSFWorkbook studentWorkbook = new HSSFWorkbook(studentFile);

            File folder = new File("C:\\Users\\86150\\Desktop\\初二二部学生住宿信息"); // 替换为实际的文件夹路径
            File[] excelFiles = folder.listFiles((dir, name) -> name.endsWith(".xls"));

            if (excelFiles != null) {
                for (File excelFile : excelFiles) {
                    FileInputStream fileInputStream = new FileInputStream(excelFile);
                    HSSFWorkbook workbook = new HSSFWorkbook(fileInputStream);

                    // 假设学生姓名在第二列,班级在第四列
                    int nameColumnIndex = 1; // 从0开始计数
                    int classColumnIndex = 3; // 从0开始计数

                    Sheet studentSheet = studentWorkbook.getSheetAt(0);
                    Sheet dataSheet = workbook.getSheetAt(0);

                    for (int dataRowIndex = 1; dataRowIndex <= dataSheet.getLastRowNum(); dataRowIndex++) {
                        Row dataRow = dataSheet.getRow(dataRowIndex);
                        //System.out.println(dataRow.getCell(0).toString());
                        String rowDataName="";
                        String classDataName="";
                        if (null!=dataRow&&null!=dataRow.getCell(1)&&null!=dataRow.getCell(3)){
                            rowDataName = dataRow.getCell(1).toString();
                            classDataName = dataRow.getCell(3).toString();
                        }
                        if ("黄思恩".equals(rowDataName)){
                            System.out.println(dataRow.getCell(0).toString());
                        }

                        for (int rowIndex = 1; rowIndex <= studentSheet.getLastRowNum(); rowIndex++) {
                            Row studentRow = studentSheet.getRow(rowIndex);

                            String studentName = "";
                            if (null!=studentRow&&null!=studentRow.getCell(1)){
                                studentName = studentRow.getCell(1).toString();
                            }
                            String studentClass = "";
                            if (null!=studentRow&&null!=studentRow.getCell(0)){
                                studentClass = studentRow.getCell(0).toString();
                            }

                            if (studentName.equals(rowDataName)&&studentClass.equals(classDataName)) {
                                    // 复制 dataRow 的其他列数据到 studentRow 中
                                    if (null!=dataRow){
                                        for (int cellIndex = 0; cellIndex < dataRow.getLastCellNum(); cellIndex++) {
                                            Cell dataCell = dataRow.getCell(cellIndex);
                                            if (null!=studentRow){
                                                Cell studentCell = studentRow.createCell(cellIndex);
                                                if (null!=dataCell){
                                                    studentCell.setCellValue(dataCell.toString());
                                                }

                                            }
                                        }



                                }

                                break;
                            }
                        }
                    }

                    fileInputStream.close();
                    workbook.close();
                }
            }

            studentFile.close();

            // 保存合并后的学生信息到新的 Excel 文件
            FileOutputStream outputStream = new FileOutputStream(new File("C:\\Users\\86150\\Desktop\\18班学生21" +
                    "(1)(1).xls"));
            studentWorkbook.write(outputStream);
            studentWorkbook.close();
            outputStream.close();

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

你可能感兴趣的:(java)