ActiveReports 报表应用教程 (16)---报表导出

简介: 葡萄城ActiveReports报表支持多种格式的报表导出,包括PDF、Excel、Word、RTF、HTML、Text、TIFF以及其它图片格式,用户可以将它们应用到Windows Forms、Web、WPF、Silverlight等应用系统中。

葡萄城ActiveReports报表支持多种格式的报表导出,包括PDF、Excel、Word、RTF、HTML、Text、TIFF以及其它图片格式,用户可以将它们应用到Windows Forms、Web、WPF、Silverlight等应用系统中。

在专业版的葡萄城ActiveReports报表里,对PDF格式的数据输出又有了增强功能。现在用户可以将不可见的数字签名或者可见的文字图案加入到报表里。通过多种属性对数字签名进行个性化设置, 用数字签名验证报表作者,还可通过Certification Level 来设定用户访问权限。用时间印章功能建立第三方授权版本。这些新功能完全和Adobe的新安全机制兼容。

本文以客户订单为例演示如何将葡萄城ActiveReports报表导出为各种格式。

1、创建报表文件

在应用程序中创建一个名为 rptInvoice.rdlx 的 ActiveReports 报表文件,使用的项目模板为 ActiveReports 页面报表。

2、打开报表资源管理器,并按照以下信息创建报表数据源

名称: NWind_CHS
类型: Micorsoft OleDb Provider
OLE DB 提供程序: Microsoft.Jet.OLEDB.4.0
服务器或文件名称: Data\NWind_CHS.mdb

 

3、 添加数据集

在新建的 NWind_CHS 数据源上鼠标右键并选择添加数据集菜单项,添加以下两个数据集:

常规-名称:OrderDetails

查询-查询:

SELECT TOP 10 订单.订单ID, 订单.客户ID, 订单.订购日期, 产品.产品名称, 订单明细.数量, 订单明细.单价, 订单明细.折扣, 订单.货主城市, 订单.货主地址, 订单.货主名称, 订单.货主邮政编码, 客户.电话
FROM ((订单 INNER JOIN 订单明细 ON 订单.订单ID = 订单明细.订单ID) INNER JOIN 产品 ON 订单明细.产品ID = 产品.产品ID) INNER JOIN 客户 ON 订单.客户ID = 客户.客户ID
ORDER BY 订单.订购日期 DESC;

 

4、设计报表界面

4.1、选中报表文件,并设置以下属性:

常规-数据集名称: OrderDetails
分组: 名称:FixedPage1_Group
表达式:=[订单ID]

 

4.2、从 VS 中将 Table 控件添加到报表设计界面,并按照以下列表设置相应属性:

表格 属性
DataSetname OrderDetails
FixedSize 19cm*15.75cm
RepeatHeaderOnNewPage True
RepeatToFill True
   
单元格 属性
Cells[2,1] Value:=RowNumber("Table1")
Cells[2,2] Value:=Fields!产品名称.Value
Cells[2,3] Value:=Fields!数量.Value
Cells[2,4] Value:=Fields!单价.Value
Cells[2,5] Value:=Fields!数量.Value * Fields!单价.Value
   
合计单元格 属性
TextBox38 Value:=Sum(Fields!数量.Value * Fields!单价.Value,"FixedPage1_Group")
TextBox42 Value:=ReportItems!TextBox38.Value * 0.17
TextBox39 Value:=ReportItems!TextBox38.Value + ReportItems!TextBox42.Value

 

最终设计界面如下:

ActiveReports 报表功能展示 导出报表

5、添加报表导出功能

5.1、Excel导出代码:

    protected void btnExcel_Click(object sender, EventArgs e)
    {
        GrapeCity.ActiveReports.PageReport _reportDef = new GrapeCity.ActiveReports.PageReport(new System.IO.FileInfo(Server.MapPath("../") + "Reports/" + report + ".rdlx"));
        _reportDef.Report.DataSources[0].DataSourceReference = Server.MapPath("../Data/NWind_CHS_Access.rdsx");
        GrapeCity.ActiveReports.Document.PageDocument _reportRuntime = new GrapeCity.ActiveReports.Document.PageDocument(_reportDef);
 
        GrapeCity.ActiveReports.Export.Excel.Section.XlsExport XlsExport1 = new GrapeCity.ActiveReports.Export.Excel.Section.XlsExport();
 
        System.IO.MemoryStream ms = new System.IO.MemoryStream();
        XlsExport1.FileFormat = GrapeCity.ActiveReports.Export.Excel.Section.FileFormat.Xlsx;
        XlsExport1.Export(_reportRuntime, ms);
 
        Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
        Response.AddHeader("content-disposition", Server.UrlPathEncode("attachment;filename=客户订单.xlsx"));
        Response.BinaryWrite(ms.ToArray());
        Response.End();
    }

 

5.2、Word导出代码:

    protected void btnWord_Click(object sender, EventArgs e)
    {
        GrapeCity.ActiveReports.PageReport _reportDef = new GrapeCity.ActiveReports.PageReport(new System.IO.FileInfo(Server.MapPath("../") + "Reports/" + report + ".rdlx"));
        _reportDef.Report.DataSources[0].DataSourceReference = Server.MapPath("../Data/NWind_CHS_Access.rdsx");
        GrapeCity.ActiveReports.Document.PageDocument _reportRuntime = new GrapeCity.ActiveReports.Document.PageDocument(_reportDef);
 
        GrapeCity.ActiveReports.Export.Word.Page.WordRenderingExtension _renderingExtension = new GrapeCity.ActiveReports.Export.Word.Page.WordRenderingExtension();
        GrapeCity.ActiveReports.Rendering.IO.MemoryStreamProvider _provider = new GrapeCity.ActiveReports.Rendering.IO.MemoryStreamProvider();
        GrapeCity.ActiveReports.Export.Word.Page.Settings s = new GrapeCity.ActiveReports.Export.Word.Page.Settings();
        s.UseMhtOutput = true;
 
        _reportRuntime.Render(_renderingExtension, _provider, s);
 
        Response.ContentType = "application/msword";
        Response.AddHeader("content-disposition", Server.UrlPathEncode("attachment;filename=客户订单.doc"));
        System.IO.MemoryStream ms = new System.IO.MemoryStream();
        _provider.GetPrimaryStream().OpenStream().CopyTo(ms);
        Response.BinaryWrite(ms.ToArray());
        Response.End();
    }

 

5.3、常规PDF导出代码:

    protected void btnPdf_Click(object sender, EventArgs e)
    {
        GrapeCity.ActiveReports.PageReport _reportDef = new GrapeCity.ActiveReports.PageReport(new System.IO.FileInfo(Server.MapPath("../") + "Reports/" + report + ".rdlx"));
        _reportDef.Report.DataSources[0].DataSourceReference = Server.MapPath("../Data/NWind_CHS_Access.rdsx");
        GrapeCity.ActiveReports.Document.PageDocument _reportRuntime = new GrapeCity.ActiveReports.Document.PageDocument(_reportDef);
 
        GrapeCity.ActiveReports.Export.Pdf.Page.PdfRenderingExtension _renderingExtension = new GrapeCity.ActiveReports.Export.Pdf.Page.PdfRenderingExtension();
        GrapeCity.ActiveReports.Rendering.IO.MemoryStreamProvider _provider = new GrapeCity.ActiveReports.Rendering.IO.MemoryStreamProvider();
        _reportRuntime.Render(_renderingExtension, _provider);
 
        Response.ContentType = "application/pdf";
        Response.AddHeader("content-disposition", Server.UrlPathEncode("attachment;filename=客户订单.pdf"));
        System.IO.MemoryStream ms = new System.IO.MemoryStream();
        _provider.GetPrimaryStream().OpenStream().CopyTo(ms);
        Response.BinaryWrite(ms.ToArray());
        Response.End();
    }

 

5.4、HTML导出代码:

    protected void btnHtml_Click(object sender, EventArgs e)
    {
        GrapeCity.ActiveReports.PageReport _reportDef = new GrapeCity.ActiveReports.PageReport(new System.IO.FileInfo(Server.MapPath("../") + "Reports/" + report + ".rdlx"));
        _reportDef.Report.DataSources[0].DataSourceReference = Server.MapPath("../Data/NWind_CHS_Access.rdsx");
        GrapeCity.ActiveReports.Document.PageDocument _reportRuntime = new GrapeCity.ActiveReports.Document.PageDocument(_reportDef);
 
        GrapeCity.ActiveReports.Export.Html.Page.HtmlRenderingExtension _renderingExtension = new GrapeCity.ActiveReports.Export.Html.Page.HtmlRenderingExtension();
        GrapeCity.ActiveReports.Rendering.IO.MemoryStreamProvider _provider = new GrapeCity.ActiveReports.Rendering.IO.MemoryStreamProvider();
        GrapeCity.ActiveReports.Export.Html.Page.Settings s = new GrapeCity.ActiveReports.Export.Html.Page.Settings();
        s.StyleStream = false;
        s.MhtOutput = false;
        s.Fragment = false;
        s.OutputTOC = true;
        s.Mode = GrapeCity.ActiveReports.Export.Html.Page.RenderMode.Paginated;
        _reportRuntime.Render(_renderingExtension, _provider, s);
 
        Response.ContentType = "text/html";
        Response.AddHeader("content-disposition", Server.UrlPathEncode("attachment;filename=客户订单.html"));
        System.IO.MemoryStream ms = new System.IO.MemoryStream();
        _provider.GetPrimaryStream().OpenStream().CopyTo(ms);
        Response.BinaryWrite(ms.ToArray());
        Response.End();
    }

 

5.5、 Text导出代码:

    protected void btnText_Click(object sender, EventArgs e)
    {
        GrapeCity.ActiveReports.PageReport _reportDef = new GrapeCity.ActiveReports.PageReport(new System.IO.FileInfo(Server.MapPath("../") + "Reports/" + report + ".rdlx"));
        _reportDef.Report.DataSources[0].DataSourceReference = Server.MapPath("../Data/NWind_CHS_Access.rdsx");
        GrapeCity.ActiveReports.Document.PageDocument _reportRuntime = new GrapeCity.ActiveReports.Document.PageDocument(_reportDef);
 
        GrapeCity.ActiveReports.Export.Xml.Section.TextExport txtExport1 = new GrapeCity.ActiveReports.Export.Xml.Section.TextExport();
        txtExport1.Encoding = Encoding.Unicode;
 
        System.IO.MemoryStream ms = new System.IO.MemoryStream();
        txtExport1.Export(_reportRuntime, ms);
 
        Response.ContentType = "text/plain";
        Response.AddHeader("content-disposition", Server.UrlPathEncode("attachment;filename=客户订单.txt"));
        Response.BinaryWrite(ms.ToArray());
        Response.End();
    }

 

5.6、CSV导出代码:

    protected void btnCSV_Click(object sender, EventArgs e)
    {
        GrapeCity.ActiveReports.PageReport _reportDef = new GrapeCity.ActiveReports.PageReport(new System.IO.FileInfo(Server.MapPath("../") + "Reports/" + report + ".rdlx"));
        _reportDef.Report.DataSources[0].DataSourceReference = Server.MapPath("../Data/NWind_CHS_Access.rdsx");
        GrapeCity.ActiveReports.Document.PageDocument _reportRuntime = new GrapeCity.ActiveReports.Document.PageDocument(_reportDef);
 
        GrapeCity.ActiveReports.Export.Xml.Section.TextExport csvExport1 = new GrapeCity.ActiveReports.Export.Xml.Section.TextExport();
        csvExport1.Encoding = Encoding.Unicode;
        csvExport1.TextDelimiter = "\t";
        csvExport1.SuppressEmptyLines = true;
 
        System.IO.MemoryStream ms = new System.IO.MemoryStream();
        csvExport1.Export(_reportRuntime, ms);
 
        Response.ContentType = "text/plain";
        Response.AddHeader("content-disposition", Server.UrlPathEncode("attachment;filename=客户订单.csv"));
        Response.BinaryWrite(ms.ToArray());
        Response.End();
    }

 

5.7、高级PDF导出代码:

    protected void btnExport_Click(object sender, EventArgs e)
    {
        GrapeCity.ActiveReports.PageReport report = new GrapeCity.ActiveReports.PageReport(new System.IO.FileInfo(Server.MapPath("../Reports/" + reportname + ".rdlx")));
        report.Report.DataSources[0].DataSourceReference = "";
        report.Report.DataSources[0].ConnectionProperties.DataProvider = "OLEDB";
        report.Report.DataSources[0].ConnectionProperties.ConnectString = string.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};", Server.MapPath("../Data/NWind_CHS.mdb"));
        GrapeCity.ActiveReports.Document.PageDocument document = new GrapeCity.ActiveReports.Document.PageDocument(report);
 
        GrapeCity.ActiveReports.Export.Pdf.Section.PdfExport pdfExport1 = new GrapeCity.ActiveReports.Export.Pdf.Section.PdfExport();
 
        switch (C1Tabs1.Selected)
        {
            case 0:
                break;
            case 1:
                pdfExport1.Security.Encrypt = true;
                pdfExport1.Security.Use128Bit = true;
                if (txtPwd.Text.Length > 0)
                    pdfExport1.Security.UserPassword = txtPwd.Text;
 
                pdfExport1.Security.Permissions = GrapeCity.ActiveReports.Export.Pdf.Section.PdfPermissions.None;
                if (chkCopy.Checked)
                    pdfExport1.Security.Permissions |= GrapeCity.ActiveReports.Export.Pdf.Section.PdfPermissions.AllowCopy;
 
                if (chkEidt1.Checked)
                {
                    pdfExport1.Security.Permissions |= GrapeCity.ActiveReports.Export.Pdf.Section.PdfPermissions.AllowFillIn;
                    pdfExport1.Security.Permissions |= GrapeCity.ActiveReports.Export.Pdf.Section.PdfPermissions.AllowModifyAnnotations;
                    pdfExport1.Security.Permissions |= GrapeCity.ActiveReports.Export.Pdf.Section.PdfPermissions.AllowModifyContents;
                }
 
                if (chkPrint.Checked)
                    pdfExport1.Security.Permissions |= GrapeCity.ActiveReports.Export.Pdf.Section.PdfPermissions.AllowPrint;
                break;
            case 2:
                // ImageText signature.
                pdfExport1.Signature.VisibilityType = GrapeCity.ActiveReports.Export.Pdf.Section.Signing.VisibilityType.ImageText;
 
                // Bounds (Container of Text & Image).
                pdfExport1.Signature.Stamp.Bounds = new RectangleF(0, 0, 4, 1);
 
                // Text area.
                pdfExport1.Signature.Stamp.TextAlignment = GrapeCity.ActiveReports.Export.Pdf.Section.Signing.Alignment.Left;
                pdfExport1.Signature.Stamp.Font = new Font("Comic Sans MS", 8, FontStyle.Regular);
                // Note: Specify (x, y) in relative coordinate from Bounds top-left.
                pdfExport1.Signature.Stamp.TextRectangle = new RectangleF(1, 0, 3, 1);
 
                // Image area.
                pdfExport1.Signature.Stamp.Image = System.Drawing.Image.FromFile(Server.MapPath("../Resources/Grapecity_powertools_bg.png"));
                pdfExport1.Signature.Stamp.ImageAlignment = GrapeCity.ActiveReports.Export.Pdf.Section.Signing.Alignment.Center;
                // Note: Specify (x, y) in relative coordinate from Bounds top-left.
                pdfExport1.Signature.Stamp.ImageRectangle = new RectangleF(0, 0, 1, 1);
 
                // Set certificate & password.
                pdfExport1.Signature.Certificate = new System.Security.Cryptography.X509Certificates.X509Certificate2(Server.MapPath("../Resources/ActiveReports6.pfx"), "123456");
 
                // set the certifiation level
                if (chkEidt2.Checked)
                    pdfExport1.Signature.CertificationLevel = GrapeCity.ActiveReports.Export.Pdf.Section.Signing.CertificationLevel.FormFillingAnnotations;
                else
                    pdfExport1.Signature.CertificationLevel = GrapeCity.ActiveReports.Export.Pdf.Section.Signing.CertificationLevel.NoChangesAllowed;
 
                //Signature items.
                pdfExport1.Signature.Contact = new GrapeCity.ActiveReports.Export.Pdf.Section.Signing.SignatureField<string>(txtEmail.Text, true);
                if (chkDate.Checked)
                    pdfExport1.Signature.SignDate = new GrapeCity.ActiveReports.Export.Pdf.Section.Signing.SignatureField<System.DateTime>(System.DateTime.Now, true);
                break;
            default:
                break;
        }
 
        System.IO.MemoryStream ms = new System.IO.MemoryStream();
        pdfExport1.Export(document,ms);
 
        Response.ContentType = "application/pdf";
        Response.AddHeader("content-disposition", Server.UrlPathEncode("attachment;filename=客户订单.pdf"));
        Response.BinaryWrite(ms.ToArray());
        Response.End();
 
    }

 

在线演示及源码下载地址:


http://www.gcpowertools.com.cn/products/activereports_demo.htm
ActiveReports 报表功能展示

 

相关阅读:

【报表福利大放送】100余套报表模板免费下载 

 矩表 - 现代数据分析中必不可少的报表工具

 

相关文章
|
12月前
|
资源调度 JavaScript 前端开发
vue-element-admin 综合开发一:搭建环境:vue-cli创建项目,整合element、vue-router
这篇文章是关于如何使用vue-cli搭建vue环境,并整合Element UI和vue-router来开发一个基础的前端管理后台界面。
416 0
vue-element-admin 综合开发一:搭建环境:vue-cli创建项目,整合element、vue-router
|
11月前
|
JavaScript 安全 Java
如何使用 Spring Boot 和 Ant Design Pro Vue 构建一个前后端分离的应用框架,实现动态路由和菜单功能
本文介绍了如何使用 Spring Boot 和 Ant Design Pro Vue 构建一个前后端分离的应用框架,实现动态路由和菜单功能。首先,确保开发环境已安装必要的工具,然后创建并配置 Spring Boot 项目,包括添加依赖和配置 Spring Security。接着,创建后端 API 和前端项目,配置动态路由和菜单。最后,运行项目并分享实践心得,帮助开发者提高开发效率和应用的可维护性。
669 2
|
缓存 JavaScript 安全
【Ant Design Pro】使用ant design pro做为你的开发模板(四) 联调正式后台接口与运行时全局配置
【Ant Design Pro】使用ant design pro做为你的开发模板(四) 联调正式后台接口与运行时全局配置
1882 0
【Ant Design Pro】使用ant design pro做为你的开发模板(四) 联调正式后台接口与运行时全局配置
|
JSON API 数据格式
.net core工具组件系列之Autofac—— 第一篇:Autofac系列Autofac的几种常见注册方式、生命周期和AOP
使用Autofac进行服务注册实践:新建三个项目,分别是webapi项目 Wesky.Core.Autofac以及两个类库项目 Wesky.Core.Interface和Wesky.Core.Service。在Webapi项目下,引用Autofac的三个包:Autofac、Autofac.Configuration和Autofac.Extensions.DependencyInjection 。
1839 1
.net core工具组件系列之Autofac—— 第一篇:Autofac系列Autofac的几种常见注册方式、生命周期和AOP
|
存储 API Python
Matplotlib 中文用户指南 3.5 艺术家教程
艺术家教程 原文:Artist tutorial 译者:飞龙 协议:CC BY-NC-SA 4.0 matplotlib API 有三个层级。
1376 0
|
7天前
|
存储 关系型数据库 分布式数据库
PostgreSQL 18 发布,快来 PolarDB 尝鲜!
PostgreSQL 18 发布,PolarDB for PostgreSQL 全面兼容。新版本支持异步I/O、UUIDv7、虚拟生成列、逻辑复制增强及OAuth认证,显著提升性能与安全。PolarDB-PG 18 支持存算分离架构,融合海量弹性存储与极致计算性能,搭配丰富插件生态,为企业提供高效、稳定、灵活的云数据库解决方案,助力企业数字化转型如虎添翼!
|
6天前
|
存储 人工智能 Java
AI 超级智能体全栈项目阶段二:Prompt 优化技巧与学术分析 AI 应用开发实现上下文联系多轮对话
本文讲解 Prompt 基本概念与 10 个优化技巧,结合学术分析 AI 应用的需求分析、设计方案,介绍 Spring AI 中 ChatClient 及 Advisors 的使用。
328 130
AI 超级智能体全栈项目阶段二:Prompt 优化技巧与学术分析 AI 应用开发实现上下文联系多轮对话
|
18天前
|
弹性计算 关系型数据库 微服务
基于 Docker 与 Kubernetes(K3s)的微服务:阿里云生产环境扩容实践
在微服务架构中,如何实现“稳定扩容”与“成本可控”是企业面临的核心挑战。本文结合 Python FastAPI 微服务实战,详解如何基于阿里云基础设施,利用 Docker 封装服务、K3s 实现容器编排,构建生产级微服务架构。内容涵盖容器构建、集群部署、自动扩缩容、可观测性等关键环节,适配阿里云资源特性与服务生态,助力企业打造低成本、高可靠、易扩展的微服务解决方案。
1331 8