Winform平台下用viewer1展示报表默认的情况下,是没有导出按钮的,需要进行导出按钮的添加和对应的Click事件。
默认情况下生成后,如下图:(没有导出EXCEL的按钮)
添加导出EXCEl按钮后后:
1、添加导出EXCEL的方法
第一种办法在designer中添加按钮,具体代码如下:
//Remove the print button.
viewer1.Toolbar.ToolStrip.Items.RemoveAt(2);//移除
//Remove the extra separator.
viewer1.Toolbar.ToolStrip.Items.RemoveAt(1);
//Add a new button to the end of the tool strip with the caption "Excel."
ToolStripButton tsbExcel = new ToolStripButton("导出Excel");
viewer1.Toolbar.ToolStrip.Items.Add(tsbExcel);//添加
//Create a click event handler for the button.
tsbExcel.Click += new EventHandler(tsbExcel_Click);//Click事件
第二种办法可以在from_Load中进行按钮的添加
private ToolStripButton tsbExcel = new System.Windows.Forms.ToolStripButton();
private void Form1_Load(object sender, EventArgs e)
{
this.viewer1.Visible = true;
System.Windows.Forms.ToolStrip toolStrip;
System.Windows.Forms.ToolStripItem orgItem;
System.Windows.Forms.ToolStripButton orgBtn = null;
toolStrip = this.viewer1.Toolbar.ToolStrip;
orgItem = toolStrip.Items[2];
if (orgItem is System.Windows.Forms.ToolStripButton)
{
orgBtn = (System.Windows.Forms.ToolStripButton)orgItem;
}
toolStrip.Items.RemoveAt(2);
tsbExcel.Text = "导出";
tsbExcel.Image = orgBtn.Image;
tsbExcel.Enabled = false;
tsbExcel.Click += this.btnExcel_Click;
toolStrip.Items.Insert(2, tsbExcel);
GrapeCity.ActiveReports.PageReport rpt = new GrapeCity.ActiveReports.PageReport(new System.IO.FileInfo(@"RdlReport2.rdlx"));
GrapeCity.ActiveReports.Document.PageDocument rptb = new GrapeCity.ActiveReports.Document.PageDocument(rpt);
this.viewer1.LoadDocument(rptb);
}
两种办法都可以进行按钮的添加,但是我建议大家用第一种方法:
2、导出的click事件的编码:
导出EXCEL的形式有两种办法:
第一种Rendering Extensions
private void tsbExcel_Click(object sender, EventArgs e)
{
System.IO.DirectoryInfo outputDirectory = new System.IO.DirectoryInfo(@"D:\MyExcel");
outputDirectory.Create();
GrapeCity.ActiveReports.Export.Excel.Page.ExcelRenderingExtensionSettings excelSetting = new GrapeCity.ActiveReports.Export.Excel.Page.ExcelRenderingExtensionSettings();
excelSetting.FileFormat = GrapeCity.ActiveReports.Export.Excel.Page.FileFormat.Xls;
excelSetting.MultiSheet = false;
GrapeCity.ActiveReports.Extensibility.Rendering.ISettings setting = excelSetting;
GrapeCity.ActiveReports.Export.Excel.Page.ExcelRenderingExtension excelRenderingExtension = new GrapeCity.ActiveReports.Export.Excel.Page.ExcelRenderingExtension();
GrapeCity.ActiveReports.Rendering.IO.FileStreamProvider outputProvider = new GrapeCity.ActiveReports.Rendering.IO.FileStreamProvider(outputDirectory, System.IO.Path.GetFileNameWithoutExtension(outputDirectory.Name));
outputProvider.OverwriteOutputFile = false;
_document.Render(excelRenderingExtension, outputProvider, excelSetting.GetSettings());
}
第二种Export Filters
private void btnExcel_Click(object sender, EventArgs e)
{
GrapeCity.ActiveReports.PageReport rpt = new GrapeCity.ActiveReports.PageReport(new System.IO.FileInfo(@"RdlReport2.rdlx"));
GrapeCity.ActiveReports.Document.PageDocument _reportRuntime = new GrapeCity.ActiveReports.Document.PageDocument(rpt);
GrapeCity.ActiveReports.Export.Excel.Section.XlsExport XlsExport1 = new GrapeCity.ActiveReports.Export.Excel.Section.XlsExport();
System.IO.MemoryStream ms = new System.IO.MemoryStream();
XlsExport1.FileFormat = GrapeCity.ActiveReports.Export.Excel.Section.FileFormat.Xlsx;
XlsExport1.Export(_reportRuntime, ms);
}
源码下载:
了解ActiveReports产品更多特性:
下载产品体验产品功能:
标签: MVC , 报表 , 运行时数据源 , 中国式复杂报表 , ActiveReports 10 , 自动化报表