import lombok.extern.log4j.Log4j2;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
/**
* @author 耀耀
* @Description 根据Excel表格的姓名列对照片进行排序
* 第一次做的时候因为不知道Windows会把照片按照首字母重新排序,导致自我怀疑,
* 然后发现问题后把移动后的文件改为“序号_文件名”解决。
* @date 2024年04月02日 22:13
*/
@Log4j2
public class test3 {
public static void main(String[] args) {
try (FileInputStream file = new FileInputStream(new File("D:/student/XX中学报名信息表九四.xls"))) {
Workbook workbook = new HSSFWorkbook(file);
Sheet sheet = workbook.getSheetAt(1);//取第2张表
int count = 1;//因为第一行是表头,所以从第二行开始
for (int i = 1; i <= sheet.getLastRowNum(); i++) {
Row row = sheet.getRow(i);
Cell nameCell = row.getCell(2); //姓名列在第三行,这个根据需要修改
if (nameCell != null) {
String name = nameCell.getStringCellValue();
String photoFileName = count + "_" + name + ".jpg";
File photoFile = new File("D:/student/九四照片/九四照片/有档案/" + name + ".jpg");
if (photoFile.exists()) {
Path dest = Paths.get("D:/student/照片", photoFileName);//第一个参数表示要移动到的文件地址
Files.move(photoFile.toPath(), dest);
}
count++;
}
}
workbook.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
根据Excel表格的姓名列对照片进行排序
于 2024-04-03 16:19:22 首次发布