20

基本问题:如何加载用于 POI 的 Excel 模板,然后将其保存到 XLS 文件中?

编辑:

答案是:

FileInputStream inputStream = new FileInputStream(new File(templateFile));
Workbook workbook = new HSSFWorkbook(inputStream);

(只需将模板加载为工作簿,然后将工作簿作为 XLS 文件写入其他地方。)

4

6 回答 6

16

您是否尝试过使用 POI 将其作为标准 .xls 加载、修改然后保存?

这是我用于在 POI 生成的 .xls 中插入宏的方法。我使用宏创建文件(诚然为 .xls),然后将其加载到我的应用程序中,填充数据并另存为新创建的 .xls。这一切都很好。

于 2009-04-03T14:43:25.820 回答
15

您可以直接加载将充当模板的 .xls 并对其进行修改。

POIFSFileSystem fs = new POIFSFileSystem(
                new FileInputStream("template.xls"));
HSSFWorkbook wb = new  HSSFWorkbook(fs, true);

将加载一个 xls,保留其结构(包括宏)。然后你可以修改它,

HSSFSheet sheet1 = wb.getSheet("Data");
...

然后保存它。

FileOutputStream fileOut = new FileOutputStream("new.xls"); 
wb.write(fileOut);
fileOut.close();

希望这可以帮助。

于 2009-04-03T15:01:21.097 回答
5

您还可以将内部模板用作资源。

InputStream fis = ChartSample.class.getResourceAsStream("/templates.xls");
HSSFWorkbook wb = new HSSFWorkbook(fis);        
fis.close();
HSSFSheet sh = wb.getSheetAt(0); 
//Here you go

并保存:

out = new FileOutputStream("./new.xls");
wb.write(out);
out.close();
于 2012-01-16T01:38:17.290 回答
3

您可以从 XLS 模板创建 XLS 文件。

但是,为此,您需要在每次需要使用模板时创建模板的副本。如果没有,您将编辑原始模板(您不想要的)。

因此,您需要首先获取您的模板文件:

URL url = Thread.currentThread().getContextClassLoader().getResource("templates/template.xls");
File file = new File(url.getPath());

复制模板文件:

try (FileOutputStream fileOutputStream = new FileOutputStream("/home/jake/fileCopiedFromTemplate.xls")) {

    Files.copy(file.toPath(), fileOutputStream);

    Workbook workbook = new HSSFWorkbook();
    workbook.write(fileOutputStream);
}

访问新复制的文件:

FileInputStream inp = new FileInputStream("/home/jake/fileCopiedFromTemplate.xls");

创建一个Workbook,以便您可以在新文件中写入:

Workbook workbook = WorkbookFactory.create(inp);

在你的工作簿上写下:

try (FileOutputStream fileOut = new FileOutputStream("/home/jake/fileCopiedFromTemplate.xls")) {
    workbook.write(fileOut);
}

创建 XLS 模板文件的提示是用一些变量标记模板,以便本地化要填充的位置。像:

------------------------------------
|   | Columna A     | Column B     |
------------------------------------
| 1 |  Some description            | 
------------------------------------
| 2 | {person.name} | {person.age} |
------------------------------------
于 2015-11-04T22:21:21.670 回答
2

对于.xlsx使用以下内容的 excel 文件:

FileInputStream inputStream = new FileInputStream(new File("template.xlsx"));
        @SuppressWarnings("resource")
        Workbook wb = new XSSFWorkbook(inputStream);  
        Sheet sheet = wb.getSheet("sheet1");
于 2016-12-14T13:46:46.507 回答
1

如果要将其保存为 2007+ 格式,则可以通过在 maven pom.xml 中添加 poi-ooxml 依赖项来使用 XSSF。

如果您在此 xmls 中有带有摘要表的 template.xlsx 文件,并且您想要更改特定单元格,您可以这样做:

XSSFWorkbook wb = new XSSFWorkbook(new FileInputStream("template.xlsx"));           
FileOutputStream fileOut = new FileOutputStream("new.xlsx");
XSSFSheet sheet1 = wb.getSheet("Summary");
XSSFRow row = sheet1.getRow(15);
XSSFCell cell = row.getCell(3);
cell.setCellValue("Bharthan");

wb.write(fileOut);
log.info("Written xls file");
fileOut.close();
于 2017-01-09T13:54:05.510 回答