简体   繁体   English

如何使用 C# 在 ASP.NET MVC 中创建 Crystal Report?

[英]How to create Crystal Report in ASP.NET MVC using C#?

I am struggling to create my report well here and need some help, I have this method and want my file extension not be PDF but rather XML.我在这里努力创建我的报告并且需要一些帮助,我有这个方法并且希望我的文件扩展名不是 PDF 而是 XML。 I attempted using this below logic and still not getting it right on my SetDataSource .我尝试使用下面的逻辑,但仍然无法在我的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; }
}

// Additional GUI as requrested below by Nadeem. // Nadeem 下面要求的附加 GUI。

在此处输入图像描述

在此处输入图像描述

Please use below code请使用以下代码

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


        

Kindly share the snapshot.请分享快照。 I have attached sample snapshot for your reference.我附上了示例快照供您参考。 SampleImageRequiredForProviding 解决方法

Community will work to resolve the issue or provide workaround to achieve your result.社区将努力解决问题或提供解决方法以实现您的结果。

WorkingWithGUID 使用 GUID

//####----This is Just the TEST Run. //####----这只是测试运行。

Step 1) Create a new class步骤 1) 新建 class

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

}

Step 2)第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;
            }
     }

Step 3) Database Expert a)create new data source for TrainingRegFormInfo Class and add their fields to report.步骤 3) 数据库专家 a) 为 TrainingRegFormInfo Class 创建新数据源并将其字段添加到报告中。

Step 4) Please share snapshot of error.第 4 步)请分享错误快照。 I am sure you will get the result.我相信你会得到结果。 If Yes then we together will work on your stuff.如果是,那么我们将一起处理您的工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将参数设置为Crystal报表ASP.net MVC c# - Setting Parameter To Crystal report ASP.net MVC c# 在Crystal Report和ASP.NET C#中使用子报表 - Using Subreport in Crystal Report and ASP.NET C# C# ASP.NET MVC Crystal Report 参数值未传递给报告 - C# ASP.NET MVC Crystal Report parameter value not being passed to report 如何使用Visual Studio 2012在ASP.net C#中生成水晶报表? - How to generate crystal report in ASP.net C# using visual studio 2012? 如何使用 C# Asp.net 将水晶报表直接打印到客户端机器 - How to print crystal report directly to a client machine using C# Asp.net 如何使用asp.net C#在Crystal报表中插入图像? - How to Insert Image in crystal report using asp.net C#? 在 Visual Studio Community 2022 中使用 C# 在 ASP.NET Core 7 MVC 中创建报告 - Create Report in ASP.NET Core 7 MVC using C# in Visual Studio Community 2022 如何在不询问参数字段的情况下在Asp.Net C#中的Crystal Report中加载子报表 - How to load sub report in Crystal Report in Asp.Net C# with out asking Parameter fields 在ASP.NET C#中直接打印Crystal报表 - Print Crystal Report Directly in ASP.NET C# 通过asp.net中的C#将参数传递给CRYSTAL REPORT - passing parameter to the CRYSTAL REPORT through C# in asp.net
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM