یه کلاس به پروژه ات اضافه کن و اسم اون رو ReportGenerator بذار و کدهای زیر رو توی اون کپی کن .
فقط بعد از کپی کدها namespace اون رو عوض کن
using System;
using System.Data;
using System.Drawing;
using Stimulsoft.Base;
using Stimulsoft.Base.Drawing;
using Stimulsoft.Report;
using Stimulsoft.Report.Components;
using Stimulsoft.Report.Export;
namespace Daftar_Rahnama
{
internal class ReportGenerator
{
StiPageOrientation _PageOrientation;
string _ReportTitle;
StiBorder _PageBorder;
StiBorder _ReportTitleBorder;
double _ReportTitleHeight;
StiTextHorAlignment _ReportTitleHorAlignment;
StiVertAlignment _ReportTitleVertAlignment;
Font _ReportTitleFont;
double _HeaderBandHeight;
StiBorder _HeaderBandBorder;
double _DataBandHeight;
StiBorder _DataBandBorder;
bool _DataBandCanGrow;
StiTextHorAlignment _HeaderColumnsHorAlignment;
StiVertAlignment _HeaderColumnsVertAlignment;
StiBrush _HeaderColumnsBrush;
StiBorderSides _HeaderColumnsBorderSide;
Font _HeaderColumnsFont;
StiTextHorAlignment _DataTextHorAlignment;
StiVertAlignment _DataTextVertAlignment;
StiBorderSides _DataTextBorderSides;
Font _DataTextFont;
bool _DataTextWordWrap;
StiBrush _FooterBandBrush;
string _FooterTextValue;
StiTextHorAlignment _FooterTextHorAlignment;
StiVertAlignment _FooterTextVertAlignment;
StiBrush _FooterTextBrush;
Font _FooterTextFont;
StiBorder _FooterTextBorder;
public ReportGenerator()
{
PageOrientation = StiPageOrientation.Landscape;
PageBorder = new StiBorder(StiBorderSides.All, Color.Black, 1, StiPenStyle.None);
ReportTitleBorder = new StiBorder(StiBorderSides.All, Color.Black, 1, StiPenStyle.None);
ReportTitleHeight = 1.5f;
ReportTitleHorAlignment = StiTextHorAlignment.Center;
ReportTitleVertAlignment = StiVertAlignment.Center;
ReportTitleFont = new Font("B Mitra", 20f);
HeaderBandHeight = 0.5f;
HeaderBandBorder = new StiBorder(StiBorderSides.All, Color.Black, 1, StiPenStyle.Solid);
DataBandHeight = 0.5f;
DataBandBorder = new StiBorder(StiBorderSides.Left, Color.Black, 1, StiPenStyle.Solid);
DataBandCanGrow = true;
HeaderColumnsHorAlignment = StiTextHorAlignment.Center;
HeaderColumnsVertAlignment = StiVertAlignment.Center;
HeaderColumnsBrush = new StiSolidBrush(Color.Wheat);
HeaderColumnsBorderSide = StiBorderSides.All;
HeaderColumnsFont = new Font("Tahoma", 8f,FontStyle.Bold);
DataTextHorAlignment = StiTextHorAlignment.Center;
DataTextVertAlignment = StiVertAlignment.Center;
DataTextBorderSides = StiBorderSides.All;
DataTextFont = new Font("Tahoma", 8f);
DataTextWordWrap = true;
FooterBandBrush = new StiSolidBrush(Color.WhiteSmoke);
FooterTextValue = "تعداد رکوردهای چاپ شده: {Count()}";
FooterTextHorAlignment = StiTextHorAlignment.Right;
FooterTextVertAlignment = StiVertAlignment.Center;
FooterTextBrush = new StiSolidBrush(Color.WhiteSmoke);
FooterTextFont = new Font("Tahoma", 8f);
FooterTextBorder = new StiBorder(StiBorderSides.All, Color.Black, 1, StiPenStyle.Solid);
}
public StiPageOrientation PageOrientation { get { return _PageOrientation; } set { _PageOrientation = value; } }
public string ReportTitle { get { return _ReportTitle; } set { _ReportTitle = value; } }
public StiBorder PageBorder { get { return _PageBorder; } set { _PageBorder = value; } }
public StiBorder ReportTitleBorder { get { return _ReportTitleBorder; } set { _ReportTitleBorder = value; } }
public double ReportTitleHeight { get { return _ReportTitleHeight; } set { _ReportTitleHeight = value; } }
public StiTextHorAlignment ReportTitleHorAlignment { get { return _ReportTitleHorAlignment; } set { _ReportTitleHorAlignment = value; } }
public StiVertAlignment ReportTitleVertAlignment { get { return _ReportTitleVertAlignment; } set { _ReportTitleVertAlignment = value; } }
public Font ReportTitleFont { get { return _ReportTitleFont; } set { _ReportTitleFont = value; } }
public double HeaderBandHeight { get { return _HeaderBandHeight; } set { _HeaderBandHeight = value; } }
public StiBorder HeaderBandBorder { get { return _HeaderBandBorder; } set { _HeaderBandBorder = value; } }
public double DataBandHeight { get { return _DataBandHeight; } set { _DataBandHeight = value; } }
public StiBorder DataBandBorder { get { return _DataBandBorder; } set { _DataBandBorder = value; } }
public bool DataBandCanGrow { get { return _DataBandCanGrow; } set { _DataBandCanGrow = value; } }
public StiTextHorAlignment HeaderColumnsHorAlignment { get { return _HeaderColumnsHorAlignment; } set { _HeaderColumnsHorAlignment = value; } }
public StiVertAlignment HeaderColumnsVertAlignment { get { return _HeaderColumnsVertAlignment; } set { _HeaderColumnsVertAlignment = value; } }
public StiBrush HeaderColumnsBrush { get { return _HeaderColumnsBrush; } set { _HeaderColumnsBrush = value; } }
public StiBorderSides HeaderColumnsBorderSide { get { return _HeaderColumnsBorderSide; } set { _HeaderColumnsBorderSide = value; } }
public Font HeaderColumnsFont { get { return _HeaderColumnsFont; } set { _HeaderColumnsFont = value; } }
public StiTextHorAlignment DataTextHorAlignment { get { return _DataTextHorAlignment; } set { _DataTextHorAlignment = value; } }
public StiVertAlignment DataTextVertAlignment { get { return _DataTextVertAlignment; } set { _DataTextVertAlignment = value; } }
public StiBorderSides DataTextBorderSides { get { return _DataTextBorderSides; } set { _DataTextBorderSides = value; } }
public Font DataTextFont { get { return _DataTextFont; } set { _DataTextFont = value; } }
public bool DataTextWordWrap { get { return _DataTextWordWrap; } set { _DataTextWordWrap = value; } }
public StiBrush FooterBandBrush { get { return _FooterBandBrush; } set { _FooterBandBrush = value; } }
public string FooterTextValue { get { return _FooterTextValue; } set { _FooterTextValue = value; } }
public StiTextHorAlignment FooterTextHorAlignment { get { return _FooterTextHorAlignment; } set { _FooterTextHorAlignment = value; } }
public StiVertAlignment FooterTextVertAlignment { get { return _FooterTextVertAlignment; } set { _FooterTextVertAlignment = value; } }
public StiBrush FooterTextBrush { get { return _FooterTextBrush; } set { _FooterTextBrush = value; } }
public Font FooterTextFont { get { return _FooterTextFont; } set { _FooterTextFont = value; } }
public StiBorder FooterTextBorder { get { return _FooterTextBorder; } set { _FooterTextBorder = value; } }
public void PrintReport(DataTable dtDataSource)
{
DataView dataView = dtDataSource.DefaultView;
StiReport report = new StiReport();
report.ScriptLanguage = StiReportLanguageType.CSharp;
report.RegData("view", dataView);
//Fill dictionary
report.Dictionary.Synchronize();
StiPage page = report.Pages.Items[0];
page.Border = _PageBorder;
page.Orientation = _PageOrientation;
//Create Report Title Band
StiReportTitleBand rt = new StiReportTitleBand();
rt.Height = _ReportTitleHeight;
rt.Name = "ReportTitleBand";
rt.Border = _ReportTitleBorder;
//Create Text on Report Title Band
StiText st = new StiText(new RectangleD(0, 0, page.Width, 1f));
st.Text.Value = _ReportTitle;
st.HorAlignment = _ReportTitleHorAlignment;
st.VertAlignment = _ReportTitleVertAlignment;
st.Name = "TitleText1";
st.Font = _ReportTitleFont;
rt.Components.Add(st);
page.Components.Add(rt);
//Create HeaderBand
StiHeaderBand headerBand = new StiHeaderBand();
headerBand.Height = _HeaderBandHeight;
headerBand.Name = "HeaderBand";
headerBand.Border = _HeaderBandBorder;
page.Components.Add(headerBand);
//Create DataBand
StiDataBand dataBand = new StiDataBand();
dataBand.DataSourceName = "view" + dataView.Table.TableName;
dataBand.Height = _DataBandHeight;
dataBand.Name = "DataBand";
dataBand.Border = _DataBandBorder;
dataBand.CanGrow = _DataBandCanGrow;
page.Components.Add(dataBand);
//Create texts
Double pos = 0;
Double columnWidth = StiAlignValue.AlignToMinGrid(page.Width / dataView.Table.Columns.Count, 0.1, false);
int nameIndex = 1;
for (int i = dataView.Table.Columns.Count - 1; i != -1; i--)
{
DataColumn column = dataView.Table.Columns[i];
//Create text on header
StiText headerText = new StiText(new RectangleD(pos, 0, columnWidth, 0.5f));
headerText.Text.Value = column.Caption;
headerText.HorAlignment = _HeaderColumnsHorAlignment;
headerText.VertAlignment = _HeaderColumnsVertAlignment;
headerText.Name = "HeaderText" + nameIndex.ToString();
headerText.Brush = _HeaderColumnsBrush;
headerText.Border.Side = _HeaderColumnsBorderSide;
headerBand.Components.Add(headerText);
headerText.Font = _HeaderColumnsFont;
//Create text on Data Band
StiText dataText = new StiText(new RectangleD(pos, 0, columnWidth, 0.5f));
dataText.Text.Value = "{view" + dataView.Table.TableName + "." + Stimulsoft.Report.CodeDom.StiCodeDomSerializator.R eplaceSymbols(column.ColumnName) + "}";
dataText.Name = "DataText" + nameIndex.ToString();
dataText.HorAlignment = _DataTextHorAlignment;
dataText.VertAlignment = _DataTextVertAlignment;
dataText.Font = _DataTextFont;
dataText.Border.Side = _DataTextBorderSides;
dataText.WordWrap = _DataTextWordWrap;
dataBand.Components.Add(dataText);
pos += columnWidth;
nameIndex++;
}
//Create FooterBand
StiFooterBand footerBand = new StiFooterBand();
footerBand.Height = 0.5f;
footerBand.Name = "FooterBand";
footerBand.Border = new StiBorder(StiBorderSides.All, Color.Black, 1, StiPenStyle.Solid);
footerBand.Left = dataBand.Left;
footerBand.Width = dataBand.Width;
footerBand.Brush = _FooterBandBrush;
page.Components.Add(footerBand);
//Create text on footer
StiText footerText = new StiText(new RectangleD(0, 0, page.Width, 0.5f));
footerText.Text.Value = _FooterTextValue;
footerText.HorAlignment = _FooterTextHorAlignment;
footerText.VertAlignment = _FooterTextVertAlignment;
footerText.Name = "FooterText";
footerText.Brush = _FooterTextBrush;
footerText.Font = _FooterTextFont;
footerText.Border = _FooterTextBorder;
footerBand.Components.Add(footerText);
//Render without progress bar
report.Render(false);
report.Show();
}
public void ExportToExcel(DataTable data, string FileName)
{
StiReport Report = new StiReport();
Font _HeaderFont = new Font("Tahoma", 8f, FontStyle.Bold);
Font _RowsFont = new Font("Tahoma", 8f, FontStyle.Regular);
//Add data to datastore
Report.RegData("data", data);
//Fill dictionary
Report.Dictionary.Synchronize();
Report.Dictionary.DataSources[0].Name = "data";
Report.Dictionary.DataSources[0].Alias = "data";
StiPage page = Report.Pages[0];
page.Orientation = Stimulsoft.Report.Components.StiPageOrientation.La ndscape;
page.RightToLeft = true;
page.Width = 100;
//Create HeaderBand
StiHeaderBand headerBand = new StiHeaderBand();
headerBand.Height = 0.5;
headerBand.Name = "HeaderBand";
headerBand.SetFont(_RowsFont);
page.Components.Add(headerBand);
//Create Databand
StiDataBand dataBand = new StiDataBand();
dataBand.DataSourceName = "data";
dataBand.Height = 0.5;
dataBand.Name = "DataBand";
dataBand.SetFont(_RowsFont);
page.Components.Add(dataBand);
//Create texts
double pos = 0;
//double columnWidth = StiAlignValue.AlignToMinGrid(page.Width / data.Columns.Count, 0.1, true);
double columnWidth = 2;
int nameIndex = 1;
foreach (DataColumn dataColumn in data.Columns)
{
//Create text on header
StiText headerText = new StiText(new RectangleD(pos, 0, columnWidth, 0.5));
headerText.Text.Value = dataColumn.Caption;
headerText.HorAlignment = StiTextHorAlignment.Center;
headerText.VertAlignment = StiVertAlignment.Center;
headerText.Name = "HeaderText" + nameIndex.ToString();
headerText.Font = _HeaderFont;
headerText.Brush = new StiSolidBrush(Color.WhiteSmoke);
headerText.Border.Side = StiBorderSides.All;
headerBand.Components.Add(headerText);
//Create text on Data Band
StiText dataText = new StiText(new RectangleD(pos, 0, columnWidth, 0.5));
dataText.Text.Value = "{data." + Stimulsoft.Report.CodeDom.StiCodeDomSerializator.R eplaceSymbols(dataColumn.ColumnName) + "}";
dataText.Name = "DataText" + nameIndex.ToString();
dataText.Font = _RowsFont;
dataText.Border.Side = StiBorderSides.All;
dataText.HorAlignment = StiTextHorAlignment.Center;
StiCondition condition = new StiCondition();
condition.BackColor = Color.White;
condition.TextColor = Color.Black;
condition.Font = _RowsFont;
condition.Expression = "(Line & 1) == 1";
condition.Item = StiFilterItem.Expression;
dataText.Conditions.Add(condition);
dataBand.Components.Add(dataText);
pos = pos + columnWidth;
nameIndex++;
}
//Create FooterBand
StiFooterBand footerBand = new StiFooterBand();
footerBand.Height = 0.5;
footerBand.Name = "FooterBand";
page.Components.Add(footerBand);
//Render without progress bar
Report.Render(false);
StiExcel2007ExportSettings ExcelSettings = new StiExcel2007ExportSettings();
ExcelSettings.UseOnePageHeaderAndFooter = true;
ExcelSettings.ExportObjectFormatting = true;
StiOptions.Export.Excel2007.ColumnsRightToLeft = true;
StiOptions.Export.Excel2007.RemoveEmptySpaceAtBott om = true;
Report.ExportDocument(StiExportFormat.Excel2007, FileName, ExcelSettings);
}
}
}
بعد یه نمونه از این کلاس درست کن و از متد PrintReport که یک دیتا تیبل رو به عنوان ورودی میگیره استفاده کن . اینطوری :
ReportGenerator report = new ReportGenerator();
report.ReportTitle = "ليست اطلاعات و مشخصات افراد ";
report.ReportTitleFont = new Font("B Yekan", 12f);
report.HeaderColumnsFont = new Font("B Nazanin", 8f, FontStyle.Bold);
report.PageOrientation = Stimulsoft.Report.Components.StiPageOrientation.La ndscape;
report.DataBandCanGrow = true;
report.DataTextFont = new Font("B Nazanin", 8f, FontStyle.Regular);
report.DataTextWordWrap = true;
DataTable dt = PreparPrintDataTable();
report.PrintReport(dt);
البته به توی خط یکی به آخر باید دیتاتیبل خودت رو مشخص کنی .
همچنین کلاسی که گفتم یه متد برای استخراج به اکسل به نام ExportToExcel هم داره که میتونی استفاده کنی