• R/O
  • HTTP
  • SSH
  • HTTPS

jaxcel: 提交


Commit MetaInfo

修訂1a808e9f8513d90d2f5e491610539358f886eb07 (tree)
時間2014-10-14 01:52:35
作者noboru saitoh <msnobosan@gmal...>
Commiternoboru saitoh

Log Message

InputStreamを引数とするmakeReportでのExcel2007以降のテンプレートファイルOpenError対応
InputStreamは一時ファイルに出力するよう修正

Change Summary

差異

--- a/Jaxcel/src/org/hanei/jaxcel/report/ReportMaker.java
+++ b/Jaxcel/src/org/hanei/jaxcel/report/ReportMaker.java
@@ -18,6 +18,7 @@
1818 */
1919 package org.hanei.jaxcel.report;
2020
21+import java.io.BufferedInputStream;
2122 import java.io.File;
2223 import java.io.FileOutputStream;
2324 import java.io.IOException;
@@ -28,6 +29,7 @@ import java.util.Map;
2829
2930 import org.apache.poi.hssf.usermodel.HSSFSheet;
3031 import org.apache.poi.hssf.usermodel.HSSFWorkbook;
32+import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
3133 import org.apache.poi.openxml4j.opc.OPCPackage;
3234 import org.apache.poi.poifs.filesystem.NPOIFSFileSystem;
3335 import org.apache.poi.poifs.filesystem.OfficeXmlFileException;
@@ -187,6 +189,11 @@ public class ReportMaker {
187189 private JaxcelContext context = null;
188190
189191 /**
192+ * テンプレート一時ファイル
193+ */
194+ private File templateFile = null;
195+
196+ /**
190197 * コンストラクタ
191198 */
192199 public ReportMaker() {}
@@ -194,7 +201,8 @@ public class ReportMaker {
194201 /**
195202 * 入力ストリームのExcelテンプレートファイルにデータを挿入することでExcel帳票を生成、Workbookオブジェクトを返却する。<br>
196203 * 返却されたWorkbookオブジェクトはPOIを使用し、加工・出力が可能。<br>
197- * 入力ストリームは別途クローズが必要。
204+ * 入力ストリームは別途クローズが必要。<br>
205+ * Workbookオブジェクトの使用終了後はclose()メソッドでExcelテンプレートファイルのクローズが必要。
198206 *
199207 * @param template Excelテンプレートファイル入力ストリーム
200208 * @param parameter テンプレートに挿入するデータ
@@ -211,15 +219,17 @@ public class ReportMaker {
211219 log.error("template is null");
212220 throw new JaxcelInputException("template is null");
213221 }
214- if(parameter == null) {
215- log.debug("parameter is null");
222+
223+ // 一時ファイル作成
224+ if (templateFile != null && templateFile.exists()) {
225+ templateFile.delete();
226+ log.debug("template file delete: {}", templateFile.getPath());
227+ templateFile = null;
216228 }
217-
218- // Excelテンプレートファイルオープン
219- Workbook book = openWorkbook(template);
229+ templateFile = createTempFile(template);
220230
221231 // Excel帳票生成
222- makeReport(book, parameter);
232+ Workbook book = makeReport(templateFile, parameter);
223233
224234 log.trace("makeReport end");
225235 return book;
@@ -296,7 +306,7 @@ public class ReportMaker {
296306 /**
297307 * Excelテンプレートファイルにデータを挿入することでExcel帳票を生成、Workbookオブジェクトを返却する。<br>
298308 * 返却されたWorkbookオブジェクトはPOIを使用し、加工・出力が可能。<br>
299- * Excelテンプレートファイルは別途クローズが必要。
309+ * Workbookオブジェクトの使用終了後はclose()メソッドでExcelテンプレートファイルのクローズが必要。
300310 *
301311 * @param template Excelテンプレートファイル
302312 * @param parameter テンプレートに挿入するデータ
@@ -404,7 +414,7 @@ public class ReportMaker {
404414
405415 /**
406416 * ExcelテンプレートのWorkbookオブジェクトにデータを挿入することでExcel帳票を生成する。<br>
407- * Excelテンプレートファイルは別途クローズが必要。
417+ * Workbookオブジェクトの使用終了後はclose()メソッドでExcelテンプレートファイルのクローズが必要。
408418 *
409419 * @param book Workbookオブジェクト
410420 * @param parameter テンプレートに挿入するデータ
@@ -446,40 +456,27 @@ public class ReportMaker {
446456 /**
447457 * ワークブックのオープン
448458 *
449- * @param template Excelテンプレートファイル 入力ストリーム or ファイル
459+ * @param template Excelテンプレートファイル
450460 *
451461 * @throws JaxcelInputException Excelテンプレートファイルオープン失敗時
452462 */
453- private Workbook openWorkbook(Object template) {
463+ private Workbook openWorkbook(File template) {
454464 log.trace("openWorkbook start");
455465
456466 // Workbookオブジェクト
457467 Workbook book = null;
458-
459- // Excelテンプレートファイルオープン
460-
468+
469+ // Excel2003以前
461470 try {
462- // Excel2003以前
463- if(template instanceof File) {
464- npoifs = new NPOIFSFileSystem((File) template);
465- }
466- else {
467- npoifs = new NPOIFSFileSystem((InputStream) template);
468- }
471+ npoifs = new NPOIFSFileSystem(template);
469472 book = WorkbookFactory.create(npoifs);
470- } catch(OfficeXmlFileException | IOException e1) {
471-
473+ } catch (OfficeXmlFileException | IOException e1) {
472474 // Excel2007以降
473475 try {
474- if(template instanceof File) {
475- pkg = OPCPackage.open((File) template);
476- }
477- else {
478- pkg = OPCPackage.open((InputStream) template);
479- }
476+ pkg = OPCPackage.open(template);
480477 book = WorkbookFactory.create(pkg);
481- } catch (Exception e2) {
482- log.error("template file open error: {}. {}", e1.getMessage(), e2.getMessage());
478+ } catch (InvalidFormatException | IOException e2) {
479+ log.error("template file open error: [{}]. [{}]", e1.getMessage(), e2.getMessage());
483480 }
484481 }
485482
@@ -535,6 +532,11 @@ public class ReportMaker {
535532 log.debug("template file close.");
536533 pkg = null;
537534 }
535+ if (templateFile != null && templateFile.exists()) {
536+ templateFile.delete();
537+ log.debug("template file delete: {}", templateFile.getPath());
538+ templateFile = null;
539+ }
538540 } catch (IOException e) {
539541 log.error("template file close error: {}", e.getMessage(), e);
540542 throw new JaxcelOutputException("template file close error");
@@ -544,6 +546,50 @@ public class ReportMaker {
544546 }
545547
546548 /**
549+ * InputStreamから一時ファイルを作成
550+ * @param template
551+ * @return Fileオブジェクト
552+ *
553+ * @throws JaxcelInputException 一時ファイル作成エラー発生時
554+ */
555+ private File createTempFile(InputStream template) {
556+ log.trace("createTempFile start");
557+
558+ final String PREFIX = "org.hanei.jaxcel_";
559+ final String SUFFIX = ".tmp";
560+ final int BUF_SIZE = 1024;
561+
562+ BufferedInputStream inStream = null;
563+ File tmpFile = null;
564+
565+ if(template instanceof BufferedInputStream) {
566+ inStream = (BufferedInputStream) template;
567+ }
568+ else {
569+ inStream = new BufferedInputStream((InputStream) template);
570+ }
571+ try {
572+ tmpFile = File.createTempFile(PREFIX, SUFFIX);
573+ FileOutputStream outStream = new FileOutputStream(tmpFile);
574+
575+ byte buf[]=new byte[BUF_SIZE];
576+ int len;
577+ while((len = inStream.read(buf)) != -1){
578+ outStream.write(buf, 0, len);
579+ }
580+ outStream.flush();
581+ outStream.close();
582+ log.debug("createTempFile: {}", tmpFile.getPath());
583+ } catch (SecurityException | IOException e) {
584+ log.error("template file create error: {}", e.getMessage());
585+ throw new JaxcelInputException("template file create error");
586+ }
587+
588+ log.trace("createTempFile end");
589+ return tmpFile;
590+ }
591+
592+ /**
547593 * Workbook生成
548594 * @param book Workbookオブジェクト
549595 */
Show on old repository browser