ReportViewer 自定義報表應用
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Reporting.WinForms;
using System.ComponentModel;
using System.IO;
namespace CRReportDesigner
{
public class ReportViewer : Microsoft.Reporting.WinForms.ReportViewer
{
private string _rcfile;
private ReportConfig _rpc;
private object _datasource;
private MemoryStream m_rdl;
private ReportManager reportManager;
public ReportViewer()
: base()
{
reportManager = new ReportManager(this);
}
public ReportManager ReportManager
{
get { return reportManager; }
}
/// <summary>
/// 配置檔案
/// </summary>
[DefaultValue("")]
public string Filename
{
get { return _rcfile; }
set
{
_rcfile = value;
_rpc = new ReportConfig(_rcfile);
}
}
/// <summary>
/// 報表配置
/// </summary>
public ReportConfig ReportConfig
{
get { return _rpc; }
set { this._rpc = value; }
}
/// <summary>
/// 資料來源
/// </summary>
public object DataSource
{
get { return _datasource; }
set { _datasource = value; }
}
/// <summary>
/// 顯示報表
/// </summary>
public void ShowReport()
{
if (m_rdl != null)
m_rdl.Dispose();
m_rdl = GenerateRdl();
Reset();
LocalReport.LoadReportDefinition(m_rdl);
LocalReport.DataSources.Add(new ReportDataSource("FaibLists", _datasource));
RefreshReport();
}
/// <summary>
/// 生成Rdl流
/// </summary>
/// <returns></returns>
private MemoryStream GenerateRdl()
{
MemoryStream ms = new MemoryStream();
RdlGenerator gen = new RdlGenerator();
gen.ReportConfig = _rpc;
gen.WriteXml(ms);
ms.Position = 0;
return ms;
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
namespace CRReportDesigner
{
public class ReportManager
{
ReportViewer reportViewer;
public ReportManager(ReportViewer rv)
{
reportViewer = rv;
}
/// <summary>
/// 頁面設定
/// </summary>
public void PageSetting()
{
FrmPageSettings frm = new FrmPageSettings(this.m_ReportName);
frm.ShowDialog();
}
private string m_ReportName = string.Empty;
protected System.Windows.Forms.PrintPreviewDialog previewDialog = new System.Windows.Forms.PrintPreviewDialog();
public string ReportName
{
get
{
return this.m_ReportName;
}
set
{
this.m_ReportName = value;
}
}
private EMFStreamPrintDocument printDoc = null;
/// <summary>
/// 列印預覽
/// </summary>
public void Preview()
{
if (this.printDoc == null)
{
this.printDoc = new EMFStreamPrintDocument(this.reportViewer.LocalReport, this.m_ReportName);
if (this.printDoc.ErrorMessage != string.Empty)
{
System.Windows.Forms.MessageBox.Show("Report Viewer:\r\n " + this.printDoc.ErrorMessage);
this.printDoc = null;
return;
}
}
this.previewDialog.Document = this.printDoc;
this.previewDialog.ShowDialog();
this.printDoc = null;
}
/// <summary>
/// 列印
/// </summary>
public void Print()
{
if (this.printDoc == null)
{
this.printDoc = new EMFStreamPrintDocument(this.reportViewer.LocalReport, this.m_ReportName);
if (this.printDoc.ErrorMessage != string.Empty)
{
System.Windows.Forms.MessageBox.Show("Report Viewer: \r\n " + this.printDoc.ErrorMessage);
this.printDoc = null;
return;
}
}
this.printDoc.Print();
this.printDoc = null;
}
/// <summary>
/// 重新整理
/// </summary>
public void Refresh()
{
this.reportViewer.RefreshReport();
}
public void Stop()
{
this.reportViewer.CancelRendering(0);
}
public void Back()
{
if (this.reportViewer.LocalReport.IsDrillthroughReport)
this.reportViewer.PerformBack();
}
/// <summary>
/// 儲存Excel
/// </summary>
public void SaveToExcel()
{
Microsoft.Reporting.WinForms.Warning[] Warnings;
string[] strStreamIds;
string strMimeType;
string strEncoding;
string strFileNameExtension;
byte[] bytes = this.reportViewer.LocalReport.Render("Excel", null, out strMimeType, out strEncoding, out strFileNameExtension, out strStreamIds, out Warnings);
string strFilePath = @"D:\" + this.GetTimeStamp() + ".xls";
using (System.IO.FileStream fs = new System.IO.FileStream(strFilePath, System.IO.FileMode.Create))
{
fs.Write(bytes, 0, bytes.Length);
}
if (System.Windows.Forms.MessageBox.Show("Report Viewer: \r\n Succeed to export the excel file!" + strFilePath + "\r\n Do you want to open the file" + strFilePath + "?", "", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
System.Diagnostics.Process.Start(strFilePath);
}
}
private string GetTimeStamp()
{
string strRet = string.Empty;
System.DateTime dtNow = System.DateTime.Now;
strRet += dtNow.Year.ToString() +
dtNow.Month.ToString("00") +
dtNow.Day.ToString("00") +
dtNow.Hour.ToString("00") +
dtNow.Minute.ToString("00") +
dtNow.Second.ToString("00") +
System.DateTime.Now.Millisecond.ToString("000");
return strRet;
}
/// <summary>
/// 放縮
/// </summary>
/// <param name="v"></param>
public void Zoom(int v)
{
this.reportViewer.ZoomMode = Microsoft.Reporting.WinForms.ZoomMode.Percent;
this.reportViewer.ZoomPercent = v;
}
public void ZoomWhole()
{
this.reportViewer.ZoomMode = Microsoft.Reporting.WinForms.ZoomMode.FullPage;
}
public void ZoomPageWidth()
{
this.reportViewer.ZoomMode = Microsoft.Reporting.WinForms.ZoomMode.PageWidth;
}
/// <summary>
/// 查詢
/// </summary>
/// <param name="txt"></param>
/// <param name="startPage"></param>
public void Find(string txt, int startPage)
{
if (string.IsNullOrEmpty(txt.Trim())) return;
this.reportViewer.Find(txt.Trim(), startPage);
}
public void FindNext(string txt)
{
if (string.IsNullOrEmpty(txt.Trim())) return;
this.reportViewer.FindNext();
}
public void FirstPage()
{
this.reportViewer.CurrentPage = 1;
}
public void LastPage()
{
this.reportViewer.CurrentPage = this.reportViewer.LocalReport.GetTotalPages();
}
public void PreviousPage()
{
if (this.reportViewer.CurrentPage != 1)
this.reportViewer.CurrentPage--;
}
public void NextPage()
{
if (this.reportViewer.CurrentPage != this.reportViewer.LocalReport.GetTotalPages())
this.reportViewer.CurrentPage++;
}
public void GoToPage(int i)
{
if (i <= this.reportViewer.LocalReport.GetTotalPages())
this.reportViewer.CurrentPage = i;
}
}
}
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Printing;
using System.Text;
using System.Reflection;
using System.Xml;
namespace CRReportDesigner
{
/// <summary>
/// 報表配置
/// </summary>
public class ReportConfig
{
private string _filename;
private bool _autosize;
private PageSettings _pset = new PageSettings();
private Dictionary<string, TextItem> _header;
private Dictionary<string, TextItem> _dataitem;
private Dictionary<string, TextItem> _footer;
private Dictionary<string, object> _cusmdata;
private float _headheight ;
private float _footheight ;
private float _width, _height, _lm, _tm, _rm, _bm;////頁面大小
private string _unit;
public ReportConfig()
{
_headheight = 60f;
_footheight = 60f;
_header = new Dictionary<string, TextItem>();
_dataitem = new Dictionary<string, TextItem>();
_footer = new Dictionary<string, TextItem>();
_cusmdata = new Dictionary<string, object>();
}
public ReportConfig(string frpFile)
{
if (string.IsNullOrEmpty(frpFile)) return;
_filename = frpFile;
XmlDocument xdoc = new XmlDocument();
xdoc.Load(frpFile);
XmlNode pnode = xdoc.SelectSingleNode("//configuration/pageset");
ReportConfig.GetAttribute(pnode, "unit", ref _unit);
if(string.IsNullOrEmpty(_unit)) //沒有單位
{
//頁面大小
int w = 0, h = 0;
ReportConfig.GetAttribute(pnode, "width", ref w);
ReportConfig.GetAttribute(pnode, "height", ref h);
_autosize = h == 0;
_pset.PaperSize = new PaperSize(frpFile, w, h);
//頁邊距
int m_left = 0, m_top = 0, m_right = 0, m_bottom = 0;
ReportConfig.GetAttribute(pnode, "margin-left", ref m_left);
ReportConfig.GetAttribute(pnode, "margin-top", ref m_top);
ReportConfig.GetAttribute(pnode, "margin-right", ref m_right);
ReportConfig.GetAttribute(pnode, "margin-bottom", ref m_bottom);
_pset.Margins = new Margins(m_left, m_right, m_top, m_bottom);
}
else
{
ReportConfig.GetAttribute(pnode, "width", ref _width);
ReportConfig.GetAttribute(pnode, "height", ref _height);
ReportConfig.GetAttribute(pnode, "margin-left", ref _lm);
ReportConfig.GetAttribute(pnode, "margin-top", ref _tm);
ReportConfig.GetAttribute(pnode, "margin-right", ref _rm);
ReportConfig.GetAttribute(pnode, "margin-bottom", ref _bm);
}
//頭
pnode = xdoc.SelectSingleNode("//configuration/header");
ReportConfig.GetAttribute(pnode, "height", ref _headheight);
//欄位
foreach (XmlNode node in xdoc.SelectNodes("//configuration/header/textitem"))
{
TextItem item = new TextItem(node);
if (_header == null)
{
_header = new Dictionary<string, TextItem>();
}
_header.Add(item.Key, item);
}
//尾
pnode = xdoc.SelectSingleNode("//configuration/footer");
ReportConfig.GetAttribute(pnode, "height", ref _footheight);
//欄位
foreach (XmlNode node in xdoc.SelectNodes("//configuration/footer/textitem"))
{
TextItem item = new TextItem(node);
if (_footer == null)
{
_footer = new Dictionary<string, TextItem>();
}
_footer.Add(item.Key, item);
}
//資料欄位
foreach (XmlNode node in xdoc.SelectNodes("//configuration/dataitem/textitem"))
{
TextItem item = new TextItem(node);
if(_dataitem == null)
{
_dataitem = new Dictionary<string, TextItem>();
}
_dataitem.Add(item.Key, item);
}
}
public Dictionary<string, object> CustomData
{
get {
if (_cusmdata == null)
_cusmdata = new Dictionary<string, object>();
return _cusmdata;
}
}
public Dictionary<string, TextItem> Header
{
get { return _header; }
}
public Dictionary<string, TextItem> DataItem
{
get { return _dataitem; }
}
public Dictionary<string, TextItem> Footer
{
get { return _footer; }
}
public PageSettings PageSettings
{
get { return _pset; }
}
public float HeadHeight
{
get { return _headheight; }
set { this._headheight = value; }
}
public float FootHeight
{
get { return _footheight; }
set { this._footheight = value; }
}
public float Width
{
get { return _width; }
set { this._width = value; }
}
public float Height
{
get { return _height; }
set { this._height = value; }
}
public float LeftMargin
{
get { return _lm; }
set { this._lm = value; }
}
public float TopMargin
{
get { return _tm; }
set { this._tm = value; }
}
public float RightMargin
{
get { return _rm; }
set { this._rm = value; }
}
public float BottomMargin
{
get { return _bm; }
set { this._bm = value; }
}
public bool AutoSize
{
get { return _autosize; }
set { _autosize = value; }
}
public string Unit
{
get { return _unit; }
set { _unit = value; }
}
/// <summary>
/// 從一個類獲取資料
/// </summary>
/// <param name="data"></param>
public void DataFromDataItem(object data)
{
Type type = data.GetType();
foreach (PropertyInfo pinfo in type.GetProperties(BindingFlags.Instance | BindingFlags.IgnoreCase | BindingFlags.NonPublic | BindingFlags.Public))
{
if (CustomData.ContainsKey(pinfo.Name))
continue;
object value = type.InvokeMember(pinfo.Name, BindingFlags.Instance | BindingFlags.GetProperty | BindingFlags.Public | BindingFlags.NonPublic, null, data, null);
if(value != null)
{
CustomData.Add(pinfo.Name, value);
}
}
}
internal static void GetAttribute(XmlNode node, string key, ref string value)
{
if (node.Attributes[key] != null)
{
value = node.Attributes[key].Value;
}
}
internal static void GetAttribute(XmlNode node, string key, ref int value)
{
if (node.Attributes[key] != null)
{
value = int.Parse(node.Attributes[key].Value);
}
}
internal static void GetAttribute(XmlNode node, string key, ref float value)
{
if (node.Attributes[key] != null)
{
value = float.Parse(node.Attributes[key].Value);
}
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Xml.Serialization;
namespace CRReportDesigner
{
public class RdlGenerator
{
private ReportConfig _rpc;
/// <summary>
/// 報表配置
/// </summary>
public ReportConfig ReportConfig
{
get { return _rpc; }
set { _rpc = value; }
}
/// <summary>
/// 建立報表
/// </summary>
/// <returns></returns>
private Rdl.Report CreateReport()
{
Rdl.Report report = new Rdl.Report();
string w = "", h = "", lm = "", tm = "", rm = "", bm = "";
//設定報表頁面
if(!string.IsNullOrEmpty(_rpc.Unit))
{
w = _rpc.Width + _rpc.Unit;
h = _rpc.Height + _rpc.Unit;
lm = _rpc.LeftMargin + _rpc.Unit;
tm = _rpc.TopMargin + _rpc.Unit;
rm = _rpc.RightMargin + _rpc.Unit;
bm = _rpc.BottomMargin + _rpc.Unit;
}
else
{
w = (_rpc.PageSettings.PaperSize.Width / 96.0) + "in";
h = (_rpc.PageSettings.PaperSize.Height / 96.0) + "in";
lm = (_rpc.LeftMargin / 96.0) + "in";
tm = (_rpc.TopMargin / 96.0) + "in";
rm = (_rpc.RightMargin / 96.0) + "in";
bm = (_rpc.BottomMargin / 96.0) + "in";
}
report.Items = new object[]
{
CreateDataSources(),
CreateHeader(),
CreateBody(),
CreateFooter(),
CreateDataSets(),
w,
h,
lm,
tm,
rm,
bm,
};
report.ItemsElementName = new Rdl.ItemsChoiceType37[]
{
Rdl.ItemsChoiceType37.DataSources,
Rdl.ItemsChoiceType37.PageHeader,
Rdl.ItemsChoiceType37.Body,
Rdl.ItemsChoiceType37.PageFooter,
Rdl.ItemsChoiceType37.DataSets,
Rdl.ItemsChoiceType37.Width,
Rdl.ItemsChoiceType37.PageHeight,
Rdl.ItemsChoiceType37.LeftMargin,
Rdl.ItemsChoiceType37.TopMargin,
Rdl.ItemsChoiceType37.RightMargin,
Rdl.ItemsChoiceType37.BottomMargin,
};
return report;
}
#region 資料來源
/// <summary>
/// 資料來源
/// </summary>
/// <returns></returns>
private Rdl.DataSourcesType CreateDataSources()
{
Rdl.DataSourcesType dataSources = new Rdl.DataSourcesType();
dataSources.DataSource = new Rdl.DataSourceType[] { CreateDataSource() };
return dataSources;
}
private Rdl.DataSourceType CreateDataSource()
{
Rdl.DataSourceType dataSource = new Rdl.DataSourceType();
dataSource.Name = "FaibLists";
dataSource.Items = new object[] { CreateConnectionProperties() };
return dataSource;
}
private Rdl.ConnectionPropertiesType CreateConnectionProperties()
{
Rdl.ConnectionPropertiesType connectionProperties = new Rdl.ConnectionPropertiesType();
connectionProperties.Items = new object[]
{
"",
"SQL",
};
connectionProperties.ItemsElementName = new Rdl.ItemsChoiceType[]
{
Rdl.ItemsChoiceType.ConnectString,
Rdl.ItemsChoiceType.DataProvider,
};
return connectionProperties;
}
#endregion
#region 主體
/// <summary>
/// 報表主體
/// </summary>
/// <returns></returns>
private Rdl.BodyType CreateBody()
{
Rdl.BodyType body = new Rdl.BodyType();
body.Items = new object[]
{
CreateReportItems(),
"1in",
};
body.ItemsElementName = new Rdl.ItemsChoiceType30[]
{
Rdl.ItemsChoiceType30.ReportItems,
Rdl.ItemsChoiceType30.Height,
};
return body;
}
private Rdl.ReportItemsType CreateReportItems()
{
Rdl.ReportItemsType reportItems = new Rdl.ReportItemsType();
TableRdlGenerator tableGen = new TableRdlGenerator();
tableGen.Fields = _rpc.DataItem;
reportItems.Items = new object[] { tableGen.CreateTable() };
return reportItems;
}
#endregion
#region 頁頭頁尾
private Rdl.PageHeaderFooterType CreateHeader()
{
Rdl.PageHeaderFooterType header = new Rdl.PageHeaderFooterType();
HeaderFooterRdlGenerator headerGen = new HeaderFooterRdlGenerator();
headerGen.Fields = _rpc.Header;
header.Items = new object[]
{
(_rpc.HeadHeight / 96.0) + "in",
true,
true,
headerGen.CreateItems(),
};
header.ItemsElementName = new Rdl.ItemsChoiceType34[]
{
Rdl.ItemsChoiceType34.Height,
Rdl.ItemsChoiceType34.PrintOnFirstPage,
Rdl.ItemsChoiceType34.PrintOnLastPage,
Rdl.ItemsChoiceType34.ReportItems
};
return header;
}
private Rdl.PageHeaderFooterType CreateFooter()
{
Rdl.PageHeaderFooterType footer = new Rdl.PageHeaderFooterType();
HeaderFooterRdlGenerator footerGen = new HeaderFooterRdlGenerator();
footerGen.Fields = _rpc.Footer;
footer.Items = new object[]
{
(_rpc.FootHeight / 96.0) + "in",
true,
true,
footerGen.CreateItems(),
};
footer.ItemsElementName = new Rdl.ItemsChoiceType34[]
{
Rdl.ItemsChoiceType34.Height,
Rdl.ItemsChoiceType34.PrintOnFirstPage,
Rdl.ItemsChoiceType34.PrintOnLastPage,
Rdl.ItemsChoiceType34.ReportItems
};
return footer;
}
#endregion
#region 資料集
private Rdl.DataSetsType CreateDataSets()
{
Rdl.DataSetsType dataSets = new Rdl.DataSetsType();
dataSets.DataSet = new Rdl.DataSetType[] { CreateDataSet() };
return dataSets;
}
private Rdl.DataSetType CreateDataSet()
{
Rdl.DataSetType dataSet = new Rdl.DataSetType();
dataSet.Name = "FaibLists";
dataSet.Items = new object[] { CreateQuery(), CreateFields() };
return dataSet;
}
private Rdl.QueryType CreateQuery()
{
Rdl.QueryType query = new Rdl.QueryType();
query.Items = new object[]
{
"FaibLists",
"",
};
query.ItemsElementName = new Rdl.ItemsChoiceType2[]
{
Rdl.ItemsChoiceType2.DataSourceName,
Rdl.ItemsChoiceType2.CommandText,
};
return query;
}
private Rdl.FieldsType CreateFields()
{
Rdl.FieldsType fields = new Rdl.FieldsType();
Dictionary<string, TextItem> m_fields = _rpc.DataItem;
fields.Field = new Rdl.FieldType[m_fields.Count];
int i = 0;
foreach (string key in m_fields.Keys)
{
fields.Field[i++] = CreateField(m_fields[key]);
}
return fields;
}
private Rdl.FieldType CreateField(TextItem item)
{
Rdl.FieldType field = new Rdl.FieldType();
field.Name = item.DataMember;
field.Items = new object[] { item.DataMember };
field.ItemsElementName = new Rdl.ItemsChoiceType1[] { Rdl.ItemsChoiceType1.DataField };
return field;
}
#endregion
public void WriteXml(Stream stream)
{
XmlSerializer serializer = new XmlSerializer(typeof(Rdl.Report));
serializer.Serialize(stream, CreateReport());
}
}
}
using System;
using System.Drawing;
using System.Drawing.Printing;
using System.Text;
using System.Xml;
namespace CRReportDesigner
{
[Serializable]
public class TextItem
{
private string _text;
private string _key;
private string _datamember;
private Color backgroundColor=Color.White;
private Color textColor=Color.Black;
private Rectangle _rect = Rectangle.Empty;
private StringFormat _align;
private bool _count;
private string _format;
private Font _font = System.Windows.Forms.Control.DefaultFont;
private Font _headerfont = System.Windows.Forms.Control.DefaultFont;
public TextItem()
{
AlignStringFormat = "center";
}
public TextItem(string key, string text)
{
AlignStringFormat = "center";
this._key = key;
this._text = text;
}
public TextItem(string key, string text, string datamember, Rectangle rect)
{
AlignStringFormat = "center";
this._key = key;
this._text = text;
this.DataMember = datamember;
this._rect = rect;
}
public TextItem(XmlNode node)
{
_key = node.Attributes["key"].Value;
if(!string.IsNullOrEmpty(node.InnerText))
{
_text = node.InnerText;
}
else
{
ReportConfig.GetAttribute(node, "text", ref _text);
}
ReportConfig.GetAttribute(node, "data", ref _datamember);
ReportConfig.GetAttribute(node, "format", ref _format);
string font_name = _font.Name;
string font_bold = "";
string font_italic = "";
string font_underline = "";
float font_size = Font.Size;
FontStyle fsty = FontStyle.Regular;
ReportConfig.GetAttribute(node, "font-name", ref font_name);
ReportConfig.GetAttribute(node, "font-size", ref font_size);
ReportConfig.GetAttribute(node, "font-bold", ref font_bold);
ReportConfig.GetAttribute(node, "font-italic", ref font_italic);
ReportConfig.GetAttribute(node, "font-underline", ref font_underline);
if (font_bold.Equals("1"))
{
fsty |= FontStyle.Bold;
}
if (font_italic.Equals("1"))
{
fsty |= FontStyle.Italic;
}
if (font_underline.Equals("1"))
{
fsty |= FontStyle.Underline;
}
_font = new Font(font_name, font_size, fsty);
font_name = _font.Name;
font_bold = "";
font_italic = "";
font_underline = "";
font_size = Font.Size;
fsty = FontStyle.Regular;
ReportConfig.GetAttribute(node, "header-font-name", ref font_name);
ReportConfig.GetAttribute(node, "header-font-size", ref font_size);
ReportConfig.GetAttribute(node, "header-font-bold", ref font_bold);
ReportConfig.GetAttribute(node, "header-font-italic", ref font_italic);
ReportConfig.GetAttribute(node, "header-font-underline", ref font_underline);
if (font_bold.Equals("1"))
{
fsty |= FontStyle.Bold;
}
if (font_italic.Equals("1"))
{
fsty |= FontStyle.Italic;
}
if (font_underline.Equals("1"))
{
fsty |= FontStyle.Underline;
}
_headerfont = new Font(font_name, font_size, fsty);
int left = 0, top = 0, width = 0, height = 0;
ReportConfig.GetAttribute(node, "left", ref left);
ReportConfig.GetAttribute(node, "top", ref top);
ReportConfig.GetAttribute(node, "width", ref width);
ReportConfig.GetAttribute(node, "height", ref height);
_rect = new Rectangle(left, top, width, height);
string align = "left";
ReportConfig.GetAttribute(node, "align", ref align);
string valign = "top";
ReportConfig.GetAttribute(node, "valign", ref valign);
_align = new StringFormat();
if (align.Equals("right", StringComparison.OrdinalIgnoreCase))
{
_align.Alignment = StringAlignment.Far;
}
else if (align.Equals("center", StringComparison.OrdinalIgnoreCase))
{
_align.Alignment = StringAlignment.Center;
}
if (valign.Equals("bottom", StringComparison.OrdinalIgnoreCase))
{
_align.LineAlignment = StringAlignment.Far;
}
else if (valign.Equals("middle", StringComparison.OrdinalIgnoreCase))
{
_align.LineAlignment = StringAlignment.Center;
}
string count = "";
ReportConfig.GetAttribute(node, "count", ref count);
if(count.Equals("1"))
{
_count = true;
}
}
public string Key
{
get { return _key; }
set { this._key = value; }
}
public string Text
{
get { return _text; }
set { _text = value; }
}
public string DataMember
{
get { return _datamember; }
set { this._datamember = value; }
}
public string DataFormat
{
get { return _format; }
set { this._format = value; }
}
public StringFormat Align
{
get { return _align; }
}
public string AlignStringFormat
{
get {
if (_align.Alignment.Equals(StringAlignment.Far))
{
return "right";
}
else if (_align.Alignment.Equals( StringAlignment.Center))
{
return "center";
}
if (_align.LineAlignment.Equals(StringAlignment.Far))
{
return "bottom";
}
else if (_align.LineAlignment.Equals(StringAlignment.Center))
{
return "middle";
}
return "center";
}
set
{
_align = new StringFormat();
if (value.Equals("right", StringComparison.OrdinalIgnoreCase))
{
_align.Alignment = StringAlignment.Far;
}
else if (value.Equals("center", StringComparison.OrdinalIgnoreCase))
{
_align.Alignment = StringAlignment.Center;
}
if (value.Equals("bottom", StringComparison.OrdinalIgnoreCase))
{
_align.LineAlignment = StringAlignment.Far;
}
else if (value.Equals("middle", StringComparison.OrdinalIgnoreCase))
{
_align.LineAlignment = StringAlignment.Center;
}
}
}
public Rectangle Rect
{
get { return _rect; }
set { _rect = value; }
}
public bool Count
{
get { return _count; }
set { this._count = value; }
}
public Color BackgroundColor
{
get { return backgroundColor; }
set { backgroundColor = value; }
}
public Color TextColor
{
get { return textColor; }
set { textColor = value; }
}
public Font Font
{
get { return _font; }
set { this._font = value; }
}
public Font HeaderFont
{
get { return _headerfont; }
set { this._headerfont = value; }
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Xml.Serialization;
namespace CRReportDesigner
{
public class TableRdlGenerator
{
private float _tableTop = 0;
private float _tableLeft = 30;
public float TableTop
{
get { return _tableTop; }
set { this._tableTop = value; }
}
public float TableLeft
{
get { return _tableLeft; }
set { this._tableLeft = value; }
}
private Dictionary<string, TextItem> m_fields;
public Dictionary<string, TextItem> Fields
{
get { return m_fields; }
set { m_fields = value; }
}
public Rdl.TableType CreateTable()
{
//定義表格
Rdl.TableType table = new Rdl.TableType();
table.Name = "Table1";
table.Items = new object[]
{
CreateTableColumns(),
CreateHeader(),
CreateDetails(),
(_tableTop / 96.0) + "in",
(_tableLeft/ 96.0) + "in",
};
table.ItemsElementName = new Rdl.ItemsChoiceType21[]
{
Rdl.ItemsChoiceType21.TableColumns,
Rdl.ItemsChoiceType21.Header,
Rdl.ItemsChoiceType21.Details,
Rdl.ItemsChoiceType21.Top,
Rdl.ItemsChoiceType21.Left
};
return table;
}
private Rdl.HeaderType CreateHeader()
{
Rdl.HeaderType header = new Rdl.HeaderType();
header.Items = new object[]
{
CreateHeaderTableRows(),
true,
};
header.ItemsElementName = new Rdl.ItemsChoiceType20[]
{
Rdl.ItemsChoiceType20.TableRows,
Rdl.ItemsChoiceType20.RepeatOnNewPage,
};
return header;
}
private Rdl.TableRowsType CreateHeaderTableRows()
{
Rdl.TableRowsType headerTableRows = new Rdl.TableRowsType();
headerTableRows.TableRow = new Rdl.TableRowType[] { CreateHeaderTableRow() };
return headerTableRows;
}
private Rdl.TableRowType CreateHeaderTableRow()
{
Rdl.TableRowType headerTableRow = new Rdl.TableRowType();
headerTableRow.Items = new object[] { CreateHeaderTableCells(), "0.25in" };
return headerTableRow;
}
private Rdl.TableCellsType CreateHeaderTableCells()
{
Rdl.TableCellsType headerTableCells = new Rdl.TableCellsType();
headerTableCells.TableCell = new Rdl.TableCellType[m_fields.Count];
int i = 0;
foreach (string key in m_fields.Keys)
{
headerTableCells.TableCell[i++] = CreateHeaderTableCell(m_fields[key]);
}
return headerTableCells;
}
private Rdl.TableCellType CreateHeaderTableCell(TextItem item)
{
Rdl.TableCellType headerTableCell = new Rdl.TableCellType();
headerTableCell.Items = new object[] { CreateHeaderTableCellReportItems(item) };
return headerTableCell;
}
private Rdl.ReportItemsType CreateHeaderTableCellReportItems(TextItem item)
{
Rdl.ReportItemsType headerTableCellReportItems = new Rdl.ReportItemsType();
headerTableCellReportItems.Items = new object[] { CreateHeaderTableCellTextbox(item) };
return headerTableCellReportItems;
}
private Rdl.TextboxType CreateHeaderTableCellTextbox(TextItem item)
{
Rdl.UserSortType sortType = new Rdl.UserSortType();
sortType.Items = new object[] { "=Fields!" + item.DataMember + ".Value" };
sortType.ItemsElementName = new Rdl.ItemsChoiceType13[] { Rdl.ItemsChoiceType13.SortExpression };
Rdl.TextboxType headerTableCellTextbox = new Rdl.TextboxType();
headerTableCellTextbox.Name = item.Text + "_Header";
headerTableCellTextbox.Items = new object[]
{
item.Text,
CreateHeaderTableCellTextboxStyle(item),
true,
sortType,
};
headerTableCellTextbox.ItemsElementName = new Rdl.ItemsChoiceType14[]
{
Rdl.ItemsChoiceType14.Value,
Rdl.ItemsChoiceType14.Style,
Rdl.ItemsChoiceType14.CanGrow,
Rdl.ItemsChoiceType14.UserSort
};
return headerTableCellTextbox;
}
private Rdl.StyleType CreateHeaderTableCellTextboxStyle(TextItem item)
{
Rdl.StyleType headerTableCellTextboxStyle = new Rdl.StyleType();
headerTableCellTextboxStyle.Items = new object[]
{
item.BackgroundColor.Name,
item.TextColor.Name,
item.HeaderFont.Name,
item.HeaderFont.Size + "pt",
"700",//item.HeaderFont.Bold ? "700" : "100",
"Center",
"Middle",
CreateBorderStyle(),
};
headerTableCellTextboxStyle.ItemsElementName = new Rdl.ItemsChoiceType5[]
{
Rdl.ItemsChoiceType5.BackgroundColor,
Rdl.ItemsChoiceType5.Color,
Rdl.ItemsChoiceType5.FontFamily,
Rdl.ItemsChoiceType5.FontSize,
Rdl.ItemsChoiceType5.FontWeight,
Rdl.ItemsChoiceType5.TextAlign,
Rdl.ItemsChoiceType5.VerticalAlign,
Rdl.ItemsChoiceType5.BorderStyle,
};
return headerTableCellTextboxStyle;
}
private Rdl.DetailsType CreateDetails()
{
Rdl.DetailsType details = new Rdl.DetailsType();
details.Items = new object[] { CreateTableRows() };
return details;
}
private Rdl.TableRowsType CreateTableRows()
{
Rdl.TableRowsType tableRows = new Rdl.TableRowsType();
tableRows.TableRow = new Rdl.TableRowType[] { CreateTableRow() };
return tableRows;
}
private Rdl.TableRowType CreateTableRow()
{
Rdl.TableRowType tableRow = new Rdl.TableRowType();
tableRow.Items = new object[] { CreateTableCells(), "0.25in" };
return tableRow;
}
private Rdl.TableCellsType CreateTableCells()
{
Rdl.TableCellsType tableCells = new Rdl.TableCellsType();
tableCells.TableCell = new Rdl.TableCellType[m_fields.Count];
int i = 0;
foreach(string key in m_fields.Keys)
{
tableCells.TableCell[i++] = CreateTableCell(m_fields[key]);
}
return tableCells;
}
private Rdl.TableCellType CreateTableCell(TextItem item)
{
Rdl.TableCellType tableCell = new Rdl.TableCellType();
tableCell.Items = new object[] { CreateTableCellReportItems(item) };
return tableCell;
}
private Rdl.ReportItemsType CreateTableCellReportItems(TextItem item)
{
Rdl.ReportItemsType reportItems = new Rdl.ReportItemsType();
reportItems.Items = new object[] { CreateTableCellTextbox(item) };
return reportItems;
}
private Rdl.TextboxType CreateTableCellTextbox(TextItem item)
{
Rdl.TextboxType textbox = new Rdl.TextboxType();
textbox.Name = item.Key;
textbox.Items = new object[]
{
"=Fields!" + item.DataMember + ".Value",
CreateTableCellTextboxStyle(item),
true,
};
textbox.ItemsElementName = new Rdl.ItemsChoiceType14[]
{
Rdl.ItemsChoiceType14.Value,
Rdl.ItemsChoiceType14.Style,
Rdl.ItemsChoiceType14.CanGrow,
};
return textbox;
}
private Rdl.StyleType CreateTableCellTextboxStyle(TextItem item)
{
Rdl.StyleType style = new Rdl.StyleType();
style.Items = new object[]
{
"=iif(RowNumber(Nothing) mod 2, \"AliceBlue\", \"White\")",
item.Font.Name,
item.Font.Size + "pt",
item.Font.Bold ? "400" : "100",
GetAlign(item.Align.Alignment),
"Middle",
CreateBorderStyle(),
"1pt",
"1pt",
"1pt",
"1pt",
};
style.ItemsElementName = new Rdl.ItemsChoiceType5[]
{
Rdl.ItemsChoiceType5.BackgroundColor,
Rdl.ItemsChoiceType5.FontFamily,
Rdl.ItemsChoiceType5.FontSize,
Rdl.ItemsChoiceType5.FontWeight,
Rdl.ItemsChoiceType5.TextAlign,
Rdl.ItemsChoiceType5.VerticalAlign,
Rdl.ItemsChoiceType5.BorderStyle,
Rdl.ItemsChoiceType5.PaddingLeft,
Rdl.ItemsChoiceType5.PaddingTop,
Rdl.ItemsChoiceType5.PaddingRight,
Rdl.ItemsChoiceType5.PaddingBottom,
};
return style;
}
private Rdl.TableColumnsType CreateTableColumns()
{
Rdl.TableColumnsType tableColumns = new Rdl.TableColumnsType();
tableColumns.TableColumn = new Rdl.TableColumnType[m_fields.Count];
int i = 0;
foreach (string key in m_fields.Keys)
{
tableColumns.TableColumn[i++] = CreateTableColumn(m_fields[key]);
}
return tableColumns;
}
private Rdl.TableColumnType CreateTableColumn(TextItem item)
{
Rdl.TableColumnType tableColumn = new Rdl.TableColumnType();
tableColumn.Items = new object[] { (item.Rect.Width / 96.0) + "in" };
return tableColumn;
}
private Rdl.BorderColorStyleWidthType CreateBorderStyle()
{
Rdl.BorderColorStyleWidthType bstyle = new Rdl.BorderColorStyleWidthType();
bstyle.Items = new object[]
{
"Solid",
};
bstyle.ItemsElementName = new Rdl.ItemsChoiceType3[]
{
Rdl.ItemsChoiceType3.Default,
};
return bstyle;
}
private string GetVAlign(StringAlignment sformat)
{
switch (sformat)
{
case StringAlignment.Center: return "Middle";
case StringAlignment.Far: return "Bottom";
default: return "Top";
}
}
private string GetAlign(StringAlignment sformat)
{
switch (sformat)
{
case StringAlignment.Center: return "Center";
case StringAlignment.Far: return "Right";
default: return "Left";
}
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
namespace CRReportDesigner
{
public class HeaderFooterRdlGenerator
{
private Dictionary<string, TextItem> m_fields;
private int m_height;
public Dictionary<string, TextItem> Fields
{
get { return m_fields; }
set { m_fields = value; }
}
public int Height
{
get { return m_height; }
set { m_height = value; }
}
public Rdl.ReportItemsType CreateItems()
{
Rdl.ReportItemsType items = new Rdl.ReportItemsType();
items.Items = new Rdl.TextboxType[m_fields.Count];
int i = 0;
foreach (string key in m_fields.Keys)
{
items.Items[i++] = CreateTextBox(m_fields[key]);
}
return items;
}
private Rdl.TextboxType CreateTextBox(TextItem item)
{
Rdl.TextboxType textbox = new Rdl.TextboxType();
textbox.Name = item.Key;
if (item.Rect != Rectangle.Empty)
{
textbox.Items = new object[]
{
item.Text,
CreateTextboxStyle(item),
true,
(item.Rect.Left / 96.0) + "in",
(item.Rect.Top / 96.0) + "in",
(item.Rect.Width / 96.0) + "in",
(item.Rect.Height / 96.0) + "in",
};
textbox.ItemsElementName = new Rdl.ItemsChoiceType14[]