生活随笔
收集整理的這篇文章主要介紹了
                                
C# 使用Microsoft.Reporting打印票据
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.                        
 
                                
                            
                            
                            打印票據(jù)的功能,很多軟件都用到,C# Winform開(kāi)發(fā)的桌面管理軟件,大多數(shù)情況下需要打印票據(jù)功能。作為成熟的軟件,打印票據(jù)的功能需要滿足如下條件:
 1、可以使用針式打印機(jī)自動(dòng)打印,自動(dòng)走紙。
 2、可以靈活設(shè)置紙張尺寸。
 3、票據(jù)設(shè)計(jì)和業(yè)務(wù)功能分開(kāi),數(shù)據(jù)可以自動(dòng)“灌輸”到票據(jù)模板中。
 4、可以動(dòng)態(tài)加載模板,方便各單位選擇適合自己的票據(jù)模板,模板升級(jí)不需要調(diào)整代碼。
 Microsoft.Reporting提供了票據(jù)打印功能,它可以操作畫(huà)布,通過(guò)自由繪制的方式打印各種報(bào)表,例如繪制表格,在指定位置打印文字等等。但是這種方式需要通過(guò)代碼操控,如果用戶(hù)提出需要調(diào)整票據(jù)格式,就需要升級(jí)程序。
 
有沒(méi)有辦法,報(bào)表的樣式不通過(guò)代碼操控,而是可視化編輯,最終表達(dá)到一個(gè)文件中,后期用戶(hù)如果要調(diào)整樣式,只需要升級(jí)這個(gè)文件呢?
 需要用到Microsoft.Reporting + RDLC,RDLC負(fù)責(zé)模板設(shè)計(jì),Microsoft.Reporting負(fù)責(zé)調(diào)用模板并控制打印。
 
```csharp
public class PrintHelper3{private Size rdlcPageSize 
= new Size();public LocalReport LoadFile(string rdlcfile
, string sourceName
, DataTable sourceTable
){LocalReport report 
= new LocalReport();report
.ReportPath 
= rdlcfile
;ReportDataSource source 
= new ReportDataSource(sourceName
, sourceTable
);report
.SetParameters(new ReportParameter[] {new ReportParameter("TimeNow", DateTime
.Now
.ToString()),new ReportParameter("ReportTitle", PubConstant
.REPORTTITLE
)});m_streams 
= new List<Stream>();report
.DataSources
.Add(source
);report
.Refresh();rdlcPageSize
.Width 
= report
.GetDefaultPageSettings().PaperSize
.Width
;rdlcPageSize
.Height 
= report
.GetDefaultPageSettings().PaperSize
.Height
;string deviceInfo 
="<DeviceInfo>" +"<OutputFormat>EMF</OutputFormat>" +"<PageWidth>21cm</PageWidth>" +"<PageHeight>13.9cm</PageHeight>" +"<MarginTop>0cm</MarginTop>" +"<MarginLeft>0cm</MarginLeft>" +"<MarginRight>0cm</MarginRight>" +"<MarginBottom>0cm</MarginBottom>" +"</DeviceInfo>";Warning[] warnings
;report
.Render("Image", deviceInfo
, CreateStream
, out warnings
);return report
;}private List<Stream> m_streams 
= null;private Stream CreateStream(string name
, string fileNameExtension
, Encoding encoding
, string mimeType
, bool willSeek
){Stream stream 
= new MemoryStream();m_streams
.Add(stream
);return stream
;}private int m_currentPageIndex 
= 0;public void Print(string printername
,Size size 
){m_currentPageIndex 
= 0;if (m_streams 
== null || m_streams
.Count 
== 0) return;PrintDocument printDoc 
= new PrintDocument();printDoc
.PrinterSettings
.PrinterName 
= printername
;if (!printDoc
.PrinterSettings
.IsValid
){MessageBox
.Show("未發(fā)現(xiàn)打印機(jī) " + printDoc
.PrinterSettings
.PrinterName
, "提示", MessageBoxButtons
.OK
, MessageBoxIcon
.Exclamation
);return;}printDoc
.DefaultPageSettings
.Landscape 
= false;printDoc
.DefaultPageSettings
.PaperSize 
= new PaperSize("Custom Size 1", size
.Width
,size
.Height
);printDoc
.PrintPage 
+= new PrintPageEventHandler(PrintPage
);printDoc
.Print();foreach (Stream stream 
in m_streams
){stream
.Dispose();stream
.Close();}m_streams 
= null;}private void PrintPage(object sender
, PrintPageEventArgs ev
){m_streams
[0].Position 
= 0;Metafile pageImage 
= new Metafile(m_streams
[0]);int w 
= Convert
.ToInt32(ev
.PageBounds
.Width 
/ 1.8);int h 
= Convert
.ToInt32(ev
.PageBounds
.Height 
/ 1.8);ev
.Graphics
.DrawImage(pageImage
, ev
.PageBounds
, 0, 0, w
, h
, System
.Drawing
.GraphicsUnit
.Millimeter
);m_streams
[m_currentPageIndex
].Close();m_currentPageIndex
++;ev
.HasMorePages 
= (m_currentPageIndex 
< m_streams
.Count
);} 
需要留意RDLC設(shè)計(jì)器版本,有些版本的設(shè)計(jì)器,輸出的RDLC文件,使用LocalReport載入后,生成到Meta的文件出現(xiàn)莫名其妙的字體放大,無(wú)法控制寬度等問(wèn)題,需要對(duì)尺寸等參數(shù)微調(diào)。
                            
總結(jié)
                            
                                以上是生活随笔為你收集整理的C# 使用Microsoft.Reporting打印票据的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
                            
                            
                                如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。