反饋已提交
網絡繁忙
在匯出章節中我們介紹了 Excel 匯出方式 ExcelExporter,此為原樣匯出。在 FineReport 中,匯出 Excel 還有分頁匯出、分頁分 sheet 匯出、大數據量匯出三種匯出方式,那通程式式匯出時,這三種該如何實現呢?
1)原樣匯出 Excel2003
//原樣匯出excel2003 outputStream = new FileOutputStream(new File("C:\\test\\ExcelExport.xls")); ExcelExporter excel = new ExcelExporter(); excel.setVersion(true); excel.export(outputStream, workbook.execute(parameterMap,new WriteActor()));
2)原樣匯出 Excel2007
//原樣匯出excel2007 outputStream = new FileOutputStream(new File("C:\\test\\ExcelExport.xlsx")); StreamExcel2007Exporter excel1 = new StreamExcel2007Exporter(); excel.export(outputStream, workbook.execute(parameterMap,new WriteActor()));
3)分頁匯出 Excel2003
//分頁匯出excel2003 outputStream = new FileOutputStream(new File("C:\\test\\PageExcelExport.xls")); PageExcelExporter page = new PageExcelExporter(ReportUtils.getPaperSettingListFromWorkBook(workbook.execute(parameterMap,new WriteActor()))); page.setVersion(true); page.export(outputStream, workbook.execute(parameterMap,new WriteActor()));
4)分頁匯出 Excel2007
//分頁匯出excel2007 outputStream = new FileOutputStream(new File("C:\\test\\PageExcelExport.xlsx")); PageExcel2007Exporter page1 = new PageExcel2007Exporter(ReportUtils.getPaperSettingListFromWorkBook(rworkbook)); page1.export(outputStream, workbook.execute(parameterMap,new WriteActor()));
5)分頁分 sheet 匯出 Excel2003
//分頁分sheet匯出excel2003 outputStream = new FileOutputStream(new File("C:\\test\\PageSheetExcelExport.xls")); PageToSheetExcelExporter sheet = new PageToSheetExcelExporter(ReportUtils.getPaperSettingListFromWorkBook(workbook.execute(parameterMap,new WriteActor()))); sheet.setVersion(true); sheet.export(outputStream, workbook.execute(parameterMap,new WriteActor()));
6)分頁分 sheet 匯出 Excel2007
//分頁分sheet匯出excel2007 outputStream = new FileOutputStream(new File("C:\\test\\PageSheetExcelExport.xlsx")); PageToSheetExcel2007Exporter sheet1 = new PageToSheetExcel2007Exporter(ReportUtils.getPaperSettingListFromWorkBook(rworkbook)); sheet1.export(outputStream, workbook.execute(parameterMap,new WriteActor()));
7)大數據量匯出(大數據量匯出預設是 65512 行的 .xls 組成的壓縮包)
//大數據量匯出 outputStream = new FileOutputStream(new File("C:\\test\\LargeExcelExport.zip")); LargeDataPageExcelExporter large = new LargeDataPageExcelExporter(ReportUtils.getPaperSettingListFromWorkBook(workbook.execute(parameterMap,new WriteActor())), true); //匯出2007版outputStream = new FileOutputStream(new File("C:\\test\\LargeExcelExport.xlsx")); LargeDataPageExcel2007Exporter large = new LargeDataPageExcel2007Exporter(ReportUtils.getPaperSettingListFromWorkBook(rworkbook), true); large.export(outputStream, workbook.execute(parameterMap,new WriteActor()));
下面實現後台匯出 Parameter.cpt 範本為以上各種格式的 Excel。
編譯程式前,需先建立一個 Java 工程環境,並且需要一個 Java 編輯器,如 Eclipse 或 idea 。
在編輯器工程中引入 FineReport 工程 JAR 包。包括安裝的報表工程
%FR_HOME%\webapps\webroot\WEB-INF\lib目錄下 fine 開頭的 12 個 JAR 包。
%FR_HOME%\webapps\webroot\WEB-INF\lib目錄下的 sqlite-jdbc.jar
%Tomcat_HOME%\libTomcat目錄下的 servlet-api.jar
%JAVA_HOME%\jdk\lib JDK 目錄下的 tools.jar
slf4j-simple-1.7.25.jar ,點選可下載:slf4j-simple-1.7.25.rar
如果報表中需要查詢資料庫,還需要匯入對應的 jdbc 驅動或者插件的 JAR 包(如使用 JSON 資料集,就要引入 JSON 資料集插件下的 JAR 包)。
詳細引入程式可參考:編譯Java程式
在編輯器中編寫 Java 程式 ExportExcel.java,即引入必要類後,獲取範本,將範本匯出成指定格式的檔案。完整程式碼可參見:
注:使用者使用時,注意將範例程式碼中的工程路徑、範本名稱和匯出路徑取代為使用者自己工程下的。
package com.fr.io;import com.fr.base.Parameter;import com.fr.base.operator.common.CommonOperator;import com.fr.chart.activator.ChartBaseActivator;import com.fr.cluster.engine.activator.standalone.StandaloneModeActivator;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.health.activator.ModuleHealActivator;import com.fr.io.exporter.ExcelExporter;import com.fr.io.exporter.LargeDataPageExcelExporter;import com.fr.io.exporter.PageExcel2007Exporter;import com.fr.io.exporter.PageExcelExporter;import com.fr.io.exporter.PageToSheetExcel2007Exporter;import com.fr.io.exporter.PageToSheetExcelExporter;import com.fr.io.exporter.excel.stream.StreamExcel2007Exporter;import com.fr.main.TemplateWorkBook;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;import com.fr.report.core.ReportUtils;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.store.StateServiceActivator;import com.fr.workspace.simple.SimpleWork;import java.io.File;import java.io.FileOutputStream;public class ExportExcel { public static void main(String[] args) { // 首先需要定義執行所在的環境,這樣才能正確讀取資料庫資訊 // 定義報表運作環境,用於執行報表 Module module = ActivatorToolBox.simpleLink(new BaseDBActivator(), new ConfigurationActivator(), new ResourceRepositoryActivator(), new StandaloneModeActivator(), new ModuleHealActivator(), new StateServiceActivator(), new ChartBaseActivator(), new SchedulerActivator(), new ReportBaseActivator(), new RestrictionActivator(), new ReportActivator(), new WriteActivator()); SimpleWork.supply(CommonOperator.class, new CommonOperatorImpl()); String envpath = "//Applications//FineReport10_325//webapps//webroot//WEB-INF";//工程路徑 SimpleWork.checkIn(envpath); I18nResource.getInstance(); module.start(); ResultWorkBook rworkbook = null; try { // 未執行範本工作薄 TemplateWorkBook workbook = TemplateWorkBookIO.readTemplateWorkBook("//doctw//Primary//Parameter//範本參數.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()); } // 定義匯出流 FileOutputStream outputStream; //原樣匯出excel2003 outputStream = new FileOutputStream(new File("//Users//susie//Downloads//ExcelExport.xls")); ExcelExporter excel = new ExcelExporter(); excel.setVersion(true); excel.export(outputStream, workbook.execute(parameterMap,new WriteActor())); //原樣匯出excel2007 outputStream = new FileOutputStream(new File("//Users//susie//Downloads//ExcelExport.xlsx")); StreamExcel2007Exporter excel1 = new StreamExcel2007Exporter(); excel.export(outputStream, workbook.execute(parameterMap,new WriteActor())); //分頁匯出excel2003 outputStream = new FileOutputStream(new File("//Users//susie//Downloads//PageExcelExport.xls")); PageExcelExporter page = new PageExcelExporter(ReportUtils.getPaperSettingListFromWorkBook(workbook.execute(parameterMap,new WriteActor()))); page.setVersion(true); page.export(outputStream, workbook.execute(parameterMap,new WriteActor())); //分頁匯出excel2007 outputStream = new FileOutputStream(new File("//Users//susie//Downloads//PageExcelExport.xlsx")); PageExcel2007Exporter page1 = new PageExcel2007Exporter(ReportUtils.getPaperSettingListFromWorkBook(rworkbook)); page1.export(outputStream, workbook.execute(parameterMap,new WriteActor())); //分頁分sheet匯出excel2003 outputStream = new FileOutputStream(new File("//Users//susie//Downloads//PageSheetExcelExport.xls")); PageToSheetExcelExporter sheet = new PageToSheetExcelExporter(ReportUtils.getPaperSettingListFromWorkBook(workbook.execute(parameterMap,new WriteActor()))); sheet.setVersion(true); sheet.export(outputStream, workbook.execute(parameterMap,new WriteActor())); //分頁分sheet匯出excel2007 outputStream = new FileOutputStream(new File("//Users//susie//Downloads//PageSheetExcelExport.xlsx")); PageToSheetExcel2007Exporter sheet1 = new PageToSheetExcel2007Exporter(ReportUtils.getPaperSettingListFromWorkBook(rworkbook)); sheet1.export(outputStream, workbook.execute(parameterMap,new WriteActor())); //大數據量匯出 outputStream = new FileOutputStream(new File("//Users//susie//Downloads//LargeExcelExport.zip")); LargeDataPageExcelExporter large = new LargeDataPageExcelExporter(ReportUtils.getPaperSettingListFromWorkBook(workbook.execute(parameterMap,new WriteActor())), true); //匯出2007版outputStream = new FileOutputStream(new File("//Users//susie//Downloads//LargeExcelExport.xlsx")); excel LargeDataPageExcel2007Exporter large = new LargeDataPageExcel2007Exporter(ReportUtils.getPaperSettingListFromWorkBook(rworkbook), true); large.export(outputStream, workbook.execute(parameterMap,new WriteActor())); outputStream.close(); module.stop(); } catch (Exception e) { e.printStackTrace(); } }}
Java 程式編寫完成後,在編譯器中編譯 ExportExcel.java ,編譯透過後,就會在程式碼中匯出路徑的資料夾下生成不同格式的檔案,這樣就匯出成功了。如下圖所示:
滑鼠選中內容,快速回饋問題
滑鼠選中存在疑惑的內容,即可快速回饋問題,我們將會跟進處理。
不再提示
10s後關閉
反馈已提交
网络繁忙