C# 如何在没有预览的情况下打印Reportviewer
我是c#的新手,目前正在制作一个可以打印收据的C# 如何在没有预览的情况下打印Reportviewer,c#,report,C#,Report,我是c#的新手,目前正在制作一个可以打印收据的POS应用程序。我使用了reportviewer组件来创建收据。我在工作。但是我不能直接传递print命令。预览时,我必须手动按下打印按钮。但我需要在没有预览的情况下自动打印。这是我的代码开头。有没有办法将此repotviewr与PrintDocument绑定,或者如何自动打印此reportviewer 开证收据 设置参数和绑定源 使用系统; 使用System.Collections.Generic; 使用系统组件模型; 使用系统数据; 使用系统图;
POS
应用程序。我使用了reportviewer组件来创建收据。我在工作。但是我不能直接传递print命令。预览时,我必须手动按下打印按钮。
但我需要在没有预览的情况下自动打印。
这是我的代码开头。有没有办法将此repotviewr
与PrintDocument
绑定,或者如何自动打印此reportviewer
开证收据
设置参数和绑定源
使用系统;
使用System.Collections.Generic;
使用系统组件模型;
使用系统数据;
使用系统图;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用System.Windows.Forms;
使用System.IO;
使用系统、绘图、打印;
使用Microsoft.Reporting.WinForms;
名称空间位置
{
公共部分类表单收据:MetroFramework.Forms.MetroForm
{
列表(u List),;
字符串\u总计、\u现金、\u零钱、\u日期、\u用户、\u折扣;
public formreceive(列表数据源、字符串总计、字符串现金、字符串更改、字符串日期、字符串折扣)
{
初始化组件();
_列表=数据源;
_总计=总计;
_现金=现金;
_改变=改变;
_日期=日期;
_user=Sessiondata.user;
_折扣=折扣;
}
私有void FormReceipt\u加载(对象发送方,事件参数e)
{
ReceiptBindingSource.DataSource=\u列表;
Microsoft.Reporting.WinForms.ReportParameter[]para=新的Microsoft.Reporting.WinForms.ReportParameter[]{
新的Microsoft.Reporting.WinForms.ReportParameter(“pTotal”,总共),
新的Microsoft.Reporting.WinForms.ReportParameter(“pCash”,U cash),
新的Microsoft.Reporting.WinForms.ReportParameter(“pChange”,u change),
新的Microsoft.Reporting.WinForms.ReportParameter(“pDate”,_date),
新的Microsoft.Reporting.WinForms.ReportParameter(“pUser”,用户),
新的Microsoft.Reporting.WinForms.ReportParameter(“pItems”,_list.Count.ToString()),
新的Microsoft.Reporting.WinForms.ReportParameter(“pDiscount”、\u折扣+“%”)
};
this.reportViewer1.LocalReport.SetParameters(第段);
this.reportViewer1.RefreshReport();
}
私有void reportViewer1_加载(对象发送方,事件参数e)
{
}
}
}
我正在发布我的代码,它可以按照您的要求正常工作。检查此代码,并根据需要在代码中进行自定义
List<Receipt> _list;
string _total, _cash, _change, _date, _user, _discount;
public FormReceipt(List<Receipt> datasource, string total,string cash, string change, string date, string discount)
{
InitializeComponent();
_list = datasource;
_total = total;
_cash = cash;
_change = change;
_date = date;
_user = Sessiondata.user;
_discount = discount;
}
private void FormReceipt_Load(object sender, EventArgs e)
{
ReceiptBindingSource.DataSource = _list;
Microsoft.Reporting.WinForms.ReportParameter[] para = new Microsoft.Reporting.WinForms.ReportParameter[]{
new Microsoft.Reporting.WinForms.ReportParameter("pTotal",_total),
new Microsoft.Reporting.WinForms.ReportParameter("pCash",_cash),
new Microsoft.Reporting.WinForms.ReportParameter("pChange",_change),
new Microsoft.Reporting.WinForms.ReportParameter("pDate",_date),
new Microsoft.Reporting.WinForms.ReportParameter("pUser",_user),
new Microsoft.Reporting.WinForms.ReportParameter("pItems",_list.Count.ToString()),
new Microsoft.Reporting.WinForms.ReportParameter("pDiscount",_discount+"%")
};
this.reportViewer1.LocalReport.SetParameters(para);
this.reportViewer1.RefreshReport();
Export(ReportViewer1.LocalReport, false);
Print();
Dispose();
}
private int m_currentPageIndex;
private IList<Stream> m_streams;
private Stream CreateStream(string name,
string fileNameExtension, Encoding encoding,
string mimeType, bool willSeek)
{
Stream stream = new MemoryStream();
m_streams.Add(stream);
return stream;
}
private void Export(LocalReport report, bool isLandscape)
{
string deviceInfo = string.Empty;
if (isLandscape)
{
deviceInfo =
@"<DeviceInfo>
<OutputFormat>EMF</OutputFormat>
<PageWidth>11in</PageWidth>
<PageHeight>8.5in</PageHeight>
<MarginTop>0.25in</MarginTop>
<MarginLeft>0.25in</MarginLeft>
<MarginRight>0.25in</MarginRight>
<MarginBottom>0.25in</MarginBottom>
</DeviceInfo>";
}
else
{
deviceInfo =
@"<DeviceInfo>
<OutputFormat>EMF</OutputFormat>
<PageWidth>8.5in</PageWidth>
<PageHeight>11in</PageHeight>
<MarginTop>0.25in</MarginTop>
<MarginLeft>0.25in</MarginLeft>
<MarginRight>0.25in</MarginRight>
<MarginBottom>0.25in</MarginBottom>
</DeviceInfo>";
}
Warning[] warnings;
m_streams = new List<Stream>();
// Create Report DataSource
ReportDataSource rds = new ReportDataSource();
rds.Value = _list;
ReportViewer1.LocalReport.DataSources.Clear();
ReportViewer1.LocalReport.DataSources.Add(rds);
report.Render("Image", deviceInfo, CreateStream,
out warnings);
foreach (Stream stream in m_streams)
stream.Position = 0;
}
private void Print()
{
PrinterSettings settings = new PrinterSettings(); //set printer settings
string printerName = settings.PrinterName; //use default printer name
if (m_streams == null || m_streams.Count == 0)
throw new Exception("Error: no stream to print.");
PrintDocument printDoc = new PrintDocument();
if (!printDoc.PrinterSettings.IsValid)
{
Response.Write("<script>alert('" + printerName + "')</script>");
}
else
{
printDoc.PrintPage += new PrintPageEventHandler(PrintPage);
m_currentPageIndex = 0;
printDoc.Print();
}
}
private void PrintPage(object sender, PrintPageEventArgs ev)
{
Metafile pageImage = new
Metafile(m_streams[m_currentPageIndex]);
ev.Graphics.DrawImage(pageImage, ev.PageBounds);
m_currentPageIndex++;
ev.HasMorePages = (m_currentPageIndex < m_streams.Count);
}
public void Dispose()
{
if (m_streams != null)
{
foreach (Stream stream in m_streams)
stream.Close();
m_streams = null;
}
}
List\u List;
字符串\u总计、\u现金、\u零钱、\u日期、\u用户、\u折扣;
public formreceive(列表数据源、字符串总计、字符串现金、字符串更改、字符串日期、字符串折扣)
{
初始化组件();
_列表=数据源;
_总计=总计;
_现金=现金;
_改变=改变;
_日期=日期;
_user=Sessiondata.user;
_折扣=折扣;
}
私有void FormReceipt\u加载(对象发送方,事件参数e)
{
ReceiptBindingSource.DataSource=\u列表;
Microsoft.Reporting.WinForms.ReportParameter[]para=新的Microsoft.Reporting.WinForms.ReportParameter[]{
新的Microsoft.Reporting.WinForms.ReportParameter(“pTotal”,总共),
新的Microsoft.Reporting.WinForms.ReportParameter(“pCash”,U cash),
新的Microsoft.Reporting.WinForms.ReportParameter(“pChange”,u change),
新的Microsoft.Reporting.WinForms.ReportParameter(“pDate”,_date),
新的Microsoft.Reporting.WinForms.ReportParameter(“pUser”,用户),
新的Microsoft.Reporting.WinForms.ReportParameter(“pItems”,_list.Count.ToString()),
新的Microsoft.Reporting.WinForms.ReportParameter(“pDiscount”、\u折扣+“%”)
};
this.reportViewer1.LocalReport.SetParameters(第段);
this.reportViewer1.RefreshReport();
导出(ReportViewer1.LocalReport,false);
打印();
处置();
}
私有int m_currentPageIndex;
私人IList m_溪;
私有流CreateStream(字符串名称,
字符串文件名扩展名,编码,
字符串mimeType,bool willSeek)
{
Stream=新的MemoryStream();
m_streams.Add(stream);
回流;
}
私有void导出(本地报告、bool isLandscape)
{
string deviceInfo=string.Empty;
如果(isLandscape)
{
设备信息=
@"
电动势
11英寸
8.5英寸
0.25英寸
0.25英寸
0.25英寸
0.25英寸
";
}
其他的
{
设备信息=
@"
电动势
8.5英寸
11英寸
0.25英寸
0.25英寸
0.25英寸
0.25英寸
";
}
警告[]警告;
m_streams=新列表();
//创建报表数据源
ReportDataSource rds=新的ReportDataSource();
rds.Value=_列表;
ReportViewer1.LocalReport.DataSources.Clear();
ReportViewer1.LocalReport.DataSources.Add(rds);
report.Render(“图像”、deviceInfo、CreateStream、,
发出警告);
foreach(m_streams中的流)
流位置=0;
}
私人作废打印()
{
PrinterSettings=new PrinterSettings();//设置打印机设置
字符串printerName=settings.printerName;//使用默认打印机名称
if(m|u streams==null | m|u streams.Count==0)
抛出新异常(“错误:没有要打印的流”);
PrintDocument printDoc=新的PrintDocument();
如果(!printDoc.PrinterSettings.IsValid)
{
响应。写入(“警报(““+printerName+”)”);
}
其他的
{
printDoc.PrintPage+=新的PrintPageEventHandler(PrintPage);
m_currentPageIndex=0;
printDoc.Print();
}
}
私有void PrintPage(对象发送方,PrintPageEventAr)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Drawing.Printing;
using Microsoft.Reporting.WinForms;
namespace Pos
{
public partial class FormReceipt : MetroFramework.Forms.MetroForm
{
List<Receipt> _list;
string _total, _cash, _change, _date, _user, _discount;
public FormReceipt(List<Receipt> datasource, string total,string cash, string change, string date, string discount)
{
InitializeComponent();
_list = datasource;
_total = total;
_cash = cash;
_change = change;
_date = date;
_user = Sessiondata.user;
_discount = discount;
}
private void FormReceipt_Load(object sender, EventArgs e)
{
ReceiptBindingSource.DataSource = _list;
Microsoft.Reporting.WinForms.ReportParameter[] para = new Microsoft.Reporting.WinForms.ReportParameter[]{
new Microsoft.Reporting.WinForms.ReportParameter("pTotal",_total),
new Microsoft.Reporting.WinForms.ReportParameter("pCash",_cash),
new Microsoft.Reporting.WinForms.ReportParameter("pChange",_change),
new Microsoft.Reporting.WinForms.ReportParameter("pDate",_date),
new Microsoft.Reporting.WinForms.ReportParameter("pUser",_user),
new Microsoft.Reporting.WinForms.ReportParameter("pItems",_list.Count.ToString()),
new Microsoft.Reporting.WinForms.ReportParameter("pDiscount",_discount+"%")
};
this.reportViewer1.LocalReport.SetParameters(para);
this.reportViewer1.RefreshReport();
}
private void reportViewer1_Load(object sender, EventArgs e)
{
}
}
}
List<Receipt> _list;
string _total, _cash, _change, _date, _user, _discount;
public FormReceipt(List<Receipt> datasource, string total,string cash, string change, string date, string discount)
{
InitializeComponent();
_list = datasource;
_total = total;
_cash = cash;
_change = change;
_date = date;
_user = Sessiondata.user;
_discount = discount;
}
private void FormReceipt_Load(object sender, EventArgs e)
{
ReceiptBindingSource.DataSource = _list;
Microsoft.Reporting.WinForms.ReportParameter[] para = new Microsoft.Reporting.WinForms.ReportParameter[]{
new Microsoft.Reporting.WinForms.ReportParameter("pTotal",_total),
new Microsoft.Reporting.WinForms.ReportParameter("pCash",_cash),
new Microsoft.Reporting.WinForms.ReportParameter("pChange",_change),
new Microsoft.Reporting.WinForms.ReportParameter("pDate",_date),
new Microsoft.Reporting.WinForms.ReportParameter("pUser",_user),
new Microsoft.Reporting.WinForms.ReportParameter("pItems",_list.Count.ToString()),
new Microsoft.Reporting.WinForms.ReportParameter("pDiscount",_discount+"%")
};
this.reportViewer1.LocalReport.SetParameters(para);
this.reportViewer1.RefreshReport();
Export(ReportViewer1.LocalReport, false);
Print();
Dispose();
}
private int m_currentPageIndex;
private IList<Stream> m_streams;
private Stream CreateStream(string name,
string fileNameExtension, Encoding encoding,
string mimeType, bool willSeek)
{
Stream stream = new MemoryStream();
m_streams.Add(stream);
return stream;
}
private void Export(LocalReport report, bool isLandscape)
{
string deviceInfo = string.Empty;
if (isLandscape)
{
deviceInfo =
@"<DeviceInfo>
<OutputFormat>EMF</OutputFormat>
<PageWidth>11in</PageWidth>
<PageHeight>8.5in</PageHeight>
<MarginTop>0.25in</MarginTop>
<MarginLeft>0.25in</MarginLeft>
<MarginRight>0.25in</MarginRight>
<MarginBottom>0.25in</MarginBottom>
</DeviceInfo>";
}
else
{
deviceInfo =
@"<DeviceInfo>
<OutputFormat>EMF</OutputFormat>
<PageWidth>8.5in</PageWidth>
<PageHeight>11in</PageHeight>
<MarginTop>0.25in</MarginTop>
<MarginLeft>0.25in</MarginLeft>
<MarginRight>0.25in</MarginRight>
<MarginBottom>0.25in</MarginBottom>
</DeviceInfo>";
}
Warning[] warnings;
m_streams = new List<Stream>();
// Create Report DataSource
ReportDataSource rds = new ReportDataSource();
rds.Value = _list;
ReportViewer1.LocalReport.DataSources.Clear();
ReportViewer1.LocalReport.DataSources.Add(rds);
report.Render("Image", deviceInfo, CreateStream,
out warnings);
foreach (Stream stream in m_streams)
stream.Position = 0;
}
private void Print()
{
PrinterSettings settings = new PrinterSettings(); //set printer settings
string printerName = settings.PrinterName; //use default printer name
if (m_streams == null || m_streams.Count == 0)
throw new Exception("Error: no stream to print.");
PrintDocument printDoc = new PrintDocument();
if (!printDoc.PrinterSettings.IsValid)
{
Response.Write("<script>alert('" + printerName + "')</script>");
}
else
{
printDoc.PrintPage += new PrintPageEventHandler(PrintPage);
m_currentPageIndex = 0;
printDoc.Print();
}
}
private void PrintPage(object sender, PrintPageEventArgs ev)
{
Metafile pageImage = new
Metafile(m_streams[m_currentPageIndex]);
ev.Graphics.DrawImage(pageImage, ev.PageBounds);
m_currentPageIndex++;
ev.HasMorePages = (m_currentPageIndex < m_streams.Count);
}
public void Dispose()
{
if (m_streams != null)
{
foreach (Stream stream in m_streams)
stream.Close();
m_streams = null;
}
}
private static List<Stream> m_streams;
private static int m_currentPageIndex = 0;
static LocalReport report = new LocalReport();
public static void PrintToPrinter(LocalReport report)
{
Export(report);
}
public static void Export(LocalReport report, bool print = true)
{
string deviceInfo =
@"<DeviceInfo>
<OutputFormat>EMF</OutputFormat>
<PageWidth>{pageSettings.PaperSize.Width * 100}in</PageWidth>
<PageHeight>{pageSettings.PaperSize.Height * 100}in</PageHeight>
<MarginTop>{pageSettings.Margins.Top * 100}in</MarginTop>
<MarginLeft>{pageSettings.Margins.Left * 100}in</MarginLeft>
<MarginRight>{pageSettings.Margins.Right * 100}in</MarginRight>
<MarginBottom>{pageSettings.Margins.Bottom * 100}in</MarginBottom>
</DeviceInfo>";
Warning[] warnings;
m_streams = new List<Stream>();
report.Render("Image", deviceInfo, CreateStream, out warnings);
foreach (Stream stream in m_streams)
stream.Position = 0;
if (print)
{
Print();
}
}
public static void Print()
{
if (m_streams == null || m_streams.Count == 0)
throw new Exception("Error: no stream to print.");
PrintDocument printDoc = new PrintDocument();
if (!printDoc.PrinterSettings.IsValid)
{
throw new Exception("Error: cannot find the default printer.");
}
else
{
printDoc.PrintPage += new PrintPageEventHandler(PrintPage);
m_currentPageIndex = 0;
printDoc.Print();
}
}
public static Stream CreateStream(string name, string fileNameExtension, Encoding encoding, string mimeType, bool willSeek)
{
Stream stream = new MemoryStream();
m_streams.Add(stream);
return stream;
}
public static void PrintPage(object sender, PrintPageEventArgs ev)
{
Metafile pageImage = new
Metafile(m_streams[m_currentPageIndex]);
// Adjust rectangular area with printer margins.
Rectangle adjustedRect = new Rectangle(
ev.PageBounds.Left - (int)ev.PageSettings.HardMarginX,
ev.PageBounds.Top - (int)ev.PageSettings.HardMarginY,
ev.PageBounds.Width,
ev.PageBounds.Height);
// Draw a white background for the report
ev.Graphics.FillRectangle(Brushes.White, adjustedRect);
// Draw the report content
ev.Graphics.DrawImage(pageImage, adjustedRect);
// Prepare for the next page. Make sure we haven't hit the end.
m_currentPageIndex++;
ev.HasMorePages = (m_currentPageIndex < m_streams.Count);
}
public static void DisposePrint()
{
if (m_streams != null)
{
foreach (Stream stream in m_streams)
stream.Close();
m_streams = null;
}
}