private void Run()
{
LocalReport report = new LocalReport();
report.ReportPath = @"F:\SelfSystemPractice\Report\sqlserverreport\sqlserverreport\Report1.rdl";
string connstr = @"Data Source=WIN-MT5JMJEOHPK;Initial Catalog=Study;Integrated Security=True";
SqlConnection conn = new SqlConnection(connstr);
string sql = "select usercode,username,address from sys_user";
SqlDataAdapter adapter = new SqlDataAdapter(sql, conn);
DataSet set = new DataSet();
adapter.Fill(set);
ReportDataSource DataSeta = new ReportDataSource("DataSet1", set.Tables[0]);
report.DataSources.Add(DataSeta);
Export(report);
m_currentPageIndex = 0;
Print();
}
private void Export(LocalReport report)
{
string deviceInfo =
"<DeviceInfo>" +
" <OutputFormat>EMF</OutputFormat>" +
//" <PageWidth>2in</PageWidth>" +
//" <PageHeight>20in</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>();
report.Render("Image", deviceInfo, CreateStream, out warnings);
foreach (Stream stream in m_streams)
stream.Position = 0;
}
private void Print()
{
const string printerName =
"Microsoft XPS Document Writer";
if (m_streams == null || m_streams.Count == 0)
return;
PrintDocument printDoc = new PrintDocument();
printDoc.PrinterSettings.PrinterName = printerName;
if (!printDoc.PrinterSettings.IsValid)
{
string msg = String.Format("Can't find printer \"{0}\".",
printerName);
Debug.WriteLine(msg);
return;
}
printDoc.PrintPage += new PrintPageEventHandler(PrintPage);
printDoc.Print();
}
private void PrintPage(object sender, PrintPageEventArgs ev)
{
Metafile pageImage =
new Metafile(m_streams[m_currentPageIndex]);
ev.Graphics.DrawImage(pageImage, 0, 0);
m_currentPageIndex++;
ev.HasMorePages = (m_currentPageIndex < m_streams.Count);
}
private Stream CreateStream(string name, string fileNameExtension,
Encoding encoding, string mimeType, bool willSeek)
{
Stream stream = new FileStream(name + "." + fileNameExtension,
FileMode.Create);
m_streams.Add(stream);
return stream;
}