C#.NET PrintDocument 自定義報表數據打印

 這是一個自定義的報表打印,是對PrintPreviewDialog的擴展和封裝。PrintPreviewDialog是一個windows的打印預覽控件,該類返回的就是一個PrintPreviewDialog對象了,直接顯示該控件就可以了。

    該報表主要包含兩個方面的內容:一個是數據統計分析的餅狀圖(數據集是用DataTable保存的,,圖形統計值分析DataTable的前兩列);另一個就是DataTable的數據集了,可以自己定義繪製的格式(當然得自己寫代碼了)。

    效果預覽圖如下所示:

    該類的源代碼如下:

代碼
using System;
 using System.Collections.Generic;
 using System.Linq;
using System.Text;
//
using System.Drawing;
using System.Drawing.Printing;
using System.Data;
using System.Windows.Forms;
//using Microsoft.Office.Interop.Excel;
using System.Reflection;

namespace WinFormTest
{
///<summary>
/// 數據報表統計
/// ryan-2010/9/19
///</summary>
publicclass DataReprot
    {
#region//property
//image size
int _Width =600;
int _Height =420;
//pager
privateint _TopMargin =50;
privateint _LeftMargin =60;
privateint _RightMargin =50;
privateint _BottomMargin =60;
private Font _TitleFont =new Font("宋體", 18, FontStyle.Bold);
private Font _ColumnsHeaderFont =new Font("宋體", 10, FontStyle.Bold);
private Font _ContentFont =new Font("宋體", 9, FontStyle.Regular);
        SolidBrush brush=new SolidBrush(Color.Black);
        Pen pen =new Pen(new SolidBrush(Color.Black));
int _RowHeight =30;
int _CurrentPageIndex;
int _PageCount;
int _RowsCount;
int _CurrentRowsIndex;
int _MaxRowsCount =35;
        Point _CurrentPoint;
        DataTable _DT;
string _Title;
string _ImgTitle;
string[] _ColumnsHeader;
string[] _BottomStr;
#endregion

#region//DataReprot()
public DataReprot(string title, string imgTitle, DataTable dataTable, string[] columnsHeader, string[] bottomStr)
        {
            _Title = title;
            _DT = Sort(dataTable);
            _ImgTitle = imgTitle;
            _ColumnsHeader = columnsHeader;
            _RowsCount = dataTable.Rows.Count;
            _BottomStr = bottomStr;
            _CurrentPageIndex =0;
            _CurrentRowsIndex =0;
//pagecount
if ((dataTable.Rows.Count +20) % _MaxRowsCount ==0)
                _PageCount = (dataTable.Rows.Count +20) / _MaxRowsCount;
else
                _PageCount = ((dataTable.Rows.Count +20) / _MaxRowsCount) +1;
        }
#endregion

#region//保存爲excl
publicvoid SaveAsExcl(string fileFullPath)
        {
            Microsoft.Office.Interop.Excel.ApplicationClass excel =new Microsoft.Office.Interop.Excel.ApplicationClass();
            Microsoft.Office.Interop.Excel.Workbook wBook = excel.Workbooks.Add(true);
            Microsoft.Office.Interop.Excel.Worksheet wSheet = (Microsoft.Office.Interop.Excel.Worksheet)wBook.ActiveSheet;
            excel.DisplayAlerts =false;
            excel.AlertBeforeOverwriting =false;
//
            excel.ActiveWorkbook.sav
            excel.Cells[1, 1] ="網上搜索C#實現excel操作的示例太多了,但不知道有多少是經過驗證確實 ";

//
            excel.ActiveWorkbook.SaveCopyAs(fileFullPath);
            excel.Quit();
        }
#endregion

#region//對dt排序
public DataTable Sort(DataTable dataTable)
        {
string orderName = dataTable.Columns[1].ColumnName;
            DataView dv = dataTable.DefaultView;
            dv.Sort = orderName +" DESC";
            dataTable = dv.ToTable();
return dataTable;
        }
#endregion

#region//打印報表
public PrintPreviewDialog PrintReport()
        {
            
//
            PrintDocument printDoc=new PrintDocument();
            printDoc.PrintPage+=PrintPage;
            printDoc.BeginPrint += BeginPrint;
            PrintPreviewDialog pPreviewDialog =new PrintPreviewDialog();
            pPreviewDialog.Document = printDoc;
            pPreviewDialog.ShowIcon =false;
            pPreviewDialog.PrintPreviewControl.Zoom =1.0;
            pPreviewDialog.TopLevel =false;
            SetPrintPreviewDialog(pPreviewDialog);
return pPreviewDialog;
        }
#endregion

#region//Bitmap GetPieImage()

#region//繪製餅狀圖
///<summary>
/// 繪製餅狀圖
///</summary>
///<returns></returns>
public Bitmap GetPieImage(string title, DataTable dataTable)
        {
            Bitmap image = GenerateImage(title);
            dataTable = DataFormat(dataTable);
//主區域圖形
            Rectangle RMain =new Rectangle(35, 70, 380, 300);
//圖例信息
            Rectangle RDes =new Rectangle(445, 90, 10, 10);
            Font f =new Font("宋體", 10, FontStyle.Regular);

            Graphics g = Graphics.FromImage(image);
            g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
try
            {
//分析數據,繪製餅圖和圖例說明
double[] ItemRate = GetItemRate(dataTable);
int[] ItemAngle = GetItemAngle(ItemRate);
int Angle1 =0;
int Angle2 =0;
int len = ItemRate.Length;
                Color c =new Color();
//3D
                g.DrawPie(new Pen(Color.Black), RMain, 0F, 360F);
                g.DrawPie(new Pen(Color.Black), new Rectangle(RMain.X, RMain.Y +10, RMain.Width, RMain.Height), 0F, 360F);
                g.FillPie(new SolidBrush(Color.Black), new Rectangle(RMain.X, RMain.Y +10, RMain.Width, RMain.Height), 0F, 360F);
//繪製
for (int i =0; i < len; i++)
                {
                    Angle2 = ItemAngle[i];
//if (c != GetRandomColor(i))
                    c = GetRandomColor(i);

                    SolidBrush brush =new SolidBrush(c);
string DesStr = dataTable.Rows[i][0].ToString() +"("+ (ItemRate[i] *100).ToString(".00") +"%"+")";
//
                    DrawPie(image, RMain, c, Angle1, Angle2);
                    Angle1 += Angle2;
                    DrawDes(image, RDes, c, DesStr, f, i);
                }

return image;
            }
finally
            {
                g.Dispose();
            }
        }
#endregion

#region//繪製圖像的基本數據計算方法
///<summary>
/// 數據格式化
///</summary>
private DataTable DataFormat(DataTable dataTable)
        {
if (dataTable ==null)
return dataTable;
//把大於等於10的行合併,
if (dataTable.Rows.Count <=10)
return dataTable;
//new Table
            DataTable dataTableNew = dataTable.Copy();
            dataTableNew.Rows.Clear();
for (int i =0; i <8; i++)
            {
                DataRow dataRow = dataTableNew.NewRow();
                dataRow[0] = dataTable.Rows[i][0];
                dataRow[1] = dataTable.Rows[i][1];
                dataTableNew.Rows.Add(dataRow);
            }
            DataRow dr = dataTableNew.NewRow();
            dr[0] ="其它";
double allValue =0;
for (int i =9; i < dataTable.Rows.Count; i++)
            {
                allValue += Convert.ToDouble(dataTable.Rows[i][1]);
            }
            dr[1] = allValue;
            dataTableNew.Rows.Add(dr);
return dataTableNew;
        }
///<summary>
/// 計算數值總和
///</summary>
privatestaticdouble Sum(DataTable dataTable)
        {
double t =0;
foreach (DataRow dr in dataTable.Rows)
            {
                t += Convert.ToDouble(dr[1]);
            }
return t;
        }
///<summary>
/// 計算各項比例
///</summary>
privatestaticdouble[] GetItemRate(DataTable dataTable)
        {
double sum = Sum(dataTable);
double[] ItemRate =newdouble[dataTable.Rows.Count];
for (int i =0; i < dataTable.Rows.Count; i++)
            {
                ItemRate[i] = Convert.ToDouble(dataTable.Rows[i][1]) / sum;
            }
return ItemRate;
        }
///<summary>
/// 根據比例,計算各項角度值
///</summary>
privatestaticint[] GetItemAngle(double[] ItemRate)
        {
int[] ItemAngel =newint[ItemRate.Length];
for (int i =0; i < ItemRate.Length; i++)
            {
double t =360* ItemRate[i];
                ItemAngel[i] = Convert.ToInt32(t);
            }
return ItemAngel;
        }
#endregion

#region// 隨即扇形區域顏色,繪製區域框,
///<summary>
/// 生成隨機顏色
///</summary>
///<returns></returns>
privatestatic Color GetRandomColor(int seed)
        {
            Random random =new Random(seed);
int r =0;
int g =0;
int b =0;
            r = random.Next(0, 230);
            g = random.Next(0, 230);
            b = random.Next(0, 235);
            Color randomcolor = Color.FromArgb(r, g, b);
return randomcolor;
        }
///<summary>
/// 繪製區域框、陰影
///</summary>
privatestatic Bitmap DrawRectangle(Bitmap image, Rectangle rect)
        {
            Bitmap Image = image;
            Graphics g = Graphics.FromImage(Image);
            g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
try
            {
                Rectangle rn =new Rectangle(rect.X +3, rect.Y +3, rect.Width, rect.Height);
                SolidBrush brush1 =new SolidBrush(Color.FromArgb(233, 234, 249));
                SolidBrush brush2 =new SolidBrush(Color.FromArgb(221, 213, 215));
//
                g.FillRectangle(brush2, rn);
                g.FillRectangle(brush1, rect);
return Image;
            }
finally
            {
                g.Dispose();
            }
        }
#endregion

#region//繪製圖例框、圖列信息,繪製扇形
///<summary>
/// 繪製圖例信息
///</summary>
privatestatic Bitmap DrawDes(Bitmap image, Rectangle rect, Color c, string DesStr, Font f, int i)
        {
            Bitmap Image = image;
            Graphics g = Graphics.FromImage(Image);
            g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.SingleBitPerPixelGridFit;
            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.Default;
try
            {
                SolidBrush brush =new SolidBrush(c);
                Rectangle R =new Rectangle(rect.X, rect.Y +25* i, rect.Width, rect.Height);
                Point p =new Point(rect.X +12, rect.Y +25* i);
//❀顏色矩形框
                g.FillRectangle(brush, R);
//文字說明
                g.DrawString(DesStr, f, new SolidBrush(Color.Black), p);
return Image;
            }
finally
            {
                g.Dispose();
            }
        }
//繪製扇形
privatestatic Bitmap DrawPie(Bitmap image, Rectangle rect, Color c, int Angle1, int Angle2)
        {
            Bitmap Image = image;
            Graphics g = Graphics.FromImage(Image);
            g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
try
            {
                SolidBrush brush =new SolidBrush(c);
                Rectangle R =new Rectangle(rect.X, rect.Y, rect.Width, rect.Height);
                g.FillPie(brush, R, Angle1, Angle2);
return Image;
            }
finally
            {
                g.Dispose();
            }
        }
#endregion

#region//繪製基本圖形
///<summary>
/// 生成圖片,統一設置圖片大小、背景色,圖片佈局,及標題
///</summary>
///<returns>圖片</returns>
private  Bitmap GenerateImage(string Title)
        {
            Bitmap image =new Bitmap(_Width, _Height);
            Graphics g = Graphics.FromImage(image);
//標題
            Point PTitle =new Point(30, 20);
            Font f1 =new Font("黑體", 12, FontStyle.Bold);
//線
int len = (int)g.MeasureString(Title, f1).Width;
            Point PLine1 =new Point(20, 40);
            Point PLine2 =new Point(20+len+20, 40);
            Pen pen =new Pen(new SolidBrush(Color.FromArgb(8, 34, 231)), 1.5f);
//主區域,主區域圖形
            Rectangle RMain1 =new Rectangle(20, 55, 410, 345);
            Rectangle RMain2 =new Rectangle(25, 60, 400, 335);
//圖例區域
            Rectangle RDes1 =new Rectangle(440, 55, 150, 345);
//圖例說明
string Des ="圖例說明:";
            Font f2 =new Font("黑體", 10, FontStyle.Bold);
            Point PDes =new Point(442, 65);
//圖例信息,後面的x座標上累加20
            Rectangle RDes2 =new Rectangle(445, 90, 10, 10);
            g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
try
            {
//設置背景色、繪製邊框
                g.Clear(Color.White);
                g.DrawRectangle(pen, 1, 1, _Width-2 , _Height-2);
//繪製標題、線
                g.DrawString(Title, f1, new SolidBrush(Color.Black), PTitle);
                g.DrawLine(pen, PLine1, PLine2);

//主區域 
                image = DrawRectangle(image, RMain1);
//圖例區域
                image = DrawRectangle(image, RDes1);
//“圖例說明”
                g.DrawString(Des, f2, new SolidBrush(Color.Black), PDes);
//return 
return image;
            }
finally
            {
                g.Dispose();
            }

        }
#endregion

#endregion


#region//繪製圖形、報表        
        
#region//print Event
privatevoid PrintPage(object sender, PrintPageEventArgs e)
        {
            _CurrentPageIndex++;
            _CurrentPoint =new Point(_LeftMargin, _RightMargin);
int serialNumWidth =60;
int colWidth = (e.PageBounds.Width - _LeftMargin - _RightMargin - serialNumWidth) / _DT.Columns.Count;
//第一頁繪製標題,圖形
if (_CurrentPageIndex ==1)
            {
                DrawTitle(e);
                DrawImage(e);
                DrawTableHeader(e, serialNumWidth, colWidth);
                DrawBottom(e);
                DrawTableAndSerialNumAndData(e, serialNumWidth, colWidth);
if (_PageCount >1)
                    e.HasMorePages =true;

            }
elseif (_CurrentPageIndex == _PageCount)
            {
                DrawTableHeader(e, serialNumWidth, colWidth);
                DrawTableAndSerialNumAndData(e, serialNumWidth, colWidth);
                DrawBottom(e);
                e.HasMorePages =false;
                e.Cancel =true;
            }
else
            {
                DrawTableHeader(e, serialNumWidth, colWidth);
                DrawTableAndSerialNumAndData(e, serialNumWidth, colWidth);
                DrawBottom(e);
                e.HasMorePages =true;
                
            }
        }
privatevoid BeginPrint(object sender,PrintEventArgs e)
        {
            _CurrentPageIndex =0;
            _CurrentRowsIndex =0;
            e.Cancel =false;
        }
#endregion

#region//繪製標題
privatevoid DrawTitle(PrintPageEventArgs e)
        {
//標題 居中
            _CurrentPoint.X = (e.PageBounds.Width) /2- (int)(e.Graphics.MeasureString(_Title, _TitleFont).Width) /2;
            e.Graphics.DrawString(_Title, _TitleFont, new SolidBrush(Color.Black), _CurrentPoint);
            _CurrentPoint.Y += (int)(e.Graphics.MeasureString(_Title, _TitleFont).Height);
//標題下的線
int len = (int)(e.Graphics.MeasureString(_Title, _TitleFont).Width) +100;
int start = (e.PageBounds.Width) /2- len /2;
            e.Graphics.DrawLine(new Pen(new SolidBrush(Color.Black)), new Point(start, _CurrentPoint.Y), new Point(start + len, _CurrentPoint.Y));
            _CurrentPoint.Y +=3;
            e.Graphics.DrawLine(new Pen(new SolidBrush(Color.Black)), new Point(start, _CurrentPoint.Y), new Point(start + len, _CurrentPoint.Y));
            _CurrentPoint.Y +=50;
            _CurrentPoint.X = _LeftMargin;
        }

#endregion

#region//繪製統計圖
privatevoid DrawImage(PrintPageEventArgs e)
        {
//標題 居中
            _CurrentPoint.X = (e.PageBounds.Width) /2- _Width /2;
            e.Graphics.DrawImage(GetPieImage(_ImgTitle, _DT), _CurrentPoint);
            _CurrentPoint.X = _LeftMargin;
            _CurrentPoint.Y += _Height+50;
        }
        
#endregion

#region//繪製頁尾
privatevoid DrawBottom(PrintPageEventArgs e)
        {
int pageNumWidth =70;
int count = _BottomStr.Length;
int width = (e.PageBounds.Width - _LeftMargin - _RightMargin - pageNumWidth) / (count +1);
int y = e.PageBounds.Height - _BottomMargin +5;
int x=_LeftMargin;
//line
            e.Graphics.DrawLine(new Pen(new SolidBrush(Color.Black)), x, y, e.PageBounds.Width - _RightMargin, y);
            y +=5;
for (int i =0; i < count; i++)
            {
if(i>0)
                x += width;
                e.Graphics.DrawString(_BottomStr[i], _ContentFont, new SolidBrush(Color.Black), x, y);
            }
            x = e.PageBounds.Width - _RightMargin - pageNumWidth;
            e.Graphics.DrawString(string.Format("第{0}頁/共{1}頁",_CurrentPageIndex,_PageCount), _ContentFont, new SolidBrush(Color.Black), x, y);
        }

#endregion

#region//繪製表格和序號、數據

privatevoid DrawTableAndSerialNumAndData(PrintPageEventArgs e, int serialNumWidth, int colWidth)
        {
int useAbleHeight = e.PageBounds.Height - _CurrentPoint.Y - _BottomMargin;
int useAbleRowsCount = useAbleHeight / _RowHeight;
int rowsCount=0;
if (_RowsCount-_CurrentRowsIndex > useAbleRowsCount)
                rowsCount = useAbleRowsCount;
else
                rowsCount = _RowsCount - _CurrentRowsIndex;
            Point pp =new Point(_CurrentPoint.X, _CurrentPoint.Y);
for(int i=0;i<=rowsCount;i++)
            {
                e.Graphics.DrawLine(pen, _LeftMargin, _CurrentPoint.Y + i * _RowHeight,e.PageBounds.Width-_RightMargin, _CurrentPoint.Y + i * _RowHeight);
//繪製數據
if (i >= rowsCount)
break;
                DrawCellString((i +1+ _CurrentRowsIndex).ToString(), pp, serialNumWidth,_ContentFont, e);
                pp.X += serialNumWidth;
for (int j =0; j < _DT.Columns.Count; j++)
                {
                    DrawCellString(_DT.Rows[i + _CurrentRowsIndex][j].ToString(), pp, colWidth, _ContentFont, e);
                    pp.X += colWidth;
                }
                pp.Y += _RowHeight;
                pp.X = _CurrentPoint.X;

            }
//繪製豎線
            Point p =new Point(_CurrentPoint.X,_CurrentPoint.Y);
            e.Graphics.DrawLine(pen, p, new Point(p.X, p.Y + _RowHeight * rowsCount));
            p.X += serialNumWidth;
            e.Graphics.DrawLine(pen, p, new Point(p.X, p.Y + _RowHeight * rowsCount));
for (int i =1; i < _DT.Columns.Count; i++)
            {
                p.X += colWidth;
                e.Graphics.DrawLine(pen, p, new Point(p.X, p.Y + _RowHeight * rowsCount));
            }
            p.X=e.PageBounds.Width-_RightMargin;
            e.Graphics.DrawLine(pen, p, new Point(p.X, p.Y + _RowHeight * rowsCount));
            _CurrentRowsIndex += rowsCount;
        }

#endregion

#region//填充數據到單元格
privatevoid DrawCellString(string str, Point p,int colWidth,Font f, PrintPageEventArgs e)
        {
int strWidth = (int)e.Graphics.MeasureString(str, f).Width;
int strHeight = (int)e.Graphics.MeasureString(str, f).Height;
            p.X += (colWidth - strWidth) /2;
            p.Y +=5;
            p.Y += (_RowHeight - strHeight) /2;
            e.Graphics.DrawString(str, f, brush, p);
        }
#endregion

#region//繪製標題
privatevoid DrawTableHeader(PrintPageEventArgs e, int serialNumWidth,int colWidth)
        {
//畫框
            Point pp =new Point(_CurrentPoint.X, _CurrentPoint.Y);
            e.Graphics.DrawLine(pen, pp, new Point(e.PageBounds.Width - _RightMargin, pp.Y));
            pp.Y+=_RowHeight;
            e.Graphics.DrawLine(pen,pp,new Point(e.PageBounds.Width-_RightMargin,pp.Y));
            pp =new Point(_CurrentPoint.X, _CurrentPoint.Y);
            e.Graphics.DrawLine(pen, pp, new Point(pp.X, pp.Y + _RowHeight));
            pp.X += serialNumWidth;
            e.Graphics.DrawLine(pen,pp, new Point(pp.X, pp.Y + _RowHeight));
for (int i =1; i < _DT.Columns.Count; i++)
            {
                pp.X += colWidth;
                e.Graphics.DrawLine(pen, pp, new Point(pp.X, pp.Y + _RowHeight));
            }
            pp.X = e.PageBounds.Width - _RightMargin;
            e.Graphics.DrawLine(pen, pp, new Point(pp.X, pp.Y + _RowHeight));
//
            Point p =new Point(_CurrentPoint.X +5, _CurrentPoint.Y);
            DrawCellString("序號", p, serialNumWidth,_ColumnsHeaderFont, e);
            p.X += serialNumWidth;
for (int i =0; i < _DT.Columns.Count; i++)
            {
if(i!=0)
                p.X += colWidth;
                DrawCellString(_ColumnsHeader[i], p, colWidth, _ColumnsHeaderFont, e);
            }
            _CurrentPoint.X = _LeftMargin;
            _CurrentPoint.Y += _RowHeight;
        }
#endregion

#region// 自定義設置打印預覽對話框
publicvoid SetPrintPreviewDialog(PrintPreviewDialog pPreviewDialog)
        {
            System.Reflection.PropertyInfo[] pis = pPreviewDialog.GetType().GetProperties(); 
for (int i =0; i < pis.Length; i++)
            {
switch(pis[i].Name)
                {
case"Dock":
                        pis[i].SetValue(pPreviewDialog, DockStyle.Fill, null);
break;
case"FormBorderStyle":
                        pis[i].SetValue(pPreviewDialog, FormBorderStyle.None, null);
break;
case"WindowState":
                        pis[i].SetValue(pPreviewDialog, FormWindowState.Normal, null);
break;
default:break;
                }
            }
#region//屏蔽默認的打印按鈕,添加自定義的打印和保存按鈕
foreach (Control c in pPreviewDialog.Controls)
            {
if (c is ToolStrip)
                {
                    ToolStrip ts = (ToolStrip)c;
                    ts.Items[0].Visible =false;
//print
                    ToolStripButton toolStripBtn_Print =new ToolStripButton();
                    toolStripBtn_Print.Text ="打印";
                    toolStripBtn_Print.ToolTipText ="打印當前報表數據";
                    toolStripBtn_Print.Image = Properties.Resources.printer;
                    toolStripBtn_Print.Click +=
delegate(object sender, EventArgs e)
                        {
                            PrintDialog pd =new PrintDialog();
                            pd.Document = pPreviewDialog.Document;
                            pd.UseEXDialog =true;
if (pd.ShowDialog() == DialogResult.OK)
                                pPreviewDialog.Document.Print();
                        };
                    ToolStripButton toolStripBtn_SaveAsExcel =new ToolStripButton();
                    toolStripBtn_SaveAsExcel.Text ="保存Excel";
                    toolStripBtn_SaveAsExcel.ToolTipText ="導出報表到Excel";
                    toolStripBtn_SaveAsExcel.Image = Properties.Resources.save;
                    toolStripBtn_SaveAsExcel.Click +=
delegate(object sender, EventArgs e)
                        {
                            SaveFileDialog f =new SaveFileDialog();
                            
if (f.ShowDialog() == DialogResult.OK)
                            {
                                SaveAsExcl(f.FileName);
                            }
                        };
                    ToolStripSeparator tss =new ToolStripSeparator();
                    ts.Items.Insert(0, toolStripBtn_Print);
                    ts.Items.Insert(1, toolStripBtn_SaveAsExcel);
                    ts.Items.Insert(2, tss);
                }
            }
#endregion
        }
#endregion

#endregion
    }
    
}
代碼

privatevoid button2_Click(object sender, EventArgs e)
        {
        //測試數據
            DataTable dataTable =new DataTable();
            dataTable.Columns.Add("id", typeof(string));
            dataTable.Columns.Add("value1", typeof(double));
            dataTable.Columns.Add("value2", typeof(double));
            for (int i =0; i <45; i++)
            {
                dataTable.Rows.Add(newobject[] {"北京市-"+i.ToString(),100*i,234.345*i});
            }
            string[] bottomStr={"操作人員:Ryan","打印日期:"+DateTime.Now.ToShortDateString(),"審覈人員:","財務人員:"};
            string[] header = {"城市名稱","預定數量","平均價格" };
            DataReprot dr =new DataReprot("2010年12月12日-2011年12月12日城市預訂分佈統計報表", "2010年12月12日-2011年12月12日城市預訂分佈統計圖", dataTable, header, bottomStr);
            PrintPreviewDialog p = dr.PrintReport();
            this.groupBox2.Controls.Add(p);
            p.Show();
            groupBox2.Width +=1;
            this.Refresh();
        }

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章