XSSFWorkBook覆盖模板文件

发布于 2025-01-22 15:15:05 字数 1190 浏览 3 评论 0原文

我正在使用XSSFWorkbook创建一个XLSX文件,该文件存储使用Java 8的SQL查询结果。

此过程是每个月完成的,因此我使用模板XSLX文件Template.xslx。

打开模板并将其保存为新文件的要点。 它曾经工作,但现在模板也经过了修改并充满了数据(仅在列大小,颜色,标头...)。

这是我要做的:

ZipSecureFile.setMinInflateRatio(0);
OPCPackage pkg = OPCPackage.open(new File(excelMaquette));      // Template xlsx            
XSSFWorkbook wb = new XSSFWorkbook(pkg);                        // Fichier xlsx

excelFinal += "_"+dateDuJour+".xlsx";

FileOutputStream stream = new FileOutputStream(new File(excelFinal));

XSSFSheet sheet = wb.getSheetAt(0);             

XSSFCellStyle style = wb.createCellStyle();
            
//remplissage du tableau pour la feuille de détail
for (int i = 0 ; i <= nombreLigneResultat ; i++)
{
    XSSFRow row = sheet.createRow((short)i+1);
                
    for (int l = 0; l < nombreColonnes + 1; l ++)
    {
        XSSFCell  cell = row.createCell(l);         
        cell.setCellValue(tab[i][l]);   
        //System.out.println (" xls = " + tab[i][l]);
    }               
}
                
wb.write(stream);
stream.close();
wb.close();

感谢您的帮助

编辑:我尝试重写和重命名文件,但仍然相同... 之所以不错,是因为有时新文件的数据将比上一个数据少,因此该文件将显示新的和特殊的旧数据:(

I'm using XSSFWorkbook to create a xlsx file which stores the result of a SQL query with JAVA 8.

This process is done each month so I use a template xslx file template.xslx.

The point it to open the template and save it as new file.
It used to work but now the template is also modified and filled with data (it should stay empty with just the column size, the color, the headers ...).

Here is what I do :

ZipSecureFile.setMinInflateRatio(0);
OPCPackage pkg = OPCPackage.open(new File(excelMaquette));      // Template xlsx            
XSSFWorkbook wb = new XSSFWorkbook(pkg);                        // Fichier xlsx

excelFinal += "_"+dateDuJour+".xlsx";

FileOutputStream stream = new FileOutputStream(new File(excelFinal));

XSSFSheet sheet = wb.getSheetAt(0);             

XSSFCellStyle style = wb.createCellStyle();
            
//remplissage du tableau pour la feuille de détail
for (int i = 0 ; i <= nombreLigneResultat ; i++)
{
    XSSFRow row = sheet.createRow((short)i+1);
                
    for (int l = 0; l < nombreColonnes + 1; l ++)
    {
        XSSFCell  cell = row.createCell(l);         
        cell.setCellValue(tab[i][l]);   
        //System.out.println (" xls = " + tab[i][l]);
    }               
}
                
wb.write(stream);
stream.close();
wb.close();

Thanks for you help

EDIT: I tried rewriting and renaming my files but it's still the same...
Its bad because, sometimes the new file will have less data than the previous one so the file will display the new and the extra old data :(

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你与昨日 2025-01-29 15:15:05

为什么使用opcpackage

opcpackage是从templatefile in read_write模式下打开的。如果将其关闭,也将保存。

您可以这样做:

try (FileInputStream inputStream = new FileInputStream(templateFile);
            FileOutputStream outputStream = new FileOutputStream(new File(finalFile));
            Workbook workbook = new XSSFWorkbook(inputStream)) {
    wb.getSheetAt(0);

    /*
     *
     * Your POI related operations.
     *
     */

    wb.write(stream);
    outputStream.close();
    wb.close();
}

Why are you using OPCPackage?

OPCPackage is opened from the templateFile in READ_WRITE mode. If this will be closed, it will also be saved.

You can do it this way:

try (FileInputStream inputStream = new FileInputStream(templateFile);
            FileOutputStream outputStream = new FileOutputStream(new File(finalFile));
            Workbook workbook = new XSSFWorkbook(inputStream)) {
    wb.getSheetAt(0);

    /*
     *
     * Your POI related operations.
     *
     */

    wb.write(stream);
    outputStream.close();
    wb.close();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文