将新的单元格写入工作表apache poi [英] writing a new cell to a sheet apache poi
本文介绍了将新的单元格写入工作表apache poi的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用以下代码,以便使用apache poi(其.xlsx文件)读取Excel.请让我知道我可以做些什么,也可以在循环不断进行的同时更改每行中一个单元格的值.谢谢
i am using following code, for reading a excel using apache poi, its a .xlsx file. Please let me know what i can do, to also alter a value of a cell, in each row as my loop keeps going. Thanks
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");
}
要保存工作簿,您可以编写:
To save the workbook you could just write:
FileOutputStream fos = new FileOutputStream(fileName);
workbook.write(fos);
fos.close();
您应该查看 POI忙碌的开发人员指南.
这篇关于将新的单元格写入工作表apache poi的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文