使用 C1Report 控件 > 为桌面应用场景开发报表 > 运行时载入报表 |
类似于查看器,在运行时载入报表需要一个报表定义文件。这种类型的程序主要的优点是在修改报表格式后不需要更新程序。只需要的将新的报表定义文件发送给用户,无需其他操作。
要创建一个在运行时载入报表的程序的话,参照以下步骤:
Visual Basic
Visual Basic |
拷贝代码
|
---|---|
Imports C1.C1Report Imports System.IO |
C#
C# |
拷贝代码
|
---|---|
using C1.C1Report; using System.IO; |
这将不需要通过完整的命名空间来引用C1Report和System.IO内的类和对象。
Visual Basic
Visual Basic |
拷贝代码
|
---|---|
' get application path Dim appPath As String appPath = Path.GetDirectoryName(Application.ExecutablePath).ToLower() Dim i As Integer = appPath.IndexOf("/bin") If (i < 0) Then i = appPath.IndexOf("\bin") If (i > 0) Then appPath = appPath.Remove(i, appPath.Length - i) ' get names of reports in the report definition file m_ReportDefinitionFile = appPath & "\Data\Nwind.xml" Dim reports As String() = c1r.GetReportInfo(m_ReportDefinitionFile) ' populate combo box cmbReport.Items.Clear() Dim report As String For Each report In reports cmbReport.Items.Add(report) Next |
C#
C# |
拷贝代码
|
---|---|
// get application path string appPath; appPath = Path.GetDirectoryName(Application.ExecutablePath).ToLower(); int i = appPath.IndexOf("/bin"); if ((i < 0) ) { i = appPath.IndexOf("\bin"); } if ((i > 0) ) { appPath = appPath.Remove(i, appPath.Length - i); } // get names of reports in the report definition file m_ReportDefinitionFile = appPath + "\Data\Nwind.xml"; string ( reports) = c1r.GetReportInfo(m_ReportDefinitionFile); // populate combo box cmbReport.Items.Clear(); string report; foreach report In reports cmbReport.Items.Add(report); } |
代码开头获取包含报表定义的文件的路径。通过系统定义的Path和Application类的静态方法实现。可以调整代码来指向自己的报表定义文件的路径和名字。
然后使用GetReportInfo方法获取报表定义文件(在第一步创建)中包含的所有报表的名字的数组,并且填充到允许用户选择报表的组合框中。
Visual Basic
Visual Basic |
拷贝代码
|
---|---|
Private Sub cmbReport_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles cmbReport.SelectedIndexChanged Try Cursor = Cursors.WaitCursor ' load report status.Text = "Loading " & cmbReport.Text c1r.Load(m_ReportDefinitionFile, cmbReport.Text) ' render into print preview control status.Text = "Rendering " & cmbReport.Text ppv.Document = c1r ' give focus to print preview control ppv.StartPage = 0 ppv.Focus() Finally Cursor = Cursors.Default End Try End Sub |
C#
C# |
拷贝代码
|
---|---|
private void cmbReport_SelectedIndexChanged(object sender, System.EventArgs e) { try { Cursor = Cursors.WaitCursor; // load report status.Text = "Loading " + cmbReport.Text; c1r.Load(m_ReportDefinitionFile, cmbReport.Text); // render into print preview control status.Text = "Rendering " + cmbReport.Text; ppv.Document = c1r; // give focus to print preview control ppv.StartPage = 0; ppv.Focus(); } finally { Cursor = Cursors.Default; } } |