我正在阅读已经存在的excel文件并尝试添加某些单元格(例如C4-C15)。我很难通过java操作文件。我正在使用apache poi,并希望得到任何帮助或指导。
答案 0 :(得分:1)
要访问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();