[英]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.
我附上了示例快照供您参考。
Community will work to resolve the issue or provide workaround to achieve your result.社区将努力解决问题或提供解决方法以实现您的结果。
//####----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.