Pages

Tuesday, June 29, 2010

Report viewer Print without preview

เวลาที่ทำโปรแกรมสักอย่างจะต้องมีรายงานอาจจะมากหรือน้อยก็แล้วแต่ว่า user ต้องการ ปกติผมจะใช้ Report viewer ในการสร้างรายงาน เช่น ใบสั่งซื้อ ใบรายการสินค้าต่าง ๆ

ปกติถ้าเราใช้ Report viewer จะต้องลาง control มาว่างใน form แต่ทีนี้ผมต้องการแบบไม่ใช้ control วางใน form ให้สามารถกดปุ่มพิมพ์แล้วพิมพ์ได้เลยโดยไม่ต้องแสดง report viewer ก็

สำหรับวิธีการทำ

Tools ที่ผมใช้
  1. Visual Studio 2008
  2. MS SqlServer 2008
  3. Report viewer 2008 (ตัวนี้จะต้องตาม vs ที่เราใช้นะครับ)
  4. คอม - -" (ไม่ใช้คอมจะทำไงวะเนี่ย)
มาเริ่มกันเลย

  1. สร้าง Form ขึ้นมาสักอัน
  2. สร้าง dataset
  3. สร้าง Report.rdlc พร้อมทั้ง fomat ของข้อมูลที่ต้องการแสดง
  4. หลังจากที่เราได้ครบแล้วก็มาเขียน code กันเลย

public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

ReportViewer ReportViewer1 = new ReportViewer();
byte[] bytes = null;
private void button1_Click(object sender, EventArgs e)
{
//this.dataSet11.Customers.WriteXml("data1.xml");
Run();
}

private void Form1_Load(object sender, EventArgs e)
{
customersTableAdapter1.Fill(this.dataSet1.Customers);
this.dataSet1.Customers.WriteXml("data1.xml");
}

private int m_currentPageIndex;

private IList m_streams;

private DataTable LoadSalesData()
{
DataSet dataSet = new DataSet();
dataSet.ReadXml("data1.xml");
return dataSet.Tables[0];
}

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;
}

private void Export(LocalReport report)
{
string deviceInfo =
"" +
" EMF" +
" 8.5in" +
" 11in" +
" 0.25in" +
" 0.25in" +
" 0.25in" +
" 0.25in" +
"";
Warning[] warnings;
m_streams = new List();
//report.Render("Image", deviceInfo, CreateStream, out warnings);
//Warning[] warnings;
string[] streamids;
string mimeType;
string encoding;
string extension;

bytes = ReportViewer1.LocalReport.Render(
"PDF", null, out mimeType, out encoding,
out extension,
out streamids, out warnings);

FileStream fs = new FileStream(@"D:\output.PDF",
FileMode.Create);
fs.Write(bytes, 0, bytes.Length);
fs.Close();


foreach (Stream stream in m_streams)
stream.Position = 0;
}

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 <>
}

private void Print()
{
const string printerName = "Microsoft Office Document Image Writer";

if (m_streams == null || m_streams.Count == 0)
return;

PrintDocument printDoc = new PrintDocument();
System.Windows.Forms.PrintDialog printDialog = new System.Windows.Forms.PrintDialog();
printDialog.Document = printDoc;
if (printDialog.ShowDialog() == System.Windows.Forms.DialogResult.Cancel)
return;

printDoc.PrinterSettings.PrinterName = printDialog.PrinterSettings.PrinterName;
if (!printDoc.PrinterSettings.IsValid)
{
string msg = String.Format("Can't find printer \"{0}\".", printerName);
Console.WriteLine(msg);
return;
}
printDoc.PrintPage += new PrintPageEventHandler(PrintPage);
printDoc.Print();
}

public void Run()
{
//LocalReport report = new LocalReport();
//report.ReportPath = "Report.rdlc";
//report.DataSources.Add(new ReportDataSource("Sales", LoadSalesData()));
//report.DataSources.Add(new ReportDataSource("Sales", LoadSalesData()));
//ReportViewer oViewer = new ReportViewer();

//this.customersTableAdapter1.Fill(this.dataSet1.Customers);
this.ReportViewer1.ProcessingMode = ProcessingMode.Local;
this.ReportViewer1.LocalReport.ReportPath =
@"Report.rdlc";
ReportViewer1.LocalReport.DataSources.Add(
new ReportDataSource("Customers", LoadSalesData()));

string deviceInfo =
"" +
" EMF" +
" 8.5in" +
" 11in" +
" 0.25in" +
" 0.25in" +
" 0.25in" +
" 0.25in" +
"";
Warning[] warnings;
//m_streams = new List();
//report.Render("Image", deviceInfo, CreateStream, out warnings);

//Warning[] warnings;
string[] streamids;
string mimeType;
string encoding;
string extension;

bytes = ReportViewer1.LocalReport.Render(
"PDF", null, out mimeType, out encoding,
out extension,
out streamids, out warnings);

FileStream fs = new FileStream(@"D:\output.PDF",
FileMode.Create);
fs.Write(bytes, 0, bytes.Length);
fs.Close();


foreach (Stream stream in m_streams)
stream.Position = 0;

//Export(report);

m_currentPageIndex = 0;
Print();
}

public void Dispose()
{
if (m_streams != null)
{
foreach (Stream stream in m_streams)
stream.Close();
m_streams = null;
}
}
}

Resource : http://www.gotreportviewer.com/

Download

No comments:

Post a Comment