C# 如何使用列表形式的代码创建PDF报告,而不是使用Microsoft Report Viewer直接从数据库创建数据

C# 如何使用列表形式的代码创建PDF报告,而不是使用Microsoft Report Viewer直接从数据库创建数据,c#,asp.net,pdf,rdlc,reportviewer,C#,Asp.net,Pdf,Rdlc,Reportviewer,我正在使用Microsoft.Report.Viewer在ASP.NET WEB API应用程序中生成PDF。到目前为止,当我的报告直接从数据库获取数据时,没有问题。问题是,当我试图创建一个包含子报表的报表,并且子报表数据是从列表形式的代码中获取的时,我没有得到预期的结果。我要做的是创建一个包含子报表的报表。但是当我生成PDF时,我得到的是这条消息 这是我的密码: 生成PDF的My Web API: [Route("GeneratePDFForDailyProgram")] [HttpG

我正在使用Microsoft.Report.Viewer在ASP.NET WEB API应用程序中生成PDF。到目前为止,当我的报告直接从数据库获取数据时,没有问题。问题是,当我试图创建一个包含子报表的报表,并且子报表数据是从列表形式的代码中获取的时,我没有得到预期的结果。我要做的是创建一个包含子报表的报表。但是当我生成PDF时,我得到的是这条消息

这是我的密码:

生成PDF的My Web API:

[Route("GeneratePDFForDailyProgram")]
    [HttpGet]
    [AllowAnonymous]
    public async Task<IHttpActionResult> GeneratePDFForDailyProgram(int id) {

        string guid = Guid.NewGuid().ToString();
        string fileName = string.Concat("Booking_No" + Convert.ToString(id) + "_" + guid.ToUpper() + ".pdf");
        string filePath = HttpContext.Current.Server.MapPath("~/Content/Temp/" + fileName);
        string name = Request.RequestUri.GetLeftPart(UriPartial.Authority) + System.Configuration.ConfigurationManager.ConnectionStrings[System.Configuration.ConfigurationManager.AppSettings["CUSTOMPORT"]];
        string name2 = Request.GetRequestContext().VirtualPathRoot;
        List<TourDestination> destination = new List<TourDestination>();

        TourTransactionSummaryViewModel summaryViewModel = new TourTransactionSummaryViewModel();

        try {

            //summaryViewModel = await CalcSummaryViewModel(transaction, summaryViewModel, db);

            summaryViewModel.DailyPrograms = DailyProgram(id);
            destination = TourDestination(summaryViewModel.DailyPrograms);

            List < Movement > movementList = summaryViewModel.DailyPrograms.FirstOrDefault().Movements.ToList();
            Reports.ReportGenerator.GeneratePDFForDailyProgram(summaryViewModel.DailyPrograms, destination, filePath);
            //await Reports.ReportGenerator.GeneratePDFForMovement(movementList,  filePath);


            HttpResponseMessage result = null;
            result = Request.CreateResponse(HttpStatusCode.OK);
            result.Content = new StreamContent(new FileStream(filePath, FileMode.Open));
            result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment");
            result.Content.Headers.ContentDisposition.FileName = fileName;

            result.Dispose();

            string convertedFilePath = filePath.Replace(@"\\", @"\");



        } catch (Exception ex) {

            return BadRequest(ex.Message);
        }

        return Ok(name + name2 + "/Content/Temp/" + fileName);
    }
[路由(“GeneratePDFForDailyProgram”)]
[HttpGet]
[异名]
公共异步任务GeneratePDFForDailyProgram(int id){
字符串guid=guid.NewGuid().ToString();
string fileName=string.Concat(“预订号”+Convert.ToString(id)+“+guid.ToUpper()+”.pdf”);
字符串filePath=HttpContext.Current.Server.MapPath(“~/Content/Temp/”+fileName);
字符串名称=Request.RequestUri.GetLeftPart(UriPartial.Authority)+System.Configuration.ConfigurationManager.connectionString[System.ConfigurationManager.AppSettings[“CUSTOMPORT”];
字符串名称2=Request.GetRequestContext().VirtualPath;
列表目的地=新列表();
TourTransactionSummaryViewModel summaryViewModel=新的TourTransactionSummaryViewModel();
试一试{
//summaryViewModel=等待CalcSummaryViewModel(事务,summaryViewModel,db);
summaryViewModel.DailyPrograms=DailyProgram(id);
目的地=旅游目的地(summaryViewModel.DailyPrograms);
ListmovementList=summaryViewModel.DailyPrograms.FirstOrDefault().Movements.ToList();
Reports.ReportGenerator.GeneratePDForDailyProgramm(summaryViewModel.DailyPrograms、destination、filePath);
//wait Reports.ReportGenerator.generatePDFORMOVE(移动列表,文件路径);
HttpResponseMessage结果=null;
结果=Request.CreateResponse(HttpStatusCode.OK);
result.Content=newstreamcontent(newfilestream(filePath,FileMode.Open));
result.Content.Headers.ContentDisposition=新的ContentDispositionHeaderValue(“附件”);
result.Content.Headers.ContentDisposition.FileName=文件名;
result.Dispose();
字符串convertedFilePath=filePath.Replace(@“\\”,@“\”);
}捕获(例外情况除外){
返回请求(例如消息);
}
返回Ok(name+name2+“/Content/Temp/”+fileName);
}
从上面的代码中可以看到,我使用GeneratepDFForDailyProgramm方法生成PDF

这里是我的GeneratePDFForDailyProgram方法:

public static bool GeneratePDFForDailyProgram(TourTransactionSummaryViewModel.DailyProgram[] dailyPrograms, List<TourDestination> destination , string filePath) {

        try {
            string binPath = System.IO.Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "bin");
            var assembly = Assembly.Load(System.IO.File.ReadAllBytes(binPath + "\\TripPlannerAPI.dll"));

            using (Stream stream = assembly.GetManifestResourceStream(DailyProgramReport)) {
                var viewer = new ReportViewer();
                viewer.LocalReport.EnableExternalImages = true;
                viewer.LocalReport.LoadReportDefinition(stream);

                Warning[] warnings;
                string[] streamids;
                string mimeType;
                string encoding;
                string filenameExtension;

                viewer.LocalReport.SubreportProcessing += new Microsoft.Reporting.WebForms.SubreportProcessingEventHandler(LocalReport_SubreportProcessing);
                viewer.LocalReport.DataSources.Add(new ReportDataSource("DailyProgram", dailyPrograms));


                byte[] bytes = viewer.LocalReport.Render(
                    "PDF", null, out mimeType, out encoding, out filenameExtension,
                    out streamids, out warnings);

                using (FileStream fs = new FileStream(filePath, FileMode.Create)) {
                    fs.Write(bytes, 0, bytes.Length);
                    fs.Flush();
                }

                stream.Flush();
            }
        } catch (Exception ex) {

            return false;
        }

        return true;
    }
public static bool generatePDForDailyProgram(TourTransactionSummaryViewModel.DailyProgram[]dailyPrograms,列表目标,字符串文件路径){
试一试{
字符串binPath=System.IO.Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory,“bin”);
var assembly=assembly.Load(System.IO.File.ReadAllBytes(binPath+“\\TripPlannerAPI.dll”);
使用(Stream=assembly.GetManifestResourceStream(DailyProgramReport)){
var viewer=new ReportViewer();
viewer.LocalReport.EnableExternalImages=true;
viewer.LocalReport.LoadReportDefinition(流);
警告[]警告;
字符串[]流线;
字符串模拟类型;
字符串编码;
字符串文件名扩展名;
viewer.LocalReport.SubreportProcessing+=新的Microsoft.Reporting.WebForms.SubreportProcessingEventHandler(LocalReport\u SubreportProcessing);
Add(newreportdatasource(“DailyProgram”,dailyPrograms));
byte[]bytes=viewer.LocalReport.Render(
“PDF”、null、输出mimeType、输出编码、输出文件名扩展、,
输出流线,输出警告);
使用(FileStream fs=newfilestream(filePath,FileMode.Create)){
fs.Write(字节,0,字节.长度);
fs.Flush();
}
stream.Flush();
}
}捕获(例外情况除外){
返回false;
}
返回true;
}
要运行子报表,我使用eventhandler代码:

private static void LocalReport_SubreportProcessing(object sender, SubreportProcessingEventArgs e) {

        DateTime movementDate = Convert.ToDateTime(e.Parameters[0].Values[0]);

        TourTransactionsController controller = new TourTransactionsController();

        var movement = controller.Movements();


        List<Movement> movementList = new List<Movement>();
        movementList.Add(new Movement {
            Destination = "TEST",
            MovementDescription = "TEST",
            DateTime =  Convert.ToDateTime("2017-09-25") //HARDCODE FOR TEST
        });

        e.DataSources.Clear();

        e.DataSources.Add(new Microsoft.Reporting.WebForms.ReportDataSource() {
            Name = "DSMovements",
            Value = movementList
        });

        //throw new NotImplementedException();
    }
private static void LocalReport\u SubreportProcessing(对象发送方,SubreportProcessingEventArgs e){
DateTime movementDate=Convert.ToDateTime(例如参数[0]。值[0]);
TourTransactionController=新的TourTransactionController();
var movement=controller.Movements();
List movementList=新列表();
移动列表。添加(新移动){
Destination=“TEST”,
MovementDescription=“TEST”,
DateTime=Convert.ToDateTime(“2017-09-25”)//测试的硬代码
});
e、 DataSources.Clear();
e、 添加(新的Microsoft.Reporting.WebForms.ReportDataSource(){
Name=“DSMovements”,
值=移动列表
});
//抛出新的NotImplementedException();
}
我尝试的是:

  • 检查子报表名称,并将其从报表名称更改为报表文件url。仍然得到同样的信息
  • 直接生成子报表,而不是从主报表生成。子报表已成功生成
  • PS:当我调试代码,并在事件处理程序
    LocalReport\u SubreportProcessing
    中放置断点时,调试时断点未命中

    我的问题是:

  • 为什么调试时eventhandler上的断点未命中
  • 调试时未命中的eventhandler是否可能是我收到消息“MovementReport”子报表无法被激活的原因 在指定的lo处找到