您可以根据需要制作rdlc报告,并通过控制器功能在您的视图中点击打印按钮/链接进行调用。
在你看来
Html.ActionLink("Print", "Print", new { id = c.sid })
你
控制器
public ActionResult Print(int id)
{
string unitc = Session["unit"].ToString();
ctid= unitc;//class level variable used in detailreport function
brid = id;//class level variable used in detailreport function
return DetailsReport();
}
FileContentResult DetailsReport()
{
LocalReport localReport = new LocalReport();
localReport.ReportPath = Server.MapPath("~/Reports/rptinvoice.rdlc");
InvoiceRepository ivr = new InvoiceRepository();
if (localReport.DataSources.Count > 0)
{
localReport.DataSources.RemoveAt(0);
localReport.DataSources.RemoveAt(1);
localReport.DataSources.RemoveAt(2);
}
localReport.Refresh();
ReportDataSource reportDataSource = new ReportDataSource("DataSet1", ivr.GetSales(ctid));
localReport.SetParameters(new ReportParameter[] { new ReportParameter("ct_id", ctid.ToString()), new ReportParameter("ct_br_id", brid.ToString()) });
localReport.DataSources.Add(reportDataSource);
string reportType = "PDF";
string mimeType;
string encoding;
string fileNameExtension;
string deviceInfo =
"<DeviceInfo>" +
" <OutputFormat>PDF</OutputFormat>" +
" <PageWidth>8.5in</PageWidth>" +
" <PageHeight>11in</PageHeight>" +
" <MarginTop>0.2in</MarginTop>" +
" <MarginLeft>0.05in</MarginLeft>" +
" <MarginRight>0.05in</MarginRight>" +
" <MarginBottom>0.1in</MarginBottom>" +
"</DeviceInfo>";
Warning[] warnings;
string[] streams;
byte[] renderedBytes;
localReport.EnableExternalImages = true;
//Render the report
try
{
renderedBytes = localReport.Render(
reportType,
deviceInfo,
out mimeType,
out encoding,
out fileNameExtension,
out streams,
out warnings);
}
catch (Exception Ex)
{
ViewData["ResultP"] = Ex.Message + ",<br>" + Ex.InnerException.Message;
throw;
}
return File(renderedBytes, mimeType);
}
我已经测试了iTextSharp的从头创建一个PDF的应用程序的不同部分,但它是凌乱的,它没有似乎可以按照我想要的方式进行格式化。因为我们熟悉它,所以我们很可能会从CrystalReport的路线生成一些东西。至于这个问题,你建议我创建一个没有输入控件(即文本框,网格)的新输出视图,并使用打印按钮来生成PDF。该输出视图是否必须显示,或者我可以使用您提到的方法直接转到PDF输出。 – Elim99
是的,您可以替换任何PDF生成库来代替iTextSharp。 就显示的视图而言,只要你返回一个文件,就像我在示例代码中那样,用户将得到一个“save/opn”对话框而不是HTML页面。 – nikmd23