gpt4 book ai didi

java - 使用 java apache 在 excel 中添加单元格

转载 作者:行者123 更新时间:2023-12-01 12:29:22 24 4
gpt4 key购买 nike

我正在读取一个已经存在的 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();

Source (more examples here)

关于java - 使用 java apache 在 excel 中添加单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26068627/

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