From 1a808e9f8513d90d2f5e491610539358f886eb07 Mon Sep 17 00:00:00 2001 From: noboru saitoh Date: Tue, 14 Oct 2014 01:52:35 +0900 Subject: [PATCH] =?utf8?q?InputStream=E3=82=92=E5=BC=95=E6=95=B0=E3=81=A8?= =?utf8?q?=E3=81=99=E3=82=8BmakeReport=E3=81=A7=E3=81=AEExcel2007=E4=BB=A5?= =?utf8?q?=E9=99=8D=E3=81=AE=E3=83=86=E3=83=B3=E3=83=97=E3=83=AC=E3=83=BC?= =?utf8?q?=E3=83=88=E3=83=95=E3=82=A1=E3=82=A4=E3=83=ABOpenError=E5=AF=BE?= =?utf8?q?=E5=BF=9C=20InputStream=E3=81=AF=E4=B8=80=E6=99=82=E3=83=95?= =?utf8?q?=E3=82=A1=E3=82=A4=E3=83=AB=E3=81=AB=E5=87=BA=E5=8A=9B=E3=81=99?= =?utf8?q?=E3=82=8B=E3=82=88=E3=81=86=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../src/org/hanei/jaxcel/report/ReportMaker.java | 108 +++++++++++++++------ 1 file changed, 77 insertions(+), 31 deletions(-) diff --git a/Jaxcel/src/org/hanei/jaxcel/report/ReportMaker.java b/Jaxcel/src/org/hanei/jaxcel/report/ReportMaker.java index b355d19..e6bac69 100644 --- a/Jaxcel/src/org/hanei/jaxcel/report/ReportMaker.java +++ b/Jaxcel/src/org/hanei/jaxcel/report/ReportMaker.java @@ -18,6 +18,7 @@ */ package org.hanei.jaxcel.report; +import java.io.BufferedInputStream; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; @@ -28,6 +29,7 @@ import java.util.Map; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; +import org.apache.poi.openxml4j.exceptions.InvalidFormatException; import org.apache.poi.openxml4j.opc.OPCPackage; import org.apache.poi.poifs.filesystem.NPOIFSFileSystem; import org.apache.poi.poifs.filesystem.OfficeXmlFileException; @@ -187,6 +189,11 @@ public class ReportMaker { private JaxcelContext context = null; /** + * テンプレート一時ファイル + */ + private File templateFile = null; + + /** * コンストラクタ */ public ReportMaker() {} @@ -194,7 +201,8 @@ public class ReportMaker { /** * 入力ストリームのExcelテンプレートファイルにデータを挿入することでExcel帳票を生成、Workbookオブジェクトを返却する。
* 返却されたWorkbookオブジェクトはPOIを使用し、加工・出力が可能。
- * 入力ストリームは別途クローズが必要。 + * 入力ストリームは別途クローズが必要。
+ * Workbookオブジェクトの使用終了後はclose()メソッドでExcelテンプレートファイルのクローズが必要。 * * @param template Excelテンプレートファイル入力ストリーム * @param parameter テンプレートに挿入するデータ @@ -211,15 +219,17 @@ public class ReportMaker { log.error("template is null"); throw new JaxcelInputException("template is null"); } - if(parameter == null) { - log.debug("parameter is null"); + + // 一時ファイル作成 + if (templateFile != null && templateFile.exists()) { + templateFile.delete(); + log.debug("template file delete: {}", templateFile.getPath()); + templateFile = null; } - - // Excelテンプレートファイルオープン - Workbook book = openWorkbook(template); + templateFile = createTempFile(template); // Excel帳票生成 - makeReport(book, parameter); + Workbook book = makeReport(templateFile, parameter); log.trace("makeReport end"); return book; @@ -296,7 +306,7 @@ public class ReportMaker { /** * Excelテンプレートファイルにデータを挿入することでExcel帳票を生成、Workbookオブジェクトを返却する。
* 返却されたWorkbookオブジェクトはPOIを使用し、加工・出力が可能。
- * Excelテンプレートファイルは別途クローズが必要。 + * Workbookオブジェクトの使用終了後はclose()メソッドでExcelテンプレートファイルのクローズが必要。 * * @param template Excelテンプレートファイル * @param parameter テンプレートに挿入するデータ @@ -404,7 +414,7 @@ public class ReportMaker { /** * ExcelテンプレートのWorkbookオブジェクトにデータを挿入することでExcel帳票を生成する。
- * Excelテンプレートファイルは別途クローズが必要。 + * Workbookオブジェクトの使用終了後はclose()メソッドでExcelテンプレートファイルのクローズが必要。 * * @param book Workbookオブジェクト * @param parameter テンプレートに挿入するデータ @@ -446,40 +456,27 @@ public class ReportMaker { /** * ワークブックのオープン * - * @param template Excelテンプレートファイル 入力ストリーム or ファイル + * @param template Excelテンプレートファイル * * @throws JaxcelInputException Excelテンプレートファイルオープン失敗時 */ - private Workbook openWorkbook(Object template) { + private Workbook openWorkbook(File template) { log.trace("openWorkbook start"); // Workbookオブジェクト Workbook book = null; - - // Excelテンプレートファイルオープン - + + // Excel2003以前 try { - // Excel2003以前 - if(template instanceof File) { - npoifs = new NPOIFSFileSystem((File) template); - } - else { - npoifs = new NPOIFSFileSystem((InputStream) template); - } + npoifs = new NPOIFSFileSystem(template); book = WorkbookFactory.create(npoifs); - } catch(OfficeXmlFileException | IOException e1) { - + } catch (OfficeXmlFileException | IOException e1) { // Excel2007以降 try { - if(template instanceof File) { - pkg = OPCPackage.open((File) template); - } - else { - pkg = OPCPackage.open((InputStream) template); - } + pkg = OPCPackage.open(template); book = WorkbookFactory.create(pkg); - } catch (Exception e2) { - log.error("template file open error: {}. {}", e1.getMessage(), e2.getMessage()); + } catch (InvalidFormatException | IOException e2) { + log.error("template file open error: [{}]. [{}]", e1.getMessage(), e2.getMessage()); } } @@ -535,6 +532,11 @@ public class ReportMaker { log.debug("template file close."); pkg = null; } + if (templateFile != null && templateFile.exists()) { + templateFile.delete(); + log.debug("template file delete: {}", templateFile.getPath()); + templateFile = null; + } } catch (IOException e) { log.error("template file close error: {}", e.getMessage(), e); throw new JaxcelOutputException("template file close error"); @@ -544,6 +546,50 @@ public class ReportMaker { } /** + * InputStreamから一時ファイルを作成 + * @param template + * @return Fileオブジェクト + * + * @throws JaxcelInputException 一時ファイル作成エラー発生時 + */ + private File createTempFile(InputStream template) { + log.trace("createTempFile start"); + + final String PREFIX = "org.hanei.jaxcel_"; + final String SUFFIX = ".tmp"; + final int BUF_SIZE = 1024; + + BufferedInputStream inStream = null; + File tmpFile = null; + + if(template instanceof BufferedInputStream) { + inStream = (BufferedInputStream) template; + } + else { + inStream = new BufferedInputStream((InputStream) template); + } + try { + tmpFile = File.createTempFile(PREFIX, SUFFIX); + FileOutputStream outStream = new FileOutputStream(tmpFile); + + byte buf[]=new byte[BUF_SIZE]; + int len; + while((len = inStream.read(buf)) != -1){ + outStream.write(buf, 0, len); + } + outStream.flush(); + outStream.close(); + log.debug("createTempFile: {}", tmpFile.getPath()); + } catch (SecurityException | IOException e) { + log.error("template file create error: {}", e.getMessage()); + throw new JaxcelInputException("template file create error"); + } + + log.trace("createTempFile end"); + return tmpFile; + } + + /** * Workbook生成 * @param book Workbookオブジェクト */ -- 2.11.0