gpt4 book ai didi

java - 将新单元格写入工作表 apache poi

转载 作者:行者123 更新时间:2023-12-03 23:08:52 25 4
gpt4 key购买 nike

我正在使用以下代码,使用 apache poi 读取 excel,它是一个 .xlsx 文件。请让我知道我能做什么,在我的循环继续进行时,在每一行中更改单元格的值。谢谢

import java.io.FileInputStream;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

String fileName = "C:/createCDN.xlsx";
FileInputStream fis = null;
fis = new FileInputStream(fileName);
XSSFWorkbook workbook = new XSSFWorkbook(fis);
FileOutputStream fos=new FileOutputStream(fileName);
XSSFSheet sheet = workbook.getSheetAt(0);
int totalRows=sheet.getLastRowNum();
for(int i=1;i<=totalRows;i++) {
XSSFRow row = sheet.getRow(i);
method.fillTextBox(row.getCell(0),"name", pageProp);
method.fillTextBox(row.getCell(0),"name", pageProp);
} //Do something here, or inside loop to write to lets say cell(2) of same row.

谢谢。

最佳答案

应该是这样的:

for(int i = 1; i <= totalRows; i++) {
Row row = sheet.getRow(i);
Cell cell = row.getCell(2);
if (cell == null) {
cell = row.createCell(2);
}
cell.setCellType(Cell.CELL_TYPE_STRING);
cell.setCellValue("some value");
}

要保存工作簿,您可以编写:
FileOutputStream fos = new FileOutputStream(fileName);
workbook.write(fos);
fos.close();

你应该看看 POI's Busy Developers' Guide .

关于java - 将新单元格写入工作表 apache poi,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12754641/

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