Ejemplo n.º 1
0
        private static byte[] GetExcelBytes(byte[] reportTemplate, List <ReportDataSource> dataSources)
        {
            using (Stimulsoft.Report.StiReport stiReport = new Stimulsoft.Report.StiReport())
            {
                foreach (var dataSource in dataSources)
                {
                    stiReport.RegBusinessObject(dataSource.DataSourceName, dataSource.DataSource);
                }
                stiReport.Load(reportTemplate);
                stiReport.Render();


                byte[] result = null;
                using (MemoryStream stream = new MemoryStream())
                {
                    StiExcel2007ExportService excelService = new StiExcel2007ExportService();
                    excelService.ExportExcel(stiReport, stream);
                    result = stream.ToArray();
                }
                return(result);
            }
        }
Ejemplo n.º 2
0
        public static Exception ExportToExcel(ref DataTable dt, string OutputPath, bool HasPageHeader = true, bool HasHeaderBand = true,
                                              bool HasFooter = true, bool HasLineNo = false)
        {
            try
            {
                DateTime  ExportTimeStart = DateTime.Now;
                StiReport report          = new StiReport();
                StiPage   page            = report.Pages[0];
                page.PaperSize = System.Drawing.Printing.PaperKind.Custom;
                page.PageWidth = dt.Columns.Count * 1.2;
                StiHeaderBand headerBand = new StiHeaderBand();
                StiText       headerText = new StiText(new RectangleD(0, 0, page.Width, 0.5));
                page.RightToLeft = true;
                dt.TableName     = "Result";

                if (HasPageHeader)
                {
                    AddPageHeader(ref page, ref headerText);
                }

                if (HasFooter)
                {
                    AddFooter(ref page);
                }

                HasHeaderBand = false;
                if (HasHeaderBand)
                {
                    AddHasHeaderBand(ref headerText, ref page, ref headerBand);
                }

                #region Data
                //Create Databand
                StiDataBand dataBand = new StiDataBand();
                dataBand.DataSourceName = "Result";
                dataBand.Height         = 0.5;
                dataBand.CanGrow        = true;
                dataBand.GrowToHeight   = true;
                headerText.HorAlignment = StiTextHorAlignment.Right;
                dataBand.Name           = "DataBand1";
                page.Components.Add(dataBand);

                #endregion data

                double pos         = 0;
                double columnWidth = (page.Width - 1) / dt.Columns.Count;
                int    nameIndex   = 1;
                // StiDataTableSource userSource = new StiDataTableSource("Result", "Result", "Result");

                StiText hText    = new StiText();
                StiText dataText = new StiText();


                #region FillHeaderAndDate
                foreach (DataColumn dataColumn in dt.Columns)
                {
                    //userSource.Columns.Add(new Stimulsoft.Report.Dictionary.StiDataColumn(dataColumn.ColumnName, dataColumn.GetType()));

                    //Create text on header
                    //  StiText
                    hText = new StiText(new RectangleD(pos, 0, columnWidth, 0.5));
                    hText.HorAlignment  = StiTextHorAlignment.Center;
                    hText.VertAlignment = StiVertAlignment.Center;
                    hText.Font          = new System.Drawing.Font("Tahoma", 10);
                    hText.Brush         = new StiSolidBrush(Color.LightSteelBlue);
                    hText.Interaction.SortingEnabled = true;
                    hText.Interaction.SortingColumn  = "DataBand." + Stimulsoft.Report.CodeDom.StiCodeDomSerializator.ReplaceSymbols(dataColumn.ColumnName);
                    hText.Text.Value   = dataColumn.Caption;
                    hText.Name         = "HeaderText" + nameIndex.ToString();
                    hText.WordWrap     = true;
                    hText.CanGrow      = true;
                    hText.GrowToHeight = true;
                    hText.Border.Side  = StiBorderSides.All;
                    headerBand.Components.Add(hText);

                    //StiText
                    dataText = new StiText(new RectangleD(pos, 0, columnWidth, 0.5));
                    dataText.HorAlignment               = StiTextHorAlignment.Center;
                    dataText.VertAlignment              = StiVertAlignment.Center;
                    dataText.Font                       = new System.Drawing.Font("Tahoma", 10);
                    dataText.Text                       = "{Result." + Stimulsoft.Report.CodeDom.StiCodeDomSerializator.ReplaceSymbols(dataColumn.ColumnName) + "}";
                    dataText.Name                       = "DataText" + nameIndex.ToString();
                    dataText.WordWrap                   = true;
                    dataText.CanGrow                    = true;
                    dataText.GrowToHeight               = true;
                    dataText.ShrinkFontToFit            = true;
                    dataText.Border.Side                = StiBorderSides.All;
                    dataText.Interaction.SortingEnabled = true;
                    dataBand.Components.Add(dataText);
                    pos = pos + columnWidth;
                    nameIndex++;
                }
                #endregion

                if (HasLineNo)
                {
                    AddLineNo(ref hText, ref dataText, ref headerBand, ref dataBand);
                }
                string rptPath = System.IO.Path.GetTempFileName();
                report.RegData("Result", dt);
                //report.Save(rptPath);
                //report.Load(rptPath);
                //report.RegData("Result", dt);
                report.Design();
                //userSource.SetData()
                //userSource.Dictionary =


                //report.CacheAllData = true;
                report.Render(true);

                StiExcel2007ExportService excelExport = new StiExcel2007ExportService();
                //StiExcelExportService excelExport = new StiExcelExportService();
                excelExport.ExportExcel(report, OutputPath);


                DateTime ExportTimeEnd = DateTime.Now;
                ExportTime = (ExportTimeEnd - ExportTimeStart);

                return(null);
            }
            catch (Exception ex)
            {
                return(ex);
            }
        }