1.导入Maven依赖
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>4.1.2</version>
</dependency>
2.代码
package com.wzt.test1;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.file.Paths;
public class ExcelFolderCreator {
public static void main(String[] args) {
String excelFilePath = "G:\\coding\\Java\\Java\\src\\main\\resources\\副本2019级.xlsx";
String directoryPath = "C:\\Users\\86136\\Desktop\\19级一次测试";
try (FileInputStream fis = new FileInputStream(new File(excelFilePath))) {
Workbook workbook = new XSSFWorkbook(fis);
Sheet sheet = workbook.getSheetAt(0); // Assuming data is in the first sheet
for (Row row : sheet) {
if (row.getRowNum() == 0) {
// Assuming the first row is the header
continue;
}
String studentId = getCellValueAsString(row.getCell(0)); // Assuming the student ID is in the first column
String studentName = getCellValueAsString(row.getCell(1)); // Assuming the student name is in the second column
String folderName = studentId + "-" + studentName;
File directory = Paths.get(directoryPath, folderName).toFile();
if (!directory.exists()) {
directory.mkdir();
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
private static String getCellValueAsString(Cell cell) {
if (cell == null) return "";
switch (cell.getCellType()) {
case STRING:
return cell.getStringCellValue();
case NUMERIC:
// Convert numeric value to string
return String.valueOf((int) cell.getNumericCellValue());
default:
return "";
}
}}
3.将xlsx文件放到resource路径下
4.效果
5致谢
感谢gpt4.0的支持,让我把好几天的活,半天干完!!!