53 lines
1.5 KiB
Java
53 lines
1.5 KiB
Java
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 "新增完成";
|
||
}
|
||
}
|