WinForm应用程序的导出

Winform平台下用viewer1展示报表默认的情况下,是没有导出按钮的,需要进行导出按钮的添加和对应的Click事件。默认情况下生成后,如下图:(没有导出EXCEL的按钮)添加导出EXCEl

发布于 2017/08/31 00:00

ActiveReports

Winform平台下用viewer1展示报表默认的情况下,是没有导出按钮的,需要进行导出按钮的添加和对应的Click事件。

默认情况下生成后,如下图:(没有导出EXCEL的按钮

image

添加导出EXCEl按钮后后:

image

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产品更多特性:

/developer/activereports

下载产品体验产品功能:

/download/?pid=16

标签: MVC , 报表 , 运行时数据源 , 中国式复杂报表 , ActiveReports 10 , 自动化报表

 

ActiveReports 报表控件| 下载试用

ActiveReports 是一款专注于 .NET 平台的报表控件,全面满足 HTML5 / WinForm / ASP.NET / ASP.NET MVC / WPF 等平台下报表设计和开发工作需求,作为专业的报表工具为全球超过 300,000 开发人员提供了全面的报表开发服务。

您对ActiveReports产品的任何技术问题,都有技术支持工程师提供1对1专业解答,点击此处即可发帖提问>>技术支持论坛

相关产品
推荐相关案例
关注微信
葡萄城社区二维码

关注“葡萄城社区”

活字格低代码二维码

关注“活字格低代码”

想了解更多信息,请联系我们, 随时掌握技术资源和产品动态