最近做个项目遇到个棘手的问题,在做RDLC报表不预览直接打印功能时,调式状态下打印功能正常,但是发布后提示找不到打印机,计算机上已经安装了打印机。并且可用,请高手指教,源码如下:
private IList<Stream> m_streams;
//用来提供Stream对象的函数,用于LocalReport对象的Render方法的第三个参数。
private Stream CreateStream(string name, string fileNameExtension, Encoding encoding, string mimeType, bool willSeek)
{
//将报表输出的数据保存为文件,使用FileStream对象。
Stream stream = new MemoryStream();
//Stream stream = new FileStream(name + "." + fileNameExtension, FileMode.Create);
m_streams.Add(stream);
return stream;
}
/// <summary>
/// 关闭当前所有文件流
/// </summary>
private void CloseStreams()
{
if (this.m_streams != null && this.m_streams.Count > 0)
{
//foreach (Stream s in this.m_streams)
//{
// s.SetLength(0);
// s.Flush();
// s.Close();
//}
this.m_streams.Clear();
}
}
//将报表的内容输出为指定格式的数据流。
private void Export(LocalReport report)
{
string deviceInfo = "<DeviceInfo>" + "<OutputFormat>EMF</OutputFormat>" + "<PageWidth>8.54331</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>();
try
{
//将报表的内容按照deviceInfo指定的格式输出到CreateStream函数提供的Stream中。
report.Render("Image", deviceInfo, CreateStream, out warnings);
}
catch (Exception ex)
{
Exception innerEx = ex.InnerException;//取内异常。
while (innerEx != null)
{
//MessageBox.Show(innerEx.Message);
innerEx = innerEx.InnerException;
throw (innerEx);
}
}
}
//打印响应事件处理
private void PrintPage(object sender, PrintPageEventArgs ev)
{
//Metafile对象用来保存EMF或WMF格式的图形,
//将报表的内容输出为EMF图形格式的数据流。
m_streams[m_currentPageIndex].Position = 0;
Metafile pageImage = new Metafile(m_streams[m_currentPageIndex]);
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(System.Drawing.Brushes.White, adjustedRect);
// Draw the report content
ev.Graphics.DrawImage(pageImage, adjustedRect);
//指定是否横向打印
ev.PageSettings.Landscape = false;
m_currentPageIndex++;
//设置是否需要继续打印
ev.HasMorePages = (m_currentPageIndex < m_streams.Count);
}
// 当前呈现的页
private int m_currentPageIndex;
//打印
private void Print()
{
if (m_streams == null || m_streams.Count == 0)
return;
PrintDocument printDoc = new PrintDocument();
//判断指定的打印机是否可用
if (!printDoc.PrinterSettings.IsValid)
{
//MessageBox.Show("Can't find printer");
ClientScript.RegisterClientScriptBlock(this.GetType(), "HasSameRow", "<script type='text/javascript'>alert('找不到可用的打印机!')</script>");
return;
}
//声明PrintDocument对象的PrintPage事件,具体的打印操作需要在这个事件中处理。
printDoc.PrintPage += new PrintPageEventHandler(PrintPage);
//初始页设置
m_currentPageIndex = 0;
//执行打印操作,Print方法将触发PrintPage事件。
printDoc.Print();
}
BLL_Invoice bllinvoice = new BLL_Invoice();
Model.Invoice newInvoice = new Model.Invoice();
//报表设置及执行打印报表
private void Run()
{
DataSet ds;
ds = bllinvoice.SInvoice(newInvoice);
DataTable dt;
dt = bllinvoice.SInvoiceCuaddr(newInvoice);
LocalReport report = new LocalReport();
report.ReportPath = @"Page\Sales\SInvoicePrint.rdlc";//加上报表的路径
report.DataSources.Clear();
report.DataSources.Add(new ReportDataSource("DSInvoice", ds.Tables[0].DefaultView));
ReportParameter InvoiceID = new ReportParameter("Parameter_InvoiceNo", dt.Rows[0]["SaleInvoiceID"].ToString());
report.SetParameters(new ReportParameter[] { InvoiceID });
ReportParameter SaleDate = new ReportParameter("Parameter_Date", dt.Rows[0]["SaleOrderDate"].ToString());
report.SetParameters(new ReportParameter[] { SaleDate });
ReportParameter Route = new ReportParameter("Parameter_Route", dt.Rows[0]["RouteName"].ToString());
report.SetParameters(new ReportParameter[] { Route });
ReportParameter CustName = new ReportParameter("Parameter_CustName", dt.Rows[0]["CustomerName"].ToString());
report.SetParameters(new ReportParameter[] { CustName });
ReportParameter Addr = new ReportParameter("Parameter_Addr", dt.Rows[0]["Addr"].ToString());
report.SetParameters(new ReportParameter[] { Addr });
ReportParameter City = new ReportParameter("Parameter_City", dt.Rows[0]["City"].ToString());
report.SetParameters(new ReportParameter[] { City });
ReportParameter States = new ReportParameter("Parameter_States", dt.Rows[0]["States"].ToString());
report.SetParameters(new ReportParameter[] { States });
report.Refresh();
Export(report);
Print();
}
//打印选中行
protected void btnprint_Click(object sender, EventArgs e)
{
int rowNO= int.Parse( HFSelectedRow.Value);
Label lblInvoice = (Label)gv_SaleInvoiceList.Rows[rowNO - 1].FindControl("LBSaleInvoiceID");
newInvoice.InvoiceID = lblInvoice.Text;
if (newInvoice.InvoiceID!=null|| newInvoice.InvoiceID!="")
{
try
{
Run();
}
catch (Exception ex)
{
throw (ex);
}
finally
{
CloseStreams();
}
}
}