首页 > 解决方案 > 如何使用 C# 在 ASP.NET MVC 中创建 Crystal Report?

问题描述

我正在努力在这里创建我的报告并需要一些帮助,我有这种方法并且希望我的文件扩展名不是 PDF 而是 XML。我尝试使用下面的逻辑,但仍然无法在我的SetDataSource.

public ActionResult Download_XMLReport()
{
    eNtsaRegistration context = new eNtsaRegistration();

    ReportDocument rpt = new ReportDocument();
    rpt.Load(Path.Combine(Server.MapPath("~/Reports"), "AcademyReports.rpt"));
    rpt.SetDataSource(from TrainingRegForm in context.TrainingRegs.Take(5) select TrainingRegForm);

    Response.Buffer = false;
    Response.ClearContent();
    Response.ClearHeaders();

    rpt.PrintOptions.PaperOrientation = CrystalDecisions.Shared.PaperOrientation.Landscape;
    rpt.PrintOptions.ApplyPageMargins(new CrystalDecisions.Shared.PageMargins(5, 5, 5, 5));
    rpt.PrintOptions.PaperSize = CrystalDecisions.Shared.PaperSize.PaperA5;

    Stream stream = rpt.ExportToStream(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat);
    stream.Seek(0, SeekOrigin.Begin);

    return File(stream, "application/xml", "eNtsaReportTrainingForm.xml");
}

// Table from the Database.
public class TrainingRegForm
{
        *[Key]
        public Guid? Id { get; set; } // Must this be now public abstract Guid Guid {get;set;}...??*
        public string Title { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Position { get; set; }
        public string Company { get; set; }

        public string StreetAddress { get; set; }
        public string StreetAddressLine { get; set; }
        public string City { get; set; }
        public string StateProvince { get; set; }
        public int ZipCode { get; set; }
        public string Email { get; set; }

        [Required(ErrorMessage = "This field is required")]
        [DataType(DataType.PhoneNumber)]
        public string CellNumber { get; set; }
        public string DietaryRequirement { get; set; }
}

// Nadeem 下面要求的附加 GUI。

在此处输入图像描述

在此处输入图像描述

标签: c#asp.net-mvcentity-frameworkcrystal-reports

解决方案


请使用以下代码

public ActionResult Download_XMLReport()
        {
            eNtsaRegistration context = new eNtsaRegistration();

            ReportDocument rpt = new ReportDocument();    
            var result =(from TrainingRegForm in context.TrainingRegs.Take(5) select
                  TrainingRegForm).ToList(); 

            rpt.Load(Path.Combine(Server.MapPath("~/Reports"), "AcademyReports.rpt"));
            rpt.SetDataSource(result); 
            Response.Buffer = false;
            Response.ClearContent();
            Response.ClearHeaders();

            rpt.PrintOptions.PaperOrientation = CrystalDecisions.Shared.PaperOrientation.Landscape;
            rpt.PrintOptions.ApplyPageMargins(new CrystalDecisions.Shared.PageMargins(5, 5, 5, 5));
            rpt.PrintOptions.PaperSize = CrystalDecisions.Shared.PaperSize.PaperA5;

            Stream stream = rpt.ExportToStream(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat);
            stream.Seek(0, SeekOrigin.Begin);




            return File(stream, "application/xml", "eNtsaReportTrainingForm.xml");
        }


        

请分享快照。我附上了样本快照供您参考。 SampleImageRequiredForProviding 解决方法

社区将努力解决问题或提供解决方法以实现您的结果。

使用 GUID

//####----这只是测试运行。

步骤 1) 创建一个新类

public class TrainingRegFormInfo
{

        public int w_Id { get; set; }
        public string w_Title { get; set; }
        public string w_FirstName { get; set; }
        public string w_LastName { get; set; }
        public string w_Position { get; set; }
        public string w_Company { get; set; }

        public string w_StreetAddress { get; set; }
        public string w_StreetAddressLine { get; set; }
        public string w_City { get; set; }
        public string w_StateProvince { get; set; }
        public int w_ZipCode { get; set; }
        public string w_Email { get; set; }

        public string w_CellNumber { get; set; }
        public string w_DietaryRequirement { get; set; }

}

第2步)

public ActionResult Download_XMLReport()
        {
         var _db = new eNtsaRegistration(); 

              var data = (from q in _db.TrainingRegForm
                        select new InvoiceInfo
                        {
              w_Title = q.DB_COLUMN_NAME,
              w_FirstName = q.DB_COLUMN_NAME,
            }).ToList();


            ReportDocument rpt = new ReportDocument();    
        rpt.Load(Server.MapPath("~/Reports/AcademyReports.rpt"));
            rpt.SetDataSource(data); 
            Response.Buffer = false;
            Response.ClearContent();
            Response.ClearHeaders();


    try
            {
            Stream stream = rpt.ExportToStream(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat);
            stream.Seek(0, SeekOrigin.Begin);
            return File(stream, "application/xml", "eNtsaReportTrainingForm.xml");
            }
    catch
            {
                throw;
            }
     }

步骤 3) 数据库专家 a) 为 TrainingRegFormInfo 类创建新数据源并将其字段添加到报告中。

第 4 步)请分享错误快照。我相信你会得到结果。如果是,那么我们将一起处理您的工作。


推荐阅读