,您應該使用微軟報表查看器 http://www.microsoft.com/en-us/download/details.aspx?id=3841 添加到項目中的dll:Microsoft.ReportViewer.Common和Microsoft.ReportViewer.WinForms所以你必須創建這樣的* .rdlc文件可以更改的設計你的報告。最後爲了節省,你可以這樣做:
public static void PrintArticles(ICollectionView Articles)
{
try
{
var articlesRows = new DataTable("Articles");
articlesRows.Columns.Add("Id");
articlesRows.Columns.Add("Description");
var arts = Articles.Cast<Article>();
foreach (var art in arts)
{
articlesRows.Rows.Add(art.Id, art.Description);
}
ReportViewer reporter = new ReportViewer();
reporter.LocalReport.DisplayName = "Report1";
reporter.LocalReport.ReportPath = Path.Combine(Program.BasePath + "PrintArticles.rdlc");
reporter.LocalReport.DataSources.Clear();
reporter.LocalReport.DataSources.Add(new ReportDataSource("Project1", articlesRows));
byte[] report = reporter.LocalReport.Render("PDF");
reporter.LocalReport.ReleaseSandboxAppDomain();
string pdfpath = Path.Combine(Program.BasePath, "file.pdf");
if (File.Exists(pdfpath))
File.Delete(Path.Combine(Program.BasePath, "file.pdf"));
FileStream writer = new FileStream(pdfpath, FileMode.Create);
writer.Write(report, 0, report.Length);
writer.Close();
Process ar = Process.Start(pdfpath);
}
catch (Exception e)
{
}
}