使用Crystal Reports for ASP.NET创建报表 [英] Creating Reports using Crystal Reports for ASP.NET

查看:49
本文介绍了使用Crystal Reports for ASP.NET创建报表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面的代码适用于高级ASP.NET开发人员步骤:

Below codes is for advance ASP.NET developers Steps:

(如果你有更方便的方式在这里发布,请尽可能...)

(Pleae if you have easer way post it here...)

#1创建XSD文件并将所有字段元素添加为属性:

 #1 Creat XSD file and add all fields elements as attribute:

<? xml version ="1.0" encoding < font color ="#0000ff"size = 2> ="utf-8" ? >

<?xml version="1.0" encoding="utf-8" ?>

< xs:schema id ="Intake_DataSet" targetNamespace ="http://tempuri.org/Intake_DataSet.xsd" < font color ="#ff0000"size = 2> elementFormDefault ="qualified"

<xs:schema id="Intake_DataSet" targetNamespace="http://tempuri.org/Intake_DataSet.xsd" elementFormDefault="qualified"

attributeFormDefault ="qualified" xmlns ="http:/ /tempuri.org/Intake_DataSet.xsd\" xmlns mstns ="http:// tempuri。 org / Intake_DataSet.xsd"

attributeFormDefault="qualified" xmlns="http://tempuri.org/Intake_DataSet.xsd" xmlns:mstns="http://tempuri.org/Intake_DataSet.xsd"

xmlns xs ="http://www.w3.org/2001/XMLSchema" xmlns msdata ="urn:schemas-microsoft-com:xml-msdata">

xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">

< xs:元素 name ="InvoiceDataSet" msdata IsDataSet ="true">

<xs:element name="InvoiceDataSet" msdata:IsDataSet="true">

< xs: complexType >

<xs:complexType>


< xs:choice maxOccurs ="unbounded">

<xs:choice maxOccurs="unbounded">

< ; xs:element 名称 ="InvoiceReport">

<xs:element name="InvoiceReport">


< xs:complexType >

<xs:complexType>


< font color ="#0000ff"size = 2>< xs:sequence > < / xs:sequence >

<xs:sequence></xs:sequence>

< xs:attribute name ="Clien tID" type ="xs:integer" />

<xs:attribute name="ClientID" type="xs:integer" />

< xs:attribute name = "AppDate" type ="xs:date" />

<xs:attribute name="AppDate" type="xs:date" />

< xs:属性 name ="ClientUniqueIDType" type ="xs:string" /> ;

<xs:attribute name="ClientUniqueIDType" type="xs:string" />

< xs:attribute name ="ClientUniqueID" type < font color ="#0000ff"size = 2> ="xs:string" / >

<xs:attribute name="ClientUniqueID" type="xs:string" />

< xs:attribute name ="LastName" type ="xs:string" />

<xs:attribute name="LastName" type="xs:string" />

< xs:attribute name ="FirstName" type ="xs:string" />

<xs:attribute name="FirstName" type="xs:string" />

< xs:attribute name ="OptionalName" type ="xs:string" />

<xs:attribute name="OptionalName" type="xs:string" />

< <字体color ="#800000"size = 2> xs:attribute name ="AddressLine1" type ="xs:string" />

<xs:attribute name="AddressLine1" type="xs:string" />

< < font color ="#800000"size = 2> xs:attribute name ="AddressLine2" type ="xs:string" />

<xs:attribute name="AddressLine2" type="xs:string" />

< xs:attribute name ="City" type ="xs:string" />

<xs:attribute name="City" type="xs:string" />

< xs:attribute name ="PostalCode" type ="xs:string" />

<xs:attribute name="PostalCode" type="xs:string" />

< xs:attribute 名称 ="省" type ="xs:string" />

<xs:attribute name="Province" type="xs:string" />


< / xs:complexType >

</xs:complexType>

< / xs:element >

</xs:element>

< / xs:choice >

</xs:choice>

< / xs:complexType >

</xs:complexType>

< / xs:element >

</xs:element>

< / xs:schema >

#2构建报告并使用xsd作为其数据源

#3将sql函数写入文件和oCmd并用结果填充数据集


Dim oDS As New DataSet,oDA As SqlDataAdapter

Dim oDS As New DataSet, oDA As New SqlDataAdapter

Dim oConn As SqlConnection,oCmd As SqlCommand = New SqlCommand

oConn = SqlConnection(_connString)'你知道吗

oConn = New SqlConnection(_connString) ' you know this

oCmd.Connection = oConn

oCmd.Connection = oConn

sSQL ="从客户中选择*".... blah blah blah

sSQL="Select * from Clients" ....blah blah blah

oCmd.CommandText = sSQL

oCmd.CommandText = sSQL

oDA.SelectCommand = oCmd

oDA.SelectCommand = oCmd

oDA.Fill(oDS,"InvoiceReport")

oDA.Fill(oDS, "InvoiceReport")


#4然后ShowReport()

#4 Then ShowReport()


ShowReport(ddlReport.SelectedItem.Value,ddlReportFormat.SelectedItem.Value,oDS)

ShowReport(ddlReport.SelectedItem.Value, ddlReportFormat.SelectedItem.Value, oDS)


私人 Sub ShowReport( ByVal sReport As String ByVal sFormat As 字符串 ByVal DS As DataSet)

Private Sub ShowReport(ByVal sReport As String, ByVal sFormat As String, ByVal DS As DataSet)


Dim sExt As 字符串 ,sContentType As String ,oExportFormatType As ExportFormatType

Dim sExt As String, sContentType As String, oExportFormatType As ExportFormatType

选择 案例 sFormat.ToUpper

Select Case sFormat.ToUpper


Case "PDF"

Case "PDF"


sExt = ".pdf"

sExt = ".pdf"

sContentType ="application / pdf"

sContentType = "application/pdf"

oExportFormatType = ExportFormatType.PortableDocFormat

oExportFormatType = ExportFormatType.PortableDocFormat


Case "MSWORD"

Case "MSWORD"


sExt ="。doc"

sExt = ".doc"

sContentType ="application / msword"

sContentType = "application/msword"

oExportFormatType = ExportFormatType.WordForWindows

oExportFormatType = ExportFormatType.WordForWindows

案例 "MSEXCEL"

Case "MSEXCEL"


sExt =" .xls"

sExt = ".xls"

sContentType ="application / vnd.ms-excel"

sContentType = "application/vnd.ms-excel"

oExportFormatType = ExportFormatType.Excel

oExportFormatType = ExportFormatType.Excel

案例 "RTF"

Case "RTF"


sExt ="。rtf"

sExt = ".rtf"

sContentType ="application / msword"

sContentType = "application/msword"

oExportFormatType = ExportFormatType.RichText

oExportFormatType = ExportFormatType.RichText

Case Else

Case Else


返回

Return


结束 选择

End Select

Dim crRptDoc As ReportDocument

Dim crRptDoc As New ReportDocument

crRptDoc.Load(Request.PhysicalApplicationPath.ToLower& amp; ; "举报"& sReport)

crRptDoc.Load(Request.PhysicalApplicationPath.ToLower & "Report\" & sReport)

'crRptDoc.Database.Tables(0).SetDataSource(DS)

'crRptDoc.Database.Tables(0).SetDataSource(DS)

'crRptDoc.Database.Tables.Item("Supplier")。SetDataSource(DS)

'crRptDoc.Database.Tables.Item("Supplier").SetDataSource(DS)

crRptDoc.SetDataSource(DS)

crRptDoc.SetDataSource(DS)

Dim crExportOptions As ExportOptions = crRptDoc.ExportOptions

Dim crExportOptions As ExportOptions = crRptDoc.ExportOptions

Dim crDiskFileDestinationOptions 作为 DiskFileDestinationOptions

Dim crDiskFileDestinationOptions As New DiskFileDestinationOptions

Dim sFileName As String = Request.PhysicalApplicationPath.ToLower& "举报"& Session.SessionID.ToString& sExt

Dim sFileName As String = Request.PhysicalApplicationPath.ToLower & "Report\" & Session.SessionID.ToString & sExt

crDiskFileDestinationOptions.DiskFileName = sFileName

crDiskFileDestinationOptions.DiskFileName = sFileName

使用 < font size = 2> crExportOptions

With crExportOptions


.DestinationOptions = crDiskFileDestinationOptions

.DestinationOptions = crDiskFileDestinationOptions

.ExportDestinationType = ExportDestinationType.DiskFile

.ExportDestinationType = ExportDestinationType.DiskFile

.ExportFormatType = oExportFormatType

.ExportFormatType = oExportFormatType

结束 使用

End With

crRptDoc.Export()

crRptDoc.Export()

Response.ClearContent()

Response.ClearContent()

Response.ClearHeaders()

Response.ClearHeaders()

Response.ContentType = sContentType

Response.ContentType = sContentType

Response.WriteFile(sFileName)

Response.WriteFile(sFileName)

Response.Flush()

Response.Flush()

回复.Close()

System.IO.File.Delete(sFileName) '从磁盘中删除导出的文件

System.IO.File.Delete(sFileName) 'delete the exported file from disk

crRptDoc = Nothing

crRptDoc = Nothing

结束 Sub

End Sub

推荐答案

您可以创建数据集,而不是手动创建模式,并使用WriteXML("c:\ mydata.xml",XmlWriteMode.WriteSchema),这将为您创建。

Instead of creating the schema manually, you can create the dataset, and use the WriteXML("c:\mydata.xml", XmlWriteMode.WriteSchema), this will create it for you.

您可以执行导出流式传输并将其流式传输到浏览器,而不是导出到磁盘,保存所有文件。

Instead of exporting to disk, you can do an export to stream and stream that to the browser, saves all the file io.

Dim oStream As MemoryStream'Imports System.IO
oStream = CType(crReport.ExportToStream(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat),MemoryStream)
Response.Clear()
Response.Buffer = True < br> Response.ContentType =" application / pdf"
Response.BinaryWrite(oStream .ToArray())
Response.End()

Dim oStream As MemoryStream 'Imports System.IO
oStream = CType(crReport.ExportToStream(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat), MemoryStream)
Response.Clear()
Response.Buffer = True
Response.ContentType = "application/pdf"
Response.BinaryWrite(oStream.ToArray())
Response.End()


这篇关于使用Crystal Reports for ASP.NET创建报表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆