String sheetRef = sheet.getPackagePart().getPartName().getName();
//save the template
- FileOutputStream os = new FileOutputStream("template.xlsx");
- wb.write(os);
- os.close();
+ try (FileOutputStream os = new FileOutputStream("template.xlsx")) {
+ wb.write(os);
+ }
//Step 2. Generate XML file.
File tmp = File.createTempFile("sheet", ".xml");
- Writer fw = new OutputStreamWriter(new FileOutputStream(tmp), XML_ENCODING);
- generate(fw, styles);
- fw.close();
+ try (Writer fw = new OutputStreamWriter(new FileOutputStream(tmp), XML_ENCODING)) {
+ generate(fw, styles);
+ }
//Step 3. Substitute the template entry with the generated data
try (FileOutputStream out = new FileOutputStream("big-grid.xlsx")) {
* <ul>
* <li>
* Access initial cells and rows in the template. After constructing
- * {@link #SXSSFWorkbook(XSSFWorkbook)} all internal windows are empty and
- * {@link SXSSFSheet#getRow} and {@link SXSSFRow#getCell} return <code>null</code>.
+ * all internal windows are empty and {@link SXSSFSheet#getRow} and
+ * {@link SXSSFRow#getCell} return <code>null</code>.
* </li>
* <li>
* Override existing cells and rows. The API silently allows that but
protected void injectData(ZipEntrySource zipEntrySource, OutputStream out) throws IOException {
try {
- ZipOutputStream zos = new ZipOutputStream(out);
- try {
+ try (ZipOutputStream zos = new ZipOutputStream(out)) {
Enumeration<? extends ZipEntry> en = zipEntrySource.getEntries();
while (en.hasMoreElements()) {
ZipEntry ze = en.nextElement();
zos.putNextEntry(new ZipEntry(ze.getName()));
InputStream is = zipEntrySource.getInputStream(ze);
- XSSFSheet xSheet=getSheetFromZipEntryName(ze.getName());
+ XSSFSheet xSheet = getSheetFromZipEntryName(ze.getName());
// See bug 56557, we should not inject data into the special ChartSheets
- if(xSheet!=null && !(xSheet instanceof XSSFChartSheet)) {
- SXSSFSheet sxSheet=getSXSSFSheet(xSheet);
- InputStream xis = sxSheet.getWorksheetXMLInputStream();
- try {
- copyStreamAndInjectWorksheet(is,zos,xis);
- } finally {
- xis.close();
+ if (xSheet != null && !(xSheet instanceof XSSFChartSheet)) {
+ SXSSFSheet sxSheet = getSXSSFSheet(xSheet);
+ try (InputStream xis = sxSheet.getWorksheetXMLInputStream()) {
+ copyStreamAndInjectWorksheet(is, zos, xis);
}
} else {
IOUtils.copy(is, zos);
}
is.close();
}
- } finally {
- zos.close();
}
} finally {
zipEntrySource.close();
File tmplFile = TempFile.createTempFile("poi-sxssf-template", ".xlsx");
boolean deleted;
try {
- FileOutputStream os = new FileOutputStream(tmplFile);
- try {
+ try (FileOutputStream os = new FileOutputStream(tmplFile)) {
_wb.write(os);
- } finally {
- os.close();
}
//Substitute the template entries with the generated sheet data files
*/
protected static OPCPackage newPackage(XSSFWorkbookType workbookType) {
try {
- OPCPackage pkg = OPCPackage.create(new ByteArrayOutputStream());
+ OPCPackage pkg = OPCPackage.create(new ByteArrayOutputStream()); // NOSONAR - we do not want to close this here
// Main part
PackagePartName corePartName = PackagingURIHelper.createPartName(XSSFRelation.WORKBOOK.getDefaultFileName());
// Create main part relationship
@Override
public int addOlePackage(byte[] oleData, String label, String fileName, String command)
- throws IOException {
+ throws IOException {
// find an unused part name
OPCPackage opc = getPackage();
PackagePartName pnOLE;
ByteArrayOutputStream bos = new ByteArrayOutputStream(oleData.length+500);
ole10.writeOut(bos);
- POIFSFileSystem poifs = new POIFSFileSystem();
- DirectoryNode root = poifs.getRoot();
- root.createDocument(Ole10Native.OLE10_NATIVE, new ByteArrayInputStream(bos.toByteArray()));
- root.setStorageClsid(ClassID.OLE10_PACKAGE);
+ try (POIFSFileSystem poifs = new POIFSFileSystem()) {
+ DirectoryNode root = poifs.getRoot();
+ root.createDocument(Ole10Native.OLE10_NATIVE, new ByteArrayInputStream(bos.toByteArray()));
+ root.setStorageClsid(ClassID.OLE10_PACKAGE);
- // TODO: generate CombObj stream
+ // TODO: generate CombObj stream
- OutputStream os = pp.getOutputStream();
- poifs.writeFilesystem(os);
- os.close();
- poifs.close();
+ try (OutputStream os = pp.getOutputStream()) {
+ poifs.writeFilesystem(os);
+ }
+ }
return oleId;
}
*/
protected static OPCPackage newPackage() {
try {
- OPCPackage pkg = OPCPackage.create(new ByteArrayOutputStream());
+ OPCPackage pkg = OPCPackage.create(new ByteArrayOutputStream()); // NOSONAR - we do not want to close this here
// Main part
PackagePartName corePartName = PackagingURIHelper.createPartName(XWPFRelation.DOCUMENT.getDefaultFileName());
// Create main part relationship
@Override
public void headerFooter(String text, boolean isHeader, String tagName) {
if (isHeader) {
- sb.append("<header tagName=\"" + tagName + "\">" + text + "</header>");
+ sb.append("<header tagName=\"").append(tagName).append("\">").append(text).append("</header>");
} else {
- sb.append("<footer tagName=\"" + tagName + "\">" + text + "</footer>");
+ sb.append("<footer tagName=\"").append(tagName).append("\">").append(text).append("</footer>");
}
}