代碼實現報表列印
//初始化報表資訊
private void SetReportInfo(string reportPath,string sourceName,DataTable dataSource,bool isFengPi)
{
if (!File.Exists(reportPath))
{
MessageBox.Show("報表檔案:" + reportPath + " 不存在!","提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
if (dataSource == null || dataSource.Rows.Count == 0)
{
MessageBox.Show("沒有找到案卷號為:"+txtArchiveNum.Text.Trim()+"的相關目錄資訊", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
pos = 1;
LocalReport report1 = new LocalReport();
//設定需要列印的報表的檔案名稱。
report1.ReportPath = reportPath;
if (isFengPi)
{
//設定參數
string archveTypeName = GetArchiveTypeName();
ReportParameter archiveType = new ReportParameter("ArchiveType", archveTypeName);
report1.SetParameters(archiveType);
}
//建立要列印的資料來源
ReportDataSource source = new ReportDataSource(sourceName, dataSource);
report1.DataSources.Add(source);
//重新整理報表中的需要呈現的資料
report1.Refresh();
pos = 2;
m_streams = new List<Stream>();
string deviceInfo ="<DeviceInfo>" +
" <OutputFormat>EMF</OutputFormat>" +
" <PageWidth>21cm</PageWidth>" +
" <PageHeight>29.7cm</PageHeight>" +
" <MarginTop>2.0066cm</MarginTop>" +
" <MarginLeft>2.0066cm</MarginLeft>" +
" <MarginRight>2.0066cm</MarginRight>" +
" <MarginBottom>2.0066cm</MarginBottom>" +
"</DeviceInfo>";
Warning[] warnings;
//將報表的內容按照deviceInfo指定的格式輸出到CreateStream函數提供的Stream中。
report1.Render("Image", deviceInfo, CreateStream, out warnings);
}
//聲明一個Stream對象的列表用來儲存報表的輸出資料
//LocalReport對象的Render方法會將報表按頁輸出為多個Stream對象。
private List<Stream> m_streams;
//用來提供Stream對象的函數,用於LocalReport對象的Render方法的第三個參數。
private Stream CreateStream(string name, string fileNameExtension, Encoding encoding, string mimeType, bool willSeek)
{
pos = 3;
//如果需要將報表輸出的資料儲存為檔案,請使用FileStream對象。
Stream stream = new MemoryStream();
m_streams.Add(stream);
return stream;
}
//用來記錄當前列印到第幾頁了
private int m_currentPageIndex;
#region 列印報表
private void Print()
{
pos = 4;
m_currentPageIndex = 0;
if (m_streams == null || m_streams.Count == 0)
return;
//聲明PrintDocument對象用於資料的列印
PrintDocument printDoc = new PrintDocument();
//指定需要使用的印表機的名稱,使用Null 字元串""來指定預設印表機
// printDoc.PrinterSettings.PrinterName = "";
//判斷指定的印表機是否可用
if (!printDoc.PrinterSettings.IsValid)
{
MessageBox.Show("沒有找到印表機!","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
return;
}
pos = 5;
printDoc.PrintPage += new PrintPageEventHandler(PrintPage);
//執行列印操作,Print方法將觸發PrintPage事件。
printDoc.Print();
//釋放資源
foreach (Stream stream in m_streams)
{
stream.Dispose();
stream.Close();
}
m_streams = null;
}
private void PrintPage(object sender, PrintPageEventArgs ev)
{
pos =6;
//Metafile對象用來儲存EMF或WMF格式的圖形,
//我們在前面將報表的內容輸出為EMF圖形格式的資料流。
m_streams[m_currentPageIndex].Position = 0;
Metafile pageImage = new Metafile(m_streams[m_currentPageIndex]);
//指定是否橫向列印
ev.PageSettings.Landscape = false;
//這裡的Graphics對象實際指向了印表機
ev.Graphics.DrawImage(pageImage, ev.PageBounds);
m_streams[m_currentPageIndex].Close();
m_currentPageIndex++;
//設定是否需要繼續列印
ev.HasMorePages = (m_currentPageIndex < m_streams.Count);
}
#endregion
//列印封皮
private void btPrint_Click(object sender, EventArgs e)
{
string reportPath = Application.StartupPath + "\\Files\\ReportEnvelop.rdlc";
SetReportInfo(reportPath, "DataSet1", GetDataSource(true), true);
Print();
}