2014-10-28 128 views
0

所以我有一个GridView的命令填充它。在每一行的一侧,我设置了一个链接按钮,其中显示“生成发票”。我已经将命令名定义为“GenerateInvoiceCommand”和命令参数OrderID,以便我可以获取有关该命令的所有数据。现在我的问题是...有没有什么办法可以生成PDF报告,并且一旦生成它就会自动开始下载?有没有什么办法可以做到这一点,而不必先插入pdf报告到数据库然后下载它?在ASP.NET C#中生成报表(发票)#

还有什么其他方式来生成这样的报告?

任何帮助表示赞赏!

谢谢!

+0

是的,你可以做到这一点,你看过iTextSharp吗? – 2014-10-28 10:34:59

+0

是的,我已经看过几个教程用于生成计算机文件系统的报告(通过Windows窗体),但不知道如何通过asp.net c#实现,并且我可以自动下载生成的文件而无需插入数据库。你能指点我正确的方向吗? :)链接,视频,任何可能有帮助的东西。 – perkes456 2014-10-28 10:40:42

回答

1

我可以看到有两种方式:

  1. 使用水晶报表生成PDF,this answer
  2. 编写自己的代码生成PDF,my answer

因为你已标记的Crystal Report ,你可能想首先使用,但为了获得对报告外观的更多控制,第二个选项看起来更好。

0

你应该先使用第三方软件,如水晶报表或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); 

希望它有助于

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;