【发布时间】:2013-07-07 12:35:59
【问题描述】:
我有一个现有文件 (C:\wb.xls),我想打开并对其进行更改。如何在阿帕奇 POI 中打开现有文件?我找到的所有文档都必须与创建一个新文件有关。如果您知道,如何在 xls 文件顶部插入新行或如何自动设置列宽?
【问题讨论】:
标签: java apache-poi
我有一个现有文件 (C:\wb.xls),我想打开并对其进行更改。如何在阿帕奇 POI 中打开现有文件?我找到的所有文档都必须与创建一个新文件有关。如果您知道,如何在 xls 文件顶部插入新行或如何自动设置列宽?
【问题讨论】:
标签: java apache-poi
您是否尝试阅读Apache POI HowTo "Reading or modifying an existing file"?那应该涵盖你...
基本上,你想要做的是取自QuickGuide 例如this for loading a File
Workbook wb = WorkbookFactory.create(new File("MyExcel.xls"));
Sheet s = wb.getSheetAt(0);
// Get the 11th row, creating if not there
Row r1 = s.getRow(10);
if (r1 == null) r1 = s.createRow(10);
// Get the 3rd column, creating if not there
Cell c2 = r1.getCell(2, Row.CREATE_NULL_AS_BLANK);
// Set a string to be the value
c2.setCellValue("Hello, I'm the cell C10!");
// Save
FileOutputStream out = new FileOutputStream("New.xls");
wb.write(out);
out.close();
【讨论】:
使用以下之一
XSSFWorkbook wb = new XSSFWorkbook(new FileInputStream(xlFileAddress));
或
Workbook wb = WorkbookFactory.create(new File(xlFileAddress));
或
Workbook wb = WorkbookFactory.create(new FileInputStream(xlFileAddress));
然后使用 wb 来创建/读取/更新工作表/行/单元格,随心所欲。详情请访问here。这肯定会对你有所帮助。
【讨论】: