How to use Stimulsoft Report for C# and Excel file

2019-08-16 08:05发布

问题:

I created an app which load a sheet of Excel file and shows its content in gridview. I just want to use Stimulsoft Report to create a report for all records and export it in PDF file. How can I do that ?

回答1:

If you already have the StimulSoft Report template created then you can refer the below code to fill and register your data with it and display/Export.

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);
    }