How to use Stimulsoft Report for C# and Excel file importing in gridview?

99封情书 提交于 2019-12-11 14:12:12

问题


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


来源:https://stackoverflow.com/questions/49756787/how-to-use-stimulsoft-report-for-c-sharp-and-excel-file-importing-in-gridview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!