最近折騰采用第三方報表控件(DevExpress),開始采用的是 文件(.repx)讀取的方式進行報表設計器的加載。實現方式如下:

不設置文件復制到輸出目錄,是無法加載到該報表模塊文件的。
最近折騰采用第三方報表控件(DevExpress),開始采用的是 文件(.repx)讀取的方式進行報表設計器的加載。實現方式如下:

不設置文件復制到輸出目錄,是無法加載到該報表模塊文件的。
1 //獲取datatable數據
2 CreateDataTable();
3 DataSet ds = new DataSet();
4 ds.Tables.Add(dt);
5 //加載數據,用戶自定義報表格式
6 DevExpress.XtraReports.UI.XtraReport report = new DevExpress.XtraReports.UI.XtraReport();
7 report.LoadLayout(Application.StartupPath + @"\ReportFile\ReporTemplate.repx");
8 report.DataSource = ds;
9 report.ShowDesignerDialog();
10 report.Dispose();
做到這里表面上看,基本功能很簡單的就實現了,但是我們應該考慮一個問題就是,用戶在設置后,我們怎樣保存,其實這個也比較簡單,采用
1 report.SaveLayout(Application.StartupPath + @"\ReportFile\ReporTemplate1.repx");
保存過程中,注意文件名不能重復,因為本身的模版正在使用中,無法覆蓋的。
基本功能就說到這里。說說這里面的問題吧, 用戶保存模塊文件過多,使用起來不方便,無疑造成用戶或者服務器端文件不斷增大承載比較重,直到一天崩潰為止。下面說說,通過模版二進制流的方式來完成該功能無疑是最佳選擇。
設計思路:
1、數據庫中設計一個表,來存儲采用二進制byte生成的文件。最好是兩個一個是初始化模版,一個是用戶設計后的模版。
1 try
2 {
3 string filepath = Application.StartupPath.ToString();
4 FileStream file = new FileStream(filepath + @"\ReportFile\ReporTemplate.repx", FileMode.Open, FileAccess.ReadWrite);
5 byte[] a = new byte[file.Length];
6 file.Read(a, 0, (int)file.Length);
7 //這里是我將二進制轉換成文本顯示的
8 txtContent.Text = Convert.ToBase64String(a);
9 }
10 catch (Exception)
11 {
12 throw;
13 }
這樣你就可以獲取初始化的模版文件的二進制模版數據,保存到初始化的數據字段中,以后就不用這個了。
2、讀取報表二進制數據,加載到模版報表中。如下:
1 using System;
2 using System.Collections.Generic;
3 using System.ComponentModel;
4 using System.Data;
5 using System.Drawing;
6 using System.Text;
7 using System.Windows.Forms;
8 using DevExpress.XtraEditors;
9 using System.IO;
10 using System.Linq;
11 using DevExpress.XtraReports.UI;
12 using DevExpress.XtraReports.UserDesigner;
13 using System.Drawing.Design;
14 using System.ComponentModel.Design;
15 using DevExpress.XtraReports;
1 XtraReport r;
2 private void btnRead_Click(object sender, EventArgs e)
3 {
4 r = new XtraReport();
5 //二進制流讀取轉換
6 byte[] ff=Convert.FromBase64String(txtContent.Text.Trim());
7 System.IO.MemoryStream ms = new MemoryStream(ff);
8 r.LoadLayout(ms);
9
10 XRDesignFormEx designForm = new XRDesignFormEx();
11
12 //隱藏按鈕 這段
13 //designForm.DesignPanel.SetCommandVisibility(new ReportCommand[]{
14 // ReportCommand.NewReport,
15 // ReportCommand.SaveFileAs,
16 // ReportCommand.NewReportWizard,
17 // ReportCommand.OpenFile
18 //},new DevExpress.XtraReports.UserDesigner.c CommandVisibility.None);
19
20
21
22 //更改狀態
23 designForm.ReportStateChanged += new ReportStateEventHandler(designForm_ReportStateChanged);
24 designForm.FormClosing += new FormClosingEventHandler(designForm_FormClosing);
25 // 加載報表.
26 designForm.OpenReport(r);
27 // 打開設計器
28 designForm.ShowDialog();
29 designForm.Dispose();
30 }
31
32 void designForm_FormClosing(object sender, FormClosingEventArgs e)
33 {
34 //在此處處理關閉設計器時的操作,主要用來自定義保存數據
35
36 System.IO.MemoryStream ms = new MemoryStream();
37 r.SaveLayout(ms);
38 byte[] fff = ms.ToArray();
39 txtContent.Text = Convert.ToBase64String(fff);
40 }
41
42 void designForm_ReportStateChanged(object sender, ReportStateEventArgs e)
43 {
44 //只要報表發生改變就立即將狀態設置為保存
45 //避免系統默認保存對話框的出現
46 if (e.ReportState == ReportState.Changed)
47 {
48 ((XRDesignFormEx)sender).DesignPanel.ReportState = ReportState.Saved;
49 }
50 }
實現以上代碼需要引用 DevExpress.XtraReports.v11.1.Design、DevExpress.XtraReports.v11.1.Extensions、DevExpress.XtraReports.v11.1 就可以了。
通過以上代碼就可以實現了。多說一句,以下代碼中,通過System.IO.MemoryStream 內存流的方式獲取報表設計界面更改后的二進制數據模版的狀態只。
1 r = new XtraReport();
2 //二進制流讀取轉換
3 byte[] ff=Convert.FromBase64String(txtContent.Text.Trim());
4 System.IO.MemoryStream ms = new MemoryStream(ff);
5 r.LoadLayout(ms);
