java - 如何在 JAVA 中使用 apache POI 在 excel 中写入单元格?
问题描述
如何将excel文件写入特定的单元格。我有一个方法可以接受我必须写入的文件位置和行号。
我尝试的是读取文件然后创建一个单元格并将值设置到其中,但是当我查看它时,单元格仍然是空白的。
public void writeInExcelRowCell(String file , int rowNum) throws Exception {
FileInputStream excelFile = new FileInputStream(new File(file));
Workbook workbook = new XSSFWorkbook(excelFile);
Sheet sheet = workbook.getSheetAt(0);
sheet.getRow(rowNum).createCell(2).setCellValue("DONE");
sheet.getRow(rowNum).createCell(3).setCellValue("PENDING");
workbook.close();
}
解决方案
您所做的是正确的,但忘记写下您的更改。您需要打开一个 Outputstream 来写入这些更改。
FileOutputStream outputStream = new FileOutputStream("yourfile");
workbook.write(outputStream);
workbook.close();
outputStream.close();
希望这有效!
推荐阅读
- show-hide - 仅显示/隐藏淡入淡出的文本 css
- java - com.google.gson.JsonSyntaxException:java.lang.IllegalStateException:不是 JSON 数组:{"type":"Point","coordinates":[-122.3964485,37.7912561]}
- php - 获取数组中小于'x'的最高键
- node.js - 猫鼬:中间件预删除一个选项不起作用
- gitahead - GitAhead - 我可以“打开现有存储库”到“远程”列表中吗?
- apache-spark - 运行 Apache Hudi deltastreamer 时出错
- node.js - 如何使用 axios 和 Cheerio 实现多页抓取
- python-3.x - 我应该将协程用于异步代码中的同步代码吗?
- flutter - 如何在 Flutter 中与 BLE 设备进行绑定
- python - 自动化模型中的销售柜台