XSSFWorkBook覆盖模板文件
我正在使用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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为什么使用
opcpackage
?opcpackage
是从templatefile
inread_write
模式下打开的。如果将其关闭,也将保存。您可以这样做:
Why are you using
OPCPackage
?OPCPackage
is opened from thetemplateFile
inREAD_WRITE
mode. If this will be closed, it will also be saved.You can do it this way: