//打印按鈕為:button6
在習(xí)水等地區(qū),都構(gòu)建了全面的區(qū)域性戰(zhàn)略布局,加強(qiáng)發(fā)展的系統(tǒng)性、市場前瞻性、產(chǎn)品創(chuàng)新能力,以專注、極致的服務(wù)理念,為客戶提供網(wǎng)站設(shè)計(jì)、做網(wǎng)站 網(wǎng)站設(shè)計(jì)制作定制網(wǎng)站開發(fā),公司網(wǎng)站建設(shè),企業(yè)網(wǎng)站建設(shè),品牌網(wǎng)站設(shè)計(jì),成都營銷網(wǎng)站建設(shè),成都外貿(mào)網(wǎng)站建設(shè)公司,習(xí)水網(wǎng)站建設(shè)費(fèi)用合理。
//命名空間:(只能比我的多,不能比我的少,有時(shí)候 會(huì)忘記這個(gè)最重要的地方,這是最…的…)
//三個(gè)控件:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Drawing.Printing;
//第一種:
#region***打印代碼 簡潔版***
private void button6_Click(object sender, EventArgs e)
{
if (dataGridView1.Rows.Count != 0)
{
printDialog1.Document = this.printDocument1;
printDialog1.ShowDialog(); //設(shè)置打印文檔
printPreviewDialog1.Document = this.printDocument1;
printPreviewDialog1.ClientSize = new System.Drawing.Size(800, 500);//設(shè)置打印預(yù)覽時(shí)的頁面大小
printPreviewDialog1.PrintPreviewControl.Zoom = 1; //顯示百分比為 100%
printPreviewDialog1.ShowDialog();
}
else
{
MessageBox.Show("打印內(nèi)容為空");
}
}
private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
int r = 50; //設(shè)置橫坐標(biāo)的位置
int c = 20; //設(shè)置縱坐標(biāo)的間隔
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
for (int j = 0; j < dataGridView1.Columns.Count; j++)
{
e.Graphics.DrawString(dataGridView1.Rows[i].Cells[j].Value.ToString(), new Font("宋體", 8, FontStyle.Regular), Brushes.Black, r, c);
r = r + 100;
}
r = 50;
c += 20;
}
}
#endregion
//第二種, 在以上命名空間 的基礎(chǔ)上:
#region***打印代碼 強(qiáng)大版***
private void button6_Click(object sender, EventArgs e)
{
printDialog1.Document = this.printDocument1;
printDialog1.ShowDialog();
PrintDataGridView pdgv = new PrintDataGridView();
pdgv.myPrint(dataGridView1, "King公司考勤時(shí)間表", "詳情");
}
#endregion
//還要建一個(gè)名為PrintDataGridView類
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing.Printing;
using System.Windows.Forms;
using System.Drawing;
namespace King公司考勤時(shí)間表//命名空間 別忘了改成自己的
{
public class PrintDataGridView
{
public PrintDataGridView()
{
}
static DataGridView dgv;
//標(biāo)題名稱
static string titleName = "";
//第二標(biāo)題名稱
static string titleName2 = "";
//當(dāng)前行
static int rowIndex = 0;
//當(dāng)前頁
static int page = 1;
//每頁顯示多少行
static int rowsPerPage = 0;
///
///打印DataGridView
///
/// 要打印的DataGridView
/// 標(biāo)題
/// /// 第二標(biāo)題,可以為null
public void myPrint(DataGridView dataGridView, string title, string title2)
{
try
{
if (dataGridView == null)
{
return;
}
titleName = title;
titleName2 = title2;
dgv = dataGridView;
PrintPreviewDialog ppvw = new PrintPreviewDialog();
//顯示比例為100%
ppvw.PrintPreviewControl.Zoom = 1.0;
PrintDocument printDoc = new PrintDocument();
//A4紙
printDoc.DefaultPageSettings.PaperSize = new PaperSize("A4", 850, 1000);
//設(shè)置邊距
printDoc.DefaultPageSettings.Margins = new Margins(60, 60, 60, 60);
//設(shè)置要打印的文檔
ppvw.Document = printDoc;
//最大化
((Form)ppvw).WindowState = FormWindowState.Maximized;
//當(dāng)前行
rowIndex = 0;
//當(dāng)前頁
page = 1;
//打印事件
printDoc.PrintPage += new PrintPageEventHandler(printDoc_PrintPage);
printDoc.EndPrint += new PrintEventHandler(printDoc_EndPrint);
//打開預(yù)覽
ppvw.ShowDialog();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "錯(cuò)誤", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
static void printDoc_EndPrint(object sender, PrintEventArgs e)
{
//當(dāng)前行
rowIndex = 0;
//當(dāng)前頁
page = 1;
//每頁顯示多少行
rowsPerPage = 0;
}
private static void printDoc_PrintPage(object sender, PrintPageEventArgs e)
{
//標(biāo)題字體
Font titleFont = new Font("宋體", 16, FontStyle.Bold);
//標(biāo)題尺寸
SizeF titleSize = e.Graphics.MeasureString(titleName, titleFont, e.MarginBounds.Width);
//x坐標(biāo)
int x = e.MarginBounds.Left;
//y坐標(biāo)
int y = Convert.ToInt32(e.MarginBounds.Top - titleSize.Height);
//邊距以內(nèi)紙張寬度
int pagerWidth = e.MarginBounds.Width;
//畫標(biāo)題
e.Graphics.DrawString(titleName, titleFont, Brushes.Black, x + (pagerWidth - titleSize.Width) / 2, y);
y += (int)titleSize.Height;
if (titleName2 != null && titleName2 != "")
{
//畫第二標(biāo)題
e.Graphics.DrawString(titleName2, dgv.Font, Brushes.Black, x, y);
//第二標(biāo)題尺寸
SizeF titleSize2 = e.Graphics.MeasureString(titleName2, dgv.Font, e.MarginBounds.Width);
y += (int)titleSize2.Height; ;
}
//表頭高度
int headerHeight = 0;
//縱軸上 內(nèi)容與線的距離
int padding = 6;
//所有顯示列的寬度
int columnsWidth = 0;
//計(jì)算所有顯示列的寬度
foreach (DataGridViewColumn column in dgv.Columns)
{
//隱藏列返回
if (!column.Visible) continue;
//所有顯示列的寬度
columnsWidth += column.Width;
}
//計(jì)算表頭高度
foreach (DataGridViewColumn column in dgv.Columns)
{
//列寬
int columnWidth = (int)(Math.Floor((double)column.Width / (double)columnsWidth * (double)pagerWidth));
//表頭高度
int temp = (int)e.Graphics.MeasureString(column.HeaderText, column.InheritedStyle.Font, columnWidth).Height + 2 * padding;
if (temp > headerHeight) headerHeight = temp;
}
//畫表頭
foreach (DataGridViewColumn column in dgv.Columns)
{
//隱藏列返回
if (!column.Visible) continue;
//列寬
int columnWidth = (int)(Math.Floor((double)column.Width / (double)columnsWidth * (double)pagerWidth));
//內(nèi)容居中要加的寬度
float cenderWidth = (columnWidth - e.Graphics.MeasureString(column.HeaderText, column.InheritedStyle.Font, columnWidth).Width) / 2;
if (cenderWidth < 0) cenderWidth = 0;
//內(nèi)容居中要加的高度
float cenderHeight = (headerHeight + padding - e.Graphics.MeasureString(column.HeaderText, column.InheritedStyle.Font, columnWidth).Height) / 2;
if (cenderHeight < 0) cenderHeight = 0;
//畫背景
e.Graphics.FillRectangle(new SolidBrush(Color.LightGray), new Rectangle(x, y, columnWidth, headerHeight));
//畫邊框
e.Graphics.DrawRectangle(Pens.Black, new Rectangle(x, y, columnWidth, headerHeight));
////畫上邊線
//e.Graphics.DrawLine(Pens.Black, x, y, x + columnWidth, y);
////畫下邊線
//e.Graphics.DrawLine(Pens.Black, x, y + headerHeight, x + columnWidth, y + headerHeight);
////畫右邊線
//e.Graphics.DrawLine(Pens.Black, x + columnWidth, y, x + columnWidth, y + headerHeight);
//if (x == e.MarginBounds.Left)
//{
// //畫左邊線
// e.Graphics.DrawLine(Pens.Black, x, y, x, y + headerHeight);
//}
//畫內(nèi)容
e.Graphics.DrawString(column.HeaderText, column.InheritedStyle.Font, new SolidBrush(column.InheritedStyle.ForeColor), new RectangleF(x + cenderWidth, y + cenderHeight, columnWidth, headerHeight));
x += columnWidth;
}
x = e.MarginBounds.Left;
y += headerHeight;
//遍歷行
while (rowIndex < dgv.Rows.Count)
{
//當(dāng)前行
DataGridViewRow row = dgv.Rows[rowIndex];
if (row.Visible)
{
//行高
int rowHeight = 0;
//計(jì)算行高
foreach (DataGridViewCell cell in row.Cells)
{
//當(dāng)前列
DataGridViewColumn column = dgv.Columns[cell.ColumnIndex];
//隱藏列返回
if (!column.Visible || cell.Value == null) continue;
//列寬