【转】Winfrom datagridview 打印
using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Printing;
using System.Data;
using System.Windows.Forms;
namespace caiwu
{
public class cutePrinter2
{
private DataGridView dataGridView1;
private PrintDocument printDocument;
private PageSetupDialog pageSetupDialog;
private PrintPreviewDialog printPreviewDialog;
private string title = "";
int currentPageIndex = 0;
int rowCount = 0;
int pageCount = 0;
int titleSize = 20;
bool isCustomHeader = false;
Brush alertBrush = new SolidBrush(Color.Red);
string[] header = null;//如果自定义就填入字符串,如果需要斜线分隔,就用\表示,例如:个数#名字 其中#为splitChar
string[] uplineHeader = null;//上行文字数组
int[] upLineHeaderIndex = null;//上行的文字index,如果没有上行就设为-1;
public bool isEveryPagePrintTitle = true;//是否每一页都要打印标题。
public int headerHeight = 30;//标题高度。
public int topMargin = 30; //顶边距
public int cellTopMargin = 6;//单元格顶边距
public int cellLeftMargin = 4;//单元格左边距
public char splitChar = '#';//当header要用斜线表示的时候
public string falseStr = "×";//如果传进来的DataGridView中有 false,把其转换得字符。
public string trueStr = "√";//如果传进来的DataGridView中有 true,把其转换得字符。
public int pageRowCount = 30;//每页行数
public int rowGap = 28;//行高
public int colGap = 5;//每列间隔
public int leftMargin = 45;//左边距
public Font titleFont = new Font("黑体", 24, FontStyle.Bold);//标题字体
public Font font = new Font("宋体", 10);//正文字体
public Font headerFont = new Font("黑体", 11, FontStyle.Bold);//列名标题
public Font footerFont = new Font("Arial", 8);//页脚显示页数的字体
public Font upLineFont = new Font("Arial", 9, FontStyle.Bold);//当header分两行显示的时候,上行显示的字体。
public Font underLineFont = new Font("Arial", 8);//当header分两行显示的时候,下行显示的字体。
public Brush brush = new SolidBrush(Color.Black);//画刷
public bool isAutoPageRowCount = true;//是否自动计算行数。
public int buttomMargin = 80;//底边距
public bool needPrintPageIndex = true;//是否打印页脚页数
public bool setTongji = false; //设置是否显示统计
string sTitle = ""; //显示标题
string sTongJi01 = ""; //统计01
string sTongJi02 = ""; //统计02
string sTongJi03 = ""; //统计03
bool isTongji = false; //是否显示统计
string time01; //具体时间标题
string time02;
Font tongJiFont = new Font("宋体", 14); //统计
Font dateFont = new Font("宋体", 12, FontStyle.Bold); //日期标题
/// <summary>
/// 日统计报表打印
/// </summary>
/// <param name="dGView">DataGridView</param>
/// <param name="title">标题</param>
/// <param name="times01">起始时间</param>
/// <param name="times02">中止时间</param>
/// <param name="tj01">统计01</param>
/// <param name="tj02">统计02</param>
/// <param name="tj03">统计03</param>
/// <param name="tj">确认是否打印统计</param>
/// <param name="iPrintType">打印样式选择,默认2</param>
public cutePrinter2(DataGridView dGView, string title, string times01, string times02, string tj01, string tj02, string tj03, bool tj)
{
this.title = title;
this.sTongJi01 = tj01;
this.sTongJi02 = tj02;
this.sTongJi03 = tj03;
this.time01 = times01;
this.time02 = times02;
this.setTongji = tj;
this.dataGridView1 = dGView;
MessageBox.Show(dGView.Rows.Count.ToString());
printDocument = new PrintDocument();
printDocument.PrintPage += new PrintPageEventHandler(this.printDocument_PrintPage);
}
public bool setTowLineHeader(string[] upLineHeader, int[] upLineHeaderIndex)
{
this.uplineHeader = upLineHeader;
this.upLineHeaderIndex = upLineHeaderIndex;
this.isCustomHeader = true;
return true;
}
public bool setHeader(string[] header)
{
this.header = header;
return true;
}
private void printDocument_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
打印 --- 统计报表(年报表)
}
private void DrawLine(Point sp, Point ep, Graphics gp)
{
Pen pen = new Pen(Color.Black);
gp.DrawLine(pen, sp, ep);
}
public PrintDocument GetPrintDocument()
{
return printDocument;
}
public void Print()
{
rowCount = 0;
try
{
if (dataGridView1.DataSource.GetType().ToString() == "System.Data.DataSet")
{
rowCount = ((DataSet)dataGridView1.DataSource).Tables[0].Rows.Count;
}
else if (dataGridView1.DataSource.GetType().ToString() == "System.Data.DataView")
{
rowCount = ((DataView)dataGridView1.DataSource).Count;
}
pageSetupDialog = new PageSetupDialog();
pageSetupDialog.AllowOrientation = true;
pageSetupDialog.Document = printDocument;
pageSetupDialog.Document.DefaultPageSettings.Landscape = true; // 设置打印为横向
pageSetupDialog.ShowDialog();
printPreviewDialog = new PrintPreviewDialog();
printPreviewDialog.Document = printDocument;
printPreviewDialog.Height = 600;
printPreviewDialog.Width = 600;
printPreviewDialog.ClientSize = new System.Drawing.Size(1024, 768);
printPreviewDialog.PrintPreviewControl.Zoom = 1;
printPreviewDialog.ShowDialog();
}
catch
{
//throw new Exception("Printer error." + e.Message);
}
}
}
}
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Printing;
using System.Data;
using System.Windows.Forms;
namespace caiwu
{
public class cutePrinter2
{
private DataGridView dataGridView1;
private PrintDocument printDocument;
private PageSetupDialog pageSetupDialog;
private PrintPreviewDialog printPreviewDialog;
private string title = "";
int currentPageIndex = 0;
int rowCount = 0;
int pageCount = 0;
int titleSize = 20;
bool isCustomHeader = false;
Brush alertBrush = new SolidBrush(Color.Red);
string[] header = null;//如果自定义就填入字符串,如果需要斜线分隔,就用\表示,例如:个数#名字 其中#为splitChar
string[] uplineHeader = null;//上行文字数组
int[] upLineHeaderIndex = null;//上行的文字index,如果没有上行就设为-1;
public bool isEveryPagePrintTitle = true;//是否每一页都要打印标题。
public int headerHeight = 30;//标题高度。
public int topMargin = 30; //顶边距
public int cellTopMargin = 6;//单元格顶边距
public int cellLeftMargin = 4;//单元格左边距
public char splitChar = '#';//当header要用斜线表示的时候
public string falseStr = "×";//如果传进来的DataGridView中有 false,把其转换得字符。
public string trueStr = "√";//如果传进来的DataGridView中有 true,把其转换得字符。
public int pageRowCount = 30;//每页行数
public int rowGap = 28;//行高
public int colGap = 5;//每列间隔
public int leftMargin = 45;//左边距
public Font titleFont = new Font("黑体", 24, FontStyle.Bold);//标题字体
public Font font = new Font("宋体", 10);//正文字体
public Font headerFont = new Font("黑体", 11, FontStyle.Bold);//列名标题
public Font footerFont = new Font("Arial", 8);//页脚显示页数的字体
public Font upLineFont = new Font("Arial", 9, FontStyle.Bold);//当header分两行显示的时候,上行显示的字体。
public Font underLineFont = new Font("Arial", 8);//当header分两行显示的时候,下行显示的字体。
public Brush brush = new SolidBrush(Color.Black);//画刷
public bool isAutoPageRowCount = true;//是否自动计算行数。
public int buttomMargin = 80;//底边距
public bool needPrintPageIndex = true;//是否打印页脚页数
public bool setTongji = false; //设置是否显示统计
string sTitle = ""; //显示标题
string sTongJi01 = ""; //统计01
string sTongJi02 = ""; //统计02
string sTongJi03 = ""; //统计03
bool isTongji = false; //是否显示统计
string time01; //具体时间标题
string time02;
Font tongJiFont = new Font("宋体", 14); //统计
Font dateFont = new Font("宋体", 12, FontStyle.Bold); //日期标题
/// <summary>
/// 日统计报表打印
/// </summary>
/// <param name="dGView">DataGridView</param>
/// <param name="title">标题</param>
/// <param name="times01">起始时间</param>
/// <param name="times02">中止时间</param>
/// <param name="tj01">统计01</param>
/// <param name="tj02">统计02</param>
/// <param name="tj03">统计03</param>
/// <param name="tj">确认是否打印统计</param>
/// <param name="iPrintType">打印样式选择,默认2</param>
public cutePrinter2(DataGridView dGView, string title, string times01, string times02, string tj01, string tj02, string tj03, bool tj)
{
this.title = title;
this.sTongJi01 = tj01;
this.sTongJi02 = tj02;
this.sTongJi03 = tj03;
this.time01 = times01;
this.time02 = times02;
this.setTongji = tj;
this.dataGridView1 = dGView;
MessageBox.Show(dGView.Rows.Count.ToString());
printDocument = new PrintDocument();
printDocument.PrintPage += new PrintPageEventHandler(this.printDocument_PrintPage);
}
public bool setTowLineHeader(string[] upLineHeader, int[] upLineHeaderIndex)
{
this.uplineHeader = upLineHeader;
this.upLineHeaderIndex = upLineHeaderIndex;
this.isCustomHeader = true;
return true;
}
public bool setHeader(string[] header)
{
this.header = header;
return true;
}
private void printDocument_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
打印 --- 统计报表(年报表)
}
private void DrawLine(Point sp, Point ep, Graphics gp)
{
Pen pen = new Pen(Color.Black);
gp.DrawLine(pen, sp, ep);
}
public PrintDocument GetPrintDocument()
{
return printDocument;
}
public void Print()
{
rowCount = 0;
try
{
if (dataGridView1.DataSource.GetType().ToString() == "System.Data.DataSet")
{
rowCount = ((DataSet)dataGridView1.DataSource).Tables[0].Rows.Count;
}
else if (dataGridView1.DataSource.GetType().ToString() == "System.Data.DataView")
{
rowCount = ((DataView)dataGridView1.DataSource).Count;
}
pageSetupDialog = new PageSetupDialog();
pageSetupDialog.AllowOrientation = true;
pageSetupDialog.Document = printDocument;
pageSetupDialog.Document.DefaultPageSettings.Landscape = true; // 设置打印为横向
pageSetupDialog.ShowDialog();
printPreviewDialog = new PrintPreviewDialog();
printPreviewDialog.Document = printDocument;
printPreviewDialog.Height = 600;
printPreviewDialog.Width = 600;
printPreviewDialog.ClientSize = new System.Drawing.Size(1024, 768);
printPreviewDialog.PrintPreviewControl.Zoom = 1;
printPreviewDialog.ShowDialog();
}
catch
{
//throw new Exception("Printer error." + e.Message);
}
}
}
}