我正在读取一个已经存在的 Excel 文件并尝试添加某些单元格,例如(C4-C15)。我很难通过 java 操作文件。我正在使用 apache poi,希望得到任何帮助或指导。
最佳答案
为了访问 Apache POI 中的单元格,您必须先获取一行,然后获取所选行内的单元格。下面是示例:
//Input file
InputStream inp = new FileInputStream("workbook.xls");
//Create workbook instance
Workbook wb = WorkbookFactory.create(inp);
//Create sheet instance
Sheet sheet = wb.getSheetAt(0);
for(int i = 3; i <= 16; ++i){ //Rows from 4 to 15 (Apache POI is zero based)
Row row = sheet.getRow(i);
Cell cell = row.getCell(2); //Column "C"
//Do something with cell
}
// Write the output to a file
FileOutputStream fileOut = new FileOutputStream("workbook.xls");
wb.write(fileOut);
fileOut.close();
关于java - 使用 java apache 在 excel 中添加单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26068627/