c# - 如何使用 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。
解决方案
请使用以下代码
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");
}
社区将努力解决问题或提供解决方法以实现您的结果。
//####----这只是测试运行。
步骤 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 步)请分享错误快照。我相信你会得到结果。如果是,那么我们将一起处理您的工作。
推荐阅读
- html - 是否可以通过输入一次 URL 来创建链接?
- sql - 在 SQL 中使用 HAVING 子句
- mysql - 在 Node.js MySQL 中等待查询
- flutter - 在flutter web中直接使用url参数导航到一个网页
- java - 运行时 Maven 上的 NoClassDefFoundError
- c++ - 删除多个 _mm256_blend_ps 会降低性能而不是提高性能
- python - python数据库结果不适用于if语句
- python-3.x - 具有字符串和整数元组键的字典上的 Python 数值运算
- c++ - 带有结构“未声明的标识符”的 C++ 类
- r - 根据不同列但同一行中的值识别一列中的值