带有两个表的报表查看器

时间:2019-04-10 15:02:50

标签: c# asp.net-mvc reportviewer

我在asp.net mvc中创建报表查看器。但是我有2个表有关系,并且在报表查看器中,当我从其他表中导出报表时,数据不只是名称而已。 例如,我在Expense水中

但我导出时未在表中显示。

这是我的模特:

public class Expenses
{
    [Key]
    public int Expenses_Id { get; set; }
    public string Expenses_Type { get; set; }
}


 public class ExpenseDetails
{
    [Key]
    public int ExpenseDetails_Id { get; set; }

    public double Amount { get; set; }

    public DateTime DateExpense { get; set; }

    public int ExpensesId { get; set; }

    [ForeignKey("ExpensesId")]
    public virtual Expenses expenses { get; set; }
}

这是我的报告代码:

 public ActionResult Reports(string ReportType)
    {
        LocalReport localreport = new LocalReport();
        localreport.ReportPath = Server.MapPath("~/Reports/ExpenseReport.rdlc");

        ReportDataSource reportDataSource = new ReportDataSource();
        reportDataSource.Name = "ExpenseDataSet";
        reportDataSource.Value = _context.ExpenseDetails.ToList();
        localreport.DataSources.Add(reportDataSource);

        string reportType = ReportType;
        string mimeType;
        string encoding;
        string fileNameExtension;

        if (reportType == "Excel")
        {
            fileNameExtension = "xlsx";
        }

        if (reportType == "Word")
        {
            fileNameExtension = "docx";
        }

        if (reportType == "PDF")
        {
            fileNameExtension = "pdf";
        }

        else
        {
            fileNameExtension = "jpg";

        }

        string[] streams;
        Warning[] warnings;
        byte[] renderedByte;

        renderedByte = localreport.Render(reportType, "", out mimeType, out encoding, out fileNameExtension, out streams, out warnings);

        Response.AddHeader("content-disposition", "attachment;filename = expens_report." + fileNameExtension);

        return File(renderedByte, fileNameExtension);
    }

谢谢。

1 个答案:

答案 0 :(得分:0)

您是否刷新了.rdlc中的数据集?如果在添加外键之前创建了它,则需要删除.rdlc数据集和数据源,然后创建一个新的数据集。

如何创建DTO?有时,创建复杂的对象列表,用所需的数据填充并将其发送到rdlc会更容易。在这种情况下,您需要使用rdlc创建一个单独的项目,并添加对主项目的引用。这样,您就可以将DTO导入为对象。

            List<ExpenseDetailsDTO> list = new List<ExpenseDetailsDTO>()
        {
            new ExpenseDetailsDTO
            {
                Amount = 10,
                DateExpense = DateTime.UtcNow,
                ExpenseDetails_Id  = 1,
                Expenses_Id = 1,
                Expenses_Type = "asd"
            },
            new ExpenseDetailsDTO
            {
                Amount = 10,
                DateExpense = DateTime.UtcNow,
                ExpenseDetails_Id  = 1,
                Expenses_Id = 1,
                Expenses_Type = "asd"
            },
            new ExpenseDetailsDTO
            {
                Amount = 10,
                DateExpense = DateTime.UtcNow,
                ExpenseDetails_Id  = 1,
                Expenses_Id = 1,
                Expenses_Type = "asd"
            },
            new ExpenseDetailsDTO
            {
                Amount = 10,
                DateExpense = DateTime.UtcNow,
                ExpenseDetails_Id  = 1,
                Expenses_Id = 1,
                Expenses_Type = "asd"
            },
        };




        reportDataSource.Name = "testdatasource";
        reportDataSource.Value = list;
        localReport.DataSources.Add(reportDataSource);

至少在过去的4个月中,在迁移旧的API时,这种方法对我有用。