答案 0 :(得分:1)
通常,您无法内联更新电子表格,或者API无论如何都无法正常运行。但是,这是您可以修改现有电子表格并使用更改的工作簿覆盖旧工作簿的方法。
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
public class ModifySheet {
public static void main(String[] args) throws Exception {
String fileName = "MyExcel.xlsx";
// load the workbook
InputStream inp = new FileInputStream(fileName);
Workbook wb = WorkbookFactory.create(inp);
inp.close();
// make some changes
Sheet sh = wb.getSheetAt(0);
Row r = sh.createRow(sh.getPhysicalNumberOfRows());
Cell c = r.createCell(0);
c.setCellValue(555);
c = r.createCell(1);
c.setCellValue(777);
// overwrite the workbook with changed workbook
FileOutputStream fileOut = new FileOutputStream(fileName);
wb.write(fileOut);
fileOut.close();
wb.close();
}
}
以下是您的参考资料,因此您无需进行大量搜索:https://poi.apache.org/spreadsheet/quick-guide.html
该网站上还有很多其他有用的东西,包括javadoc和示例。