在运行时加载报表需要报表定义文件和查看器。这种类型的应用程序的主要优点是,如果您修改报表格式,则无需更新应用程序。只需将新的报表定义文件发送给用户即可。
要创建一个在运行时加载报表的应用程序,请按照下列步骤操作:
- 在C1FlexReportDesigner应用程序中创建所有必需的报表。
- 将以下控件添加到应用程序:
- 名为c1FlexReport1的C1FlexReport组件
- 名为fv的C1FlexViewer 控件
- 名为cmbReport的ComboBox控件
- 名为button1的Button控件
将以下Import语句添加到文件顶部:
CSusing C1.Win.FlexReport; using System.IO;
这样可以直接引用C1FlexReport和System.IO类和对象,而不必指定完整的名称空间。
在按钮单击事件中添加以下代码,以读取报表定义文件并构建所有报表的列表:
CS// 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\Products Report.flxr"; string[] reports = C1FlexReport.GetReportList(m_ReportDefinitionFile); // populate combo box cmbReport.Items.Clear(); foreach (string report in reports) { cmbReport.Items.Add(report); }
该代码从获取包含报表定义的文件的位置开始。这是使用系统定义的Path和Application类中的静态方法完成的。您可能必须调整代码以反映报表定义文件的位置和名称。
然后,它使用GetReportList方法检索包含报表定义文件中所有报表名称的数组(在步骤1中创建),并填充组合框,允许用户选择报表。
添加代码以呈现用户选择的报表。例如:
CStry { Cursor = Cursors.WaitCursor; // load report fv.StatusText = "Loading" + cmbReport.Text; c1FlexReport1.Load(m_ReportDefinitionFile, cmbReport.Text); // render into print preview control fv.StatusText = "Rendering" + cmbReport.Text; fv.DocumentSource = c1FlexReport1; // give focus to print preview control fv.Focus(); } finally { Cursor = Cursors.Default; }
- 运行项目。