MSDN中有代码用于打印rdlc而无需在Web应用程序中预览但是当我在IIS中实现时它不起作用...是否有任何解决方案....或任何代码在没有rdlc打印的情况下工作预习 这是链接http://msdn.microsoft.com/en-us/library/ms252091.aspx 它不会给出任何例外,但也不会打印报告 在MSDN上,他们说创建一个控制台应用程序,但我需要在IIS中运行的asp.net Web应用程序。
using System;
using System.IO;
using System.Data;
using System.Text;
using System.Drawing.Imaging;
using System.Drawing.Printing;
using System.Collections.Generic;
using Microsoft.Reporting.WebForms;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Windows.Forms;
///<summary>
/// Summary description for Printing
/// this is the cool code found on MSDN site to print labels out without preview
/// abhay maini
///</summary>
publicclassPrinting : IDisposable
{
public Printing()
{
//
// TODO: Add constructor logic here
//
}
publicint m_currentPageIndex;
publicIList<Stream> m_streams;
// Routine to provide to the report renderer, in order to
// save an image for each page of the report.
publicStream CreateStream(string name,string fileNameExtension, Encoding encoding,string mimeType, bool willSeek)
{
string CurrentDrive;
CurrentDrive = Application.StartupPath.ToString();Stream stream = newFileStream("C:\\Labels\\" + name + "." + fileNameExtension, FileMode.Create);
m_streams.Add(stream);
return stream;
}
publicvoid Export(LocalReport report)
{
string deviceInfo =
"<DeviceInfo>" +
" <OutputFormat>EMF</OutputFormat>" +
" <PageWidth>4.0in</PageWidth>" +
" <PageHeight>2.0in</PageHeight>" +
" <MarginTop>0.00in</MarginTop>" +
" <MarginLeft>0.00in</MarginLeft>" +
" <MarginRight>0.00in</MarginRight>" +
" <MarginBottom>0.00in</MarginBottom>" +
"</DeviceInfo>";
Warning[] warnings;m_streams = newList<Stream>();
report.Render("Image", deviceInfo, CreateStream, out warnings);
foreach (Stream stream in m_streams)
stream.Position = 0;
}
// Handler for PrintPageEvents
publicvoid PrintPage(object sender, PrintPageEventArgs ev)
{
Metafile pageImage = newMetafile(m_streams[m_currentPageIndex]);
ev.Graphics.DrawImage(pageImage, ev.PageBounds);
m_currentPageIndex++;
ev.HasMorePages = (m_currentPageIndex < m_streams.Count);
}
publicvoid Print(string PrinterName)
{
// const string printerName = PrinterName;
if (m_streams == null || m_streams.Count == 0)
return;
PrintDocument printDoc = newPrintDocument();
printDoc.PrinterSettings.PrinterName = PrinterName;
if (!printDoc.PrinterSettings.IsValid)
{
string msg = String.Format(
"Can't find printer \"{0}\".", PrinterName);
MessageBox.Show(msg, "Print Error");return;
}
printDoc.PrintPage += newPrintPageEventHandler(PrintPage);
printDoc.Print();
}
// Create a local report for Report.rdlc, load the data,
// export the report to an .emf file, and print it.
publicvoid Run(string ReportName, string PrinterName, DataTable MyDataTable,string DSstring)
{
LocalReport report = newLocalReport();
report.ReportPath = ReportName;
report.DataSources.Clear();
report.DataSources.Add(newReportDataSource(DSstring, MyDataTable));
Export(report);
m_currentPageIndex = 0;
Print(PrinterName);
}
publicvoid Dispose()
{
if (m_streams != null)
{
foreach (Stream stream in m_streams)
stream.Close();
m_streams = null;
}
}
}
The above class can be called as below behind text change event:
protectedvoid TxtScanId_TextChanged(object sender, EventArgs e)
{
string sqlPrintScanID = "SELECT [ScanID], [LoadID], [tempVRMA], [CustPalletID], [TypeOfAsset] FROM [SerialScanDetail] WHERE [ScanID]=" + TxtScanId.Text + "";
string strConnection = ConfigurationManager.ConnectionStrings["RevisionConnectionString"].ToString();
SqlConnection conn = newSqlConnection(strConnection);
SqlDataAdapter da = newSqlDataAdapter();
da.SelectCommand = newSqlCommand(sqlPrintScanID, conn);
DataSet ds = newDataSet();
da.Fill(ds, "RevisionDataSet_SerialScanDetail");
//ReportViewer1.LocalReport.Refresh();
NewPrinting.Run(@"Reports\Report_ScanID.rdlc", "Eltron 2442", ds.Tables[0], "RevisionDataSet_SerialScanDetail");
}
答案 0 :(得分:0)
我建议使用ReportViewer类。
ReportViewer reportViewer = new ReportViewer();
reportViewer.LocalReport.ReportPath = "ReportPath";
reportViewer.LocalReport.DataSources.Add(new ReportDataSource("data", data));
byte[] byteInfo;
byteInfo = reportViewer.LocalReport.Render("Image", deviceInfo, CreateStream, out warnings);
MemoryStream ms = new MemoryStream(byteInfo);
Image returnImage = Image.FromStream(ms);
returnImage是您可以显示/打印的图像数据类型。
我看到您调用渲染但没有分配要使用的值,这与不使用报表查看器相结合可能是问题。
MSDN建议制作控制台应用程序的原因是,在基本程序(如控制台)中进行概念验证,获得所需结果,然后将该代码移植到所需环境中是个好主意。