代碼實現報表打印
代碼實現報表打印
//初始化報表信息
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();
//指定需要使用的打印機的名稱,使用空字符串""來指定默認打印機
// 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();
}
浙公網安備 33010602011771號