在ASP.NET C#中生成报告(发票)

时间:2014-10-28 10:32:08

标签: c# asp.net webforms crystal-reports report

所以我有一个网格视图,订单填写它。在每一行的一侧,我设置了一个链接按钮,表示"生成发票"。我已将命令名定义为" GenerateInvoiceCommand"和命令参数OrderID,以便我可以获得有关该订单的所有数据。现在我的问题是......有什么方法可以生成PDF报告,一旦生成它自动开始下载?有没有办法在不先将pdf报告插入数据库然后下载的情况下才能做到这一点?

还有哪些方法可以生成这样的报告?

感谢任何帮助!

谢谢!

3 个答案:

答案 0 :(得分:1)

我可以看到有两种方式:

  1. 使用Crystal Report生成PDF this answer
  2. 编写自己的代码以生成PDF my answer
  3. 由于您已标记Crystal Report,因此您可能需要先使用,但为了获得对报表外观的更多控制,第二个选项看起来更好。

答案 1 :(得分:0)

您应首先使用第三方软件(如crystal-reports或stimulsoft )生成报告模板(myReport.mrt),然后执行以下操作:

StiReport report = new StiReport();
string path = "~/report/myReport.mrt";
report.Load(Server.MapPath(path));

// register your data to report's template

report.Render();
if (!Directory.Exists(Server.MapPath("~/report/PDF")))
     Directory.CreateDirectory(Server.MapPath("~/report/PDF"));
string ReportFileName = Server.MapPath("~/report/PDF/test.pdf");
report.ExportDocument(StiExportFormat.Pdf, ReportFileName);
FileStream file = File.Open(ReportFileName, FileMode.Open);
Response.Clear();
Response.AddHeader("Content-Disposition", "attachment; filename=test.pdf");
Response.AddHeader("Content-Length", file.Length.ToString());
Response.ContentType = "application/pdf";
file.Close();
Response.WriteFile(ReportFileName);

希望有所帮助

答案 2 :(得分:0)

您可以使用asp.net C#

中的以下代码以编程方式生成报告

您应该使用数据绑定数据表

        DataTable dt = GetDataTableFromDGV(AFIs, ArrayTitle, ArrayEnTitle, ArrayOrder, ArrayChecked);       

        DataView dataView = dt.DefaultView;

        report.ScriptLanguage = StiReportLanguageType.CSharp;
        report.RegData("view", dataView);
        //fill dictionary
        report.Dictionary.Synchronize();
        StiPage page = report.Pages.Items[0];
        if (Landscape)
            page.Orientation = StiPageOrientation.Landscape;
        else
            page.Orientation = StiPageOrientation.Portrait;

        //
        Double pos = 0;
        //Double columnWidth = StiAlignValue.AlignToMinGrid(page.Width / dataView.Table.Columns.Count, 0.1, true);

        Double columnWidth = StiAlignValue.AlignToMinGrid(page.Width / dt.Columns.Count, 0.1, true);
        int nameIndex = 1;

        columnWidth = StiAlignValue.AlignToMinGrid(page.Width / dt.Columns.Count, 0.1, true);

        //create ReportTitleBand
        StiReportTitleBand rt = new StiReportTitleBand();
        rt.Height = 1.5f;
        rt.Name = "ReportTitleBand";
        StiText st = new StiText(new RectangleD(0, 0, page.Width, 1f));
        st.Text.Value = ReportTitle;
        st.HorAlignment = StiTextHorAlignment.Center;
        st.Name = "TitleText1";
        st.Font = new Font(FontName, 16f);
        rt.Components.Add(st);
        page.Components.Add(rt);


        //create HeaderBand
        StiHeaderBand headerBand = new StiHeaderBand();
        if (chkRotate)
            headerBand.Height = 0.9f;
        else
            headerBand.Height = 0.5f;
        headerBand.Name = "HeaderBand";
        page.Components.Add(headerBand);


        //create Dataaband
        StiDataBand dataBand = new StiDataBand();
        dataBand.DataSourceName = "view" + dataView.Table.TableName;
        dataBand.Height = 0.5f;
        dataBand.Name = "DataBand";
        dataBand.CanBreak = true;//Added 11 20 2014     

        page.Components.Add(dataBand);

        //create FooterBand
        StiFooterBand footerBand = new StiFooterBand();
        footerBand.Height = 0.5f;
        footerBand.Name = "FooterBand";
        footerBand.Border = new StiBorder(StiBorderSides.All, Color.Black, 1, StiPenStyle.Solid);
        footerBand.PrintOnAllPages = true;            
        page.Components.Add(footerBand);            

        pos = (page.Width - (columnWidth * Convert.ToDouble(dataView.Table.Columns.Count))) / Convert.ToDouble(2);

        for (int i = dataView.Table.Columns.Count - 1; i != -1; i--)
        {
            DataColumn column = dataView.Table.Columns[i];

            //initilized column value
            Double headerHeight = 0.5f;
            if (chkRotate)
                headerHeight = 0.9f;

            StiText headerText = new StiText(new RectangleD(pos, 0, columnWidth, headerHeight));
            headerText.Text.Value = Stimulsoft.Report.CodeDom.StiCodeDomSerializator.ReplaceSymbols(column.Caption).Replace("_", " ");//ReplaceUnderLineWithSpace(column.Caption);
            if (chkRotate)
                headerText.Angle = 90;
            headerText.HorAlignment = StiTextHorAlignment.Center;
            headerText.VertAlignment = StiVertAlignment.Center;
            headerText.Name = "HeaderText" + nameIndex.ToString();
            headerText.Brush = new StiSolidBrush(Color.LightGreen);
            headerText.Border.Side = StiBorderSides.All;
            headerBand.Components.Add(headerText);
            headerText.Font = new Font(FontName, 11.0f);

            headerText.WordWrap = true;
            headerText.GrowToHeight = true;

            //initilized Data Band value
            StiText dataText = new StiText(new RectangleD(pos, 0, columnWidth, 0.5f));
            dataText.Text.Value = "{view" + dataView.Table.TableName + "." + Stimulsoft.Report.CodeDom.StiCodeDomSerializator.ReplaceSymbols(column.ColumnName) + "}";
            dataText.Name = "DataText" + nameIndex.ToString();
            dataText.HorAlignment = StiTextHorAlignment.Center;
            dataText.VertAlignment = StiVertAlignment.Center;
            dataText.Border.Side = StiBorderSides.All;

            dataText.WordWrap = true;
            dataText.GrowToHeight = true;
            dataText.Font = new Font(FontName, 11.0f);                

            //Add highlight
            if (true)
            {
                StiCondition condition = new StiCondition();
                condition.BackColor = Color.CornflowerBlue;
                condition.TextColor = Color.Black;
                condition.Expression = "(Line & 1) == 1";
                condition.Font = new Font(FontName, 11.0f);
                condition.Item = StiFilterItem.Expression;
                dataText.Conditions.Add(condition);
            }

            dataBand.Components.Add(dataText);

            pos += columnWidth;

            nameIndex++;
        }

        ////footer text
        StiText footerText = new StiText(new RectangleD(0, 0, page.Width, 0.5f));
        footerText.Text.Value = "صفحه {PageNumber}";
        footerText.HorAlignment = StiTextHorAlignment.Center;
        footerText.Name = "FooterText";
        footerText.Brush = new StiSolidBrush(Color.WhiteSmoke);
        footerText.Font = new Font(FontName, 11.0f);

        footerBand.Components.Add(footerText);

        report.Render(true);
        StiWebViewer1.ResetReport();
        StiWebViewer1.Report = report;