Him*_*ula 2 java junit selenium apache-poi
我正在使用以下代码,使用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.
Run Code Online (Sandbox Code Playgroud)
谢谢.
Art*_*nov 12
它应该是这样的:
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");
}
Run Code Online (Sandbox Code Playgroud)
要保存工作簿,您可以写:
FileOutputStream fos = new FileOutputStream(fileName);
workbook.write(fos);
fos.close();
Run Code Online (Sandbox Code Playgroud)
你应该看看POI的繁忙开发者指南.
| 归档时间: |
|
| 查看次数: |
22469 次 |
| 最近记录: |