Files
pachongdelete/src/main/java/com/example/sso/dao/InsertNumber.java
2025-09-25 09:52:41 +08:00

53 lines
1.5 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package com.example.sso.dao;
import org.apache.poi.ss.usermodel.*;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class InsertNumber {
public static String insert() {
// Excel 文件路径
String excelFilePath = "C:\\Users\\李嘉卓\\Desktop\\银建.xlsx";
// Excel 表格中的行号和列号从0开始
int rowIndex = 0; // 第三行
int columnIndex = 12; // 第二列
// 要插入的数据
Integer data = 31;
try {
FileInputStream excelFile = new FileInputStream(excelFilePath);
Workbook workbook = WorkbookFactory.create(excelFile);
Sheet sheet = workbook.getSheetAt(0); // 获取第一个工作表
Row row = sheet.getRow(rowIndex);
if (row == null) {
row = sheet.createRow(rowIndex);
}
Cell cell = row.getCell(columnIndex);
if (cell == null) {
cell = row.createCell(columnIndex);
}
cell.setCellValue(data);
excelFile.close();
// 保存修改后的 Excel 文件
FileOutputStream outFile = new FileOutputStream(excelFilePath);
workbook.write(outFile);
outFile.close();
workbook.close();
System.out.println("Data has been inserted successfully.");
} catch (IOException e) {
e.printStackTrace();
}
return "新增完成";
}
}