ActiveReports 报表应用教程 (16)

Wesley13
• 阅读 717

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

查询-查询:

SELECTTOP 10 订单.订单ID, 订单.客户ID, 订单.订购日期, 产品.产品名称, 订单明细.数量, 订单明细.单价, 订单明细.折扣, 订单.货主城市, 订单.货主地址, 订单.货主名称, 订单.货主邮政编码, 客户.电话

FROM ((订单 INNERJOIN 订单明细 ON 订单.订单ID = 订单明细.订单ID) INNERJOIN 产品 ON 订单明细.产品ID = 产品.产品ID) INNERJOIN 客户 ON 订单.客户ID = 客户.客户ID

ORDERBY 订单.订购日期 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 报表应用教程 (16)

5、添加报表导出功能

5.1、Excel导出代码:

protectedvoid 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导出代码:

protectedvoid 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导出代码:

protectedvoid 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导出代码:

protectedvoid 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导出代码:

protectedvoid 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导出代码:

protectedvoid 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导出代码:

protectedvoid 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 报表应用教程 (16)

本文出自 “葡萄城控件博客” 博客,请务必保留此出处http://powertoolsteam.blog.51cto.com/2369428/1255874

点赞
收藏
评论区
推荐文章
blmius blmius
2年前
MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1
文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s
Karen110 Karen110
2年前
一篇文章带你了解JavaScript日期
日期对象允许您使用日期(年、月、日、小时、分钟、秒和毫秒)。一、JavaScript的日期格式一个JavaScript日期可以写为一个字符串:ThuFeb02201909:59:51GMT0800(中国标准时间)或者是一个数字:1486000791164写数字的日期,指定的毫秒数自1970年1月1日00:00:00到现在。1\.显示日期使用
Jacquelyn38 Jacquelyn38
2年前
2020年前端实用代码段,为你的工作保驾护航
有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )
皕杰报表之UUID
​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为
皕杰报表(关于日期时间时分秒显示不出来)
在使用皕杰报表设计器时,数据据里面是日期型,但当你web预览时候,发现有日期时间类型的数据时分秒显示不出来,只有年月日能显示出来,时分秒显示为0:00:00。1.可以使用tochar解决,数据集用selecttochar(flowdate,"yyyyMMddHH:mm:ss")fromtablename2.也可以把数据库日期类型date改成timestamp
Stella981 Stella981
2年前
Django中Admin中的一些参数配置
设置在列表中显示的字段,id为django模型默认的主键list_display('id','name','sex','profession','email','qq','phone','status','create_time')设置在列表可编辑字段list_editable
Wesley13 Wesley13
2年前
ActiveReports 报表应用教程 (1)
在开始专题内容之前,我们还是了解一下ActiveReports是一款什么产品:ActiveReports是一款在全球范围内应用非常广泛的报表控件,以提供.NET报表所需的全部报表设计功能领先于同类报表控件,包括对交互式报表的强大支持、丰富的数据可视化形式、与VisualStudio的完美集成、以及对WPF/WinForm/ASP.NET/S
Wesley13 Wesley13
2年前
MySQL部分从库上面因为大量的临时表tmp_table造成慢查询
背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_
子桓 子桓
9个月前
mac电脑版Acrobat Pro DC 2021 安装 Acrobat Pro DC 2021 中文安装教程
AcrobatProDC2021支持多种文件格式的转换,包括Word、Excel、PowerPoint、HTML、图像等,用户可以将这些文件转换成PDF格式,以便于共享和分发。同时,该软件还支持OCR(光学字符识别)技术,可以将扫描的纸质文档转换成可编辑的
Python进阶者 Python进阶者
3个月前
Excel中这日期老是出来00:00:00,怎么用Pandas把这个去除
大家好,我是皮皮。一、前言前几天在Python白银交流群【上海新年人】问了一个Pandas数据筛选的问题。问题如下:这日期老是出来00:00:00,怎么把这个去除。二、实现过程后来【论草莓如何成为冻干莓】给了一个思路和代码如下:pd.toexcel之前把这