如何在gridview中使用Stimulsoft Report进行C#和Excel文件导入?

时间:2018-04-10 14:53:34

标签: c# report stimulsoft

我创建了一个应用程序,它加载一张Excel文件并在gridview中显示其内容。我只想使用Stimulsoft Report为所有记录创建报告并将其导出为PDF文件。我怎么能这样做?

1 个答案:

答案 0 :(得分:0)

如果您已经创建了StimulSoft报告模板,那么您可以参考以下代码来填充和注册您的数据并显示/导出。

public void CreateStimulSoftReport()
    {
        StiReport stReport = new StiReport();

        //If Template has already been created then load that template
        stReport.Load("REPORT_MRT_FILE_PATH");

        #region Report Data Creation Section

        DataSet reportDataSet = new DataSet("ReportDataSet");

        DataTable table = new System.Data.DataTable();
        table.TableName = "TABLE_NAME";
        table.Columns.Add(new DataColumn("Col1", typeof(int)));
        table.Columns.Add(new DataColumn("Col2", typeof(string)));
        table.Columns.Add(new DataColumn("Col3", typeof(string)));
        table.Columns.Add(new DataColumn("Col4", typeof(string)));

        //Fill Data in Table
        //Here GRID_VIEW_DATA_ITEMS is your ObservableCollection or any collection that is bound to your DataGrid
        foreach (GRID_VIEW_DATA_ITEM item in GRID_VIEW_DATA_ITEMS)
        {
            DataRow dr = table.NewRow();
            dr["Col1"] = item.VAL1;
            dr["Col2"] = item.VAL2;
            dr["Col3"] = item.VAL3;
            dr["Col4"] = item.VAL4;
            table.Rows.Add(dr);
        }

        reportDataSet.Tables.Add(table.Copy());

        #endregion

        //Register Report Dataset with Stimulsoft Report
        stReport.RegData(reportDataSet);
        stReport.Dictionary.Synchronize();

        //Display the Report and Save to any format from the Stimulsoft Report Viewer Window
        stReport.ShowWithWpf(); //For WPF application. Use customReport.Show() method for WinForms

        //If want to directly export to pdf file without displaying, then use below code.
        string exportFilename = "SOME_FILE_NAME_WITH_PATH";
        Stimulsoft.Report.Export.StiExportSettings exportSettings;
        exportSettings = new Stimulsoft.Report.Export.StiPdfExportSettings();
        ((Stimulsoft.Report.Export.StiPdfExportSettings)exportSettings).ImageQuality = 100;
        ((Stimulsoft.Report.Export.StiPdfExportSettings)exportSettings).ImageResolution = 500;
        ((Stimulsoft.Report.Export.StiPdfExportSettings)exportSettings).Compressed = false;
        stReport.ExportDocument(StiExportFormat.Pdf, exportFilename, exportSettings);
    }