Excel导出的多种方式

编辑
文档创建者:印然 (67260 )     浏览次数:6192次     编辑次数:18次     最近更新:jiangsr 于 2018-08-30     

目录:

1. 描述编辑

在导出章节中我们介绍了Excel导出方式ExcelExporter,此为原样导出。若是您先前学习过FineReport学习教程,您会知道,在FineReport中,除原样导出外还有另外三种导出方式:分页导出、分页分sheet导出、大数据量导出。在程序中有不同的接口来实现:

1.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()));
1.2 原样导出excel2007
//原样导出excel2007 outputStream = new FileOutputStream(new File("C:\\test\\ExcelExport.xlsx")); StreamExcel2007Exporter excel1 = new StreamExcel2007Exporter(); excel.export(outputStream, workbook.execute(parameterMap,new WriteActor()));
1.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()));

1.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()));

1.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()));
1.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()));

1.7 大数据量导出,50000行为一个excel文件
//大数据量导出 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()));

2. 示例编辑

2.1 完整可执行代码
package com.fr.io; import java.io.File; import java.io.FileOutputStream; import com.fr.base.FRContext; import com.fr.general.ModuleContext; import com.fr.base.Parameter; import com.fr.dav.LocalEnv; import com.fr.io.exporter.Excel2007Exporter; 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.impl.WorkBook; import com.fr.main.workbook.ResultWorkBook; import com.fr.report.core.ReportUtils; import com.fr.report.module.EngineModule; import com.fr.stable.WriteActor; public class ExportExcel { public static void main(String[] args) { // 定义报表运行环境,才能执行报表 String envpath = "C:\\FineReport_9.0\\WebReport\\WEB-INF"; FRContext.setCurrentEnv(new LocalEnv(envpath)); ModuleContext.startModule(EngineModule.class.getName()); ResultWorkBook rworkbook = null; try { // 未执行模板工作薄 WorkBook workbook = (WorkBook) TemplateWorkBookIO .readTemplateWorkBook(FRContext.getCurrentEnv(), "\\doc\\Primary\\Parameter\\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("C:\\test\\ExcelExport.xls")); ExcelExporter excel = new ExcelExporter(); excel.setVersion(true); excel.export(outputStream, workbook.execute(parameterMap,new WriteActor())); //原样导出excel2007 outputStream = new FileOutputStream(new File("C:\\test\\ExcelExport.xlsx")); StreamExcel2007Exporter excel1 = new StreamExcel2007Exporter(); excel.export(outputStream, workbook.execute(parameterMap,new WriteActor())); //分页导出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())); //分页导出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())); //分页分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())); //分页分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())); //大数据量导出 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")); excel LargeDataPageExcel2007Exporter large = new LargeDataPageExcel2007Exporter(ReportUtils.getPaperSettingListFromWorkBook(rworkbook), true); large.export(outputStream, workbook.execute(parameterMap,new WriteActor())); outputStream.close(); ModuleContext.stopModules(); } catch (Exception e) { e.printStackTrace(); } } }

编译运行该代码后,就会在E盘下生成不同格式的文件,这样就导出成功了,如下图:

222

附件列表


主题: 二次开发
如果您认为本文档还有待完善,请编辑

文档内容仅供参考,如果你需要获取更多帮助,付费/准付费客户请咨询帆软技术支持
关于技术问题,您还可以前往帆软社区,点击顶部搜索框旁边的提问按钮
若您还有其他非技术类问题,可以联系帆软传说哥(qq:1745114201

此页面有帮助吗?