gpt4 book ai didi

java - 通过hssf/xssf向现有excel单元格写入数据。

转载 作者:行者123 更新时间:2023-11-29 05:48:33 26 4
gpt4 key购买 nike

我进退两难了。我一直在尝试通过 Java 中的 hssf/xssf 将数据写入现有的 excel 文件。我没有错误,但是当我运行它时仍然无法对 Excel 工作表进行更改。谁能帮我解决这个问题?我的代码是:

try {
FileInputStream inp = new FileInputStream("C:/Users/Training/Desktop/saurav.xlsx");


Workbook wb = null;
try {
wb = WorkbookFactory.create(inp);
} catch (InvalidFormatException e) {
e.printStackTrace();
}
Sheet sheet = wb.getSheetAt(0);
Row row = sheet.getRow(3);
Cell cell = row.getCell(4);
if (cell == null)
cell = row.createCell(4);
cell.setCellType(Cell.CELL_TYPE_STRING);
cell.setCellValue("a test");

// Write the output to a file
FileOutputStream fileOut = new FileOutputStream("C:/Users/Training/Desktop/saurav.xlsx");
wb.write(fileOut);
fileOut.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

请有人帮我解决这个问题。自从过去 2 天以来一直在尝试这个。

最佳答案

而不是使用它,它在我的系统中工作。

try {
FileInputStream inp = new FileInputStream("C:/Users/Training/Desktop/saurav.xlsx");


Workbook wb = null;
try {
wb = WorkbookFactory.create(inp);
} catch (InvalidFormatException e) {
e.printStackTrace();
}
Sheet sheet = wb.getSheetAt(0);
Row row = sheet.getRow(3);
Cell cell = row.getCell(4);
cell = row.createCell(4);
cell.setCellType(Cell.CELL_TYPE_STRING);
cell.setCellValue("a test");

// Write the output to a file
FileOutputStream fileOut = new FileOutputStream("C:/Users/Training/Desktop/saurav.xlsx");
wb.write(fileOut);
fileOut.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

还要确保在每次运行后刷新 excel 文件。

关于java - 通过hssf/xssf向现有excel单元格写入数据。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14888716/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com