From f060679c86e86d1cd00aa500e2ed4dab414a686a Mon Sep 17 00:00:00 2001 From: "Cloud.Liu" Date: Wed, 8 Jul 2020 23:38:39 +0800 Subject: [PATCH 1/2] =?UTF-8?q?DEC-13978=20=E5=AE=9A=E6=97=B6=E8=B0=83?= =?UTF-8?q?=E5=BA=A6=E4=B8=8A=E4=BC=A0=E9=99=84=E4=BB=B6Okb=20=E4=BF=AE?= =?UTF-8?q?=E6=94=B9=E5=B8=AE=E5=8A=A9=E6=96=87=E6=A1=A3DEMO?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/com/fr/output/OutputExcel.java | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/src/main/java/com/fr/output/OutputExcel.java b/src/main/java/com/fr/output/OutputExcel.java index f7c87a6..72d6469 100644 --- a/src/main/java/com/fr/output/OutputExcel.java +++ b/src/main/java/com/fr/output/OutputExcel.java @@ -32,14 +32,19 @@ public class OutputExcel extends OutputActionHandler { private void output(String path) { - String realPath = ResourceIOUtils.getRealPath(path); - File file = new File(realPath); - String newPath = "C:/test/" + file.getName(); - BufferedInputStream in = null; + InputStream in = ResourceIOUtils.read(path); OutputStream out = null; + + if (in == null) { + // 读文件失败 + return; + } + try { + // 写文件到本地磁盘 + String newPath = "C:/test/" + ResourceIOUtils.getName(path); out = new BufferedOutputStream(new FileOutputStream(new File(newPath))); - in = new BufferedInputStream(new FileInputStream(realPath)); + in = new BufferedInputStream(in); byte[] ba = new byte[in.available()]; in.read(ba); out.write(ba); @@ -47,9 +52,7 @@ public class OutputExcel extends OutputActionHandler { e.printStackTrace(); } finally { try { - if (in != null) { - in.close(); - } + in.close(); if (out != null) { out.close(); } From 99ff96fea6c0ee7aab13c3176178c41481a1ef0f Mon Sep 17 00:00:00 2001 From: "Leo.Cai" <1836630213@qq.com> Date: Mon, 13 Jul 2020 10:17:18 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E4=BB=A3=E7=A0=81=E5=81=9A=E4=BA=86?= =?UTF-8?q?=E4=BF=AE=E6=94=B9=EF=BC=8C=E8=80=8C=E4=B8=94=E8=A6=81=E5=AF=BC?= =?UTF-8?q?=E5=85=A5=E4=B8=80=E4=B8=AA=E6=96=B0=E7=9A=84JAR=E5=8C=85?= =?UTF-8?q?=EF=BC=9Aslf4j-simple-1.7.25.jar=EF=BC=8CJAR=E5=8C=85=E4=B8=8B?= =?UTF-8?q?=E8=BD=BD=E5=8F=82=E8=A7=81=E6=96=87=E6=A1=A3https://help.finer?= =?UTF-8?q?eport.com/doc-view-735.html=20=E7=9A=843.1=E8=8A=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/com/fr/io/ExportApi.java | 158 ++++++++++++++++--------- 1 file changed, 100 insertions(+), 58 deletions(-) diff --git a/src/main/java/com/fr/io/ExportApi.java b/src/main/java/com/fr/io/ExportApi.java index 084a50b..0d70ac1 100644 --- a/src/main/java/com/fr/io/ExportApi.java +++ b/src/main/java/com/fr/io/ExportApi.java @@ -7,10 +7,13 @@ import com.fr.config.activator.BaseDBActivator; import com.fr.config.activator.ConfigurationActivator; import com.fr.env.operator.CommonOperatorImpl; import com.fr.general.I18nResource; +import com.fr.general.log.Log4jConfig; +import com.fr.general.log.parser.ExtraPatternParserManager; +import com.fr.health.activator.ModuleHealActivator; +import com.fr.io.TemplateWorkBookIO; +import com.fr.io.exporter.*; import com.fr.io.exporter.excel.stream.StreamExcel2007Exporter; import com.fr.main.impl.WorkBook; -import com.fr.main.workbook.ResultWorkBook; -import com.fr.module.Module; import com.fr.module.tool.ActivatorToolBox; import com.fr.report.ReportActivator; import com.fr.report.RestrictionActivator; @@ -18,19 +21,31 @@ import com.fr.report.module.ReportBaseActivator; import com.fr.report.write.WriteActivator; import com.fr.scheduler.SchedulerActivator; import com.fr.stable.WriteActor; +import com.fr.stable.resource.ResourceLoader; import com.fr.store.StateServiceActivator; +import com.fr.third.apache.log4j.Level; +import com.fr.third.apache.log4j.PropertyConfigurator; import com.fr.workspace.simple.SimpleWork; -import java.io.File; import java.io.FileOutputStream; +import java.io.IOException; +import java.util.HashMap; +import java.util.Map; +import java.util.Properties; public class ExportApi { + public static void main(String[] args) { - // 定义报表运行环境,用于执行报表 - Module module = ActivatorToolBox.simpleLink(new BaseDBActivator(), + + initLog4j(); + + /**定义报表运行环境,用于执行报表*/ + com.fr.module.Module module = ActivatorToolBox.simpleLink(new BaseDBActivator(), new ConfigurationActivator(), new StandaloneModeActivator(), + new ModuleHealActivator(), + //2020.4.26jar包之前的版本,替换成StateServerActivator() new StateServiceActivator(), new SchedulerActivator(), new ReportBaseActivator(), @@ -39,67 +54,73 @@ public class ExportApi { new WriteActivator(), new ChartBaseActivator()); SimpleWork.supply(CommonOperator.class, new CommonOperatorImpl()); - String envpath = "E:\\默认工程\\webroot\\WEB-INF\\";//工程路径 + //定义工程路径 + String envpath = "D:\\javatools\\FineReport_10.0\\webapps\\webroot\\WEB-INF\\"; SimpleWork.checkIn(envpath); I18nResource.getInstance(); module.start(); - - ResultWorkBook rworkbook = null; + /**输出模板*/ try { - // 未执行模板工作薄 - WorkBook workbook = (WorkBook) TemplateWorkBookIO - .readTemplateWorkBook("test.cpt"); - // 获取报表参数并设置值,导出内置数据集时数据集会根据参数值查询出结果从而转为内置数据集 -// Parameter[] parameters = workbook.getParameters(); -// parameters[0].setValue("华东"); - // 定义parametermap用于执行报表,将执行后的结果工作薄保存为rworkBook - java.util.Map parameterMap = new java.util.HashMap(); -// for (int i = 0; i < parameters.length; i++) { -// parameterMap.put(parameters[i].getName(), parameters[i] -// .getValue()); -// } + // 定义输出的模板路径,以reportlets为根目录 + WorkBook workbook = (WorkBook) TemplateWorkBookIO.readTemplateWorkBook("GettingStarted.cpt"); + // 设置传入的参数 + Map parameterMap = new HashMap(); + parameterMap.put("地区","华东"); + // 定义输出流 FileOutputStream outputStream; -// // 将未执行模板工作薄导出为内置数据集模板 -// outputStream = new FileOutputStream(new File("/Users//susie//Downloads//EmbExport.cpt")); -// EmbeddedTableDataExporter templateExporter = new EmbeddedTableDataExporter(); -// templateExporter.export(outputStream, workbook); -// // 将模板工作薄导出模板文件,在导出前您可以编辑导入的模板工作薄,可参考报表调用章节 -// outputStream = new FileOutputStream(new File("/Users//susie//Downloads//TmpExport.cpt")); -// ((WorkBook) workbook).export(outputStream); -// // 将结果工作薄导出为2003Excel文件 -// outputStream = new FileOutputStream(new File("/Users//susie//Downloads//ExcelExport.xls")); -// ExcelExporter ExcelExport = new ExcelExporter(); -// ExcelExport.export(outputStream, workbook.execute(parameterMap, new WriteActor())); - // 将结果工作薄导出为Excel文件 - outputStream = new FileOutputStream(new File("D://tttt//1528.xlsx")); + String outputUrl="F:\\io\\WorkBook\\"; + + /**将模板工作薄导出为内置数据集模板文件*/ + outputStream = new FileOutputStream(new java.io.File(outputUrl+"EmbExport.cpt")); + EmbeddedTableDataExporter templateExporter = new EmbeddedTableDataExporter(); + templateExporter.export(outputStream, workbook); + + /**将模板工作薄导出为模板文件*/ + outputStream = new FileOutputStream(new java.io.File(outputUrl+"TmpExport.cpt")); + ((WorkBook) workbook).export(outputStream); + + /**将结果工作薄导出为2003Excel文件*/ + outputStream = new FileOutputStream(new java.io.File(outputUrl+"ExcelExport2003.xls")); + ExcelExporter ExcelExport = new ExcelExporter(); + ExcelExport.export(outputStream, workbook.execute(parameterMap, new WriteActor())); + + /**将结果工作薄导出为2007Excel文件*/ + outputStream = new FileOutputStream(new java.io.File(outputUrl+"ExcelExport2007.xlsx")); StreamExcel2007Exporter ExcelExport1 = new StreamExcel2007Exporter(); ExcelExport1.export(outputStream, workbook.execute(parameterMap, new WriteActor())); -// // 将结果工作薄导出为Word文件 -// outputStream = new FileOutputStream(new File("/Users//susie//Downloads//WordExport.doc")); -// WordExporter WordExport = new WordExporter(); -// WordExport.export(outputStream, workbook.execute(parameterMap, new WriteActor())); -// // 将结果工作薄导出为Pdf文件 -// outputStream = new FileOutputStream(new File("/Users//susie//Downloads//PdfExport.pdf")); -// PDFExporter PdfExport = new PDFExporter(); -// PdfExport.export(outputStream, workbook.execute(parameterMap, new WriteActor())); -// // 将结果工作薄导出为Txt文件(txt文件本身不支持表格、图表等,被导出模板一般为明细表) -// outputStream = new FileOutputStream(new File("/Users//susie//Downloads//TxtExport.txt")); -// TextExporter TxtExport = new TextExporter(); -// TxtExport.export(outputStream, workbook.execute(parameterMap, new WriteActor())); -// // 将结果工作薄导出为Csv文件 -// outputStream = new FileOutputStream(new File("/Users//susie//Downloads//CsvExport.csv")); -// CSVExporter CsvExport = new CSVExporter(); -// CsvExport.export(outputStream, workbook.execute(parameterMap, new WriteActor())); -// //将结果工作薄导出为SVG文件 -// outputStream = new FileOutputStream(new File("/Users//susie//Downloads//SvgExport.svg")); -// SVGExporter SvgExport = new SVGExporter(); -// SvgExport.export(outputStream, workbook.execute(parameterMap, new WriteActor())); -// //将结果工作薄导出为image文件 -// outputStream = new FileOutputStream(new File("/Users//susie//Downloads//PngExport.png")); -// ImageExporter ImageExport = new ImageExporter(); -// ImageExport.export(outputStream, workbook.execute(parameterMap, new WriteActor())); + + /**将结果工作薄导出为Word文件*/ + outputStream = new FileOutputStream(new java.io.File(outputUrl+"WordExport.doc")); + WordExporter WordExport = new WordExporter(); + WordExport.export(outputStream, workbook.execute(parameterMap, new WriteActor())); + + /**将结果工作薄导出为Pdf文件*/ + outputStream = new FileOutputStream(new java.io.File(outputUrl+"PdfExport.pdf")); + PDFExporter PdfExport = new PDFExporter(); + PdfExport.export(outputStream, workbook.execute(parameterMap, new WriteActor())); + + /**将结果工作薄导出为Txt文件(txt文件本身不支持表格、图表等,被导出模板一般为明细表)*/ + outputStream = new FileOutputStream(new java.io.File(outputUrl+"TxtExport.txt")); + TextExporter TxtExport = new TextExporter(); + TxtExport.export(outputStream, workbook.execute(parameterMap, new WriteActor())); + + /**将结果工作薄导出为Csv文件*/ + outputStream = new FileOutputStream(new java.io.File(outputUrl+"CsvExport.csv")); + CSVExporter CsvExport = new CSVExporter(); + CsvExport.export(outputStream, workbook.execute(parameterMap, new WriteActor())); + + /**将结果工作薄导出为SVG文件*/ + outputStream = new FileOutputStream(new java.io.File(outputUrl+"SvgExport.svg")); + SVGExporter SvgExport = new SVGExporter(); + SvgExport.export(outputStream, workbook.execute(parameterMap, new WriteActor())); + + /**将结果工作薄导出为image文件*/ + outputStream = new FileOutputStream(new java.io.File(outputUrl+"PngExport.png")); + ImageExporter ImageExport = new ImageExporter(); + ImageExport.export(outputStream, workbook.execute(parameterMap, new WriteActor())); + outputStream.close(); module.stop(); } catch (Exception e) { @@ -108,4 +129,25 @@ public class ExportApi { SimpleWork.checkOut(); } } -} \ No newline at end of file + + private static void initLog4j() { + + PropertyConfigurator.configure(loadLog4jPropertiesFromJar(Level.toLevel("INFO"))); + } + + private static Properties loadLog4jPropertiesFromJar(Level level) { + + Properties properties = new Properties(); + + System.setProperty("LOG_HOME", System.getProperty("user.dir")); + System.setProperty("LOG_ROOT_LEVEL", level.toString()); + ExtraPatternParserManager.setSystemProperty(); + try { + properties.load(ResourceLoader.getResourceAsStream("/com/fr/general/log/log4j.properties", Log4jConfig.class)); + } catch (IOException ignore) { + //do nothing + } + + return properties; + } +}