基本信息
源码名称:winfrom打印DataSet 数据 实例源码下载
源码大小:0.14M
文件格式:.7z
开发语言:C#
更新时间:2016-11-08
友情提示:(无需注册或充值,赞助后即可获取资源下载链接)
嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):78630559
本次赞助数额为: 2 元×
微信扫码支付:2 元
×
请留下您的邮箱,我们将在2小时内将文件发到您的邮箱
源码介绍
using System;
using System.Collections;
using System.Data;
using System.Drawing;
using System.Drawing.Printing;
using System.Windows.Forms;
namespace OmnipotentPrint
{
public class PrintClass
{
#region 全局变量
public static bool PageScape;
private readonly DataGridView dataGridView;
private readonly PageSetupDialog pageSetupDialog;
private readonly PrintDocument printDocument;
private readonly PrintPreviewDialog printPreviewDialog;
private readonly string title = "";
private readonly int titleSize = 16;
public int AlignmentSgin = -1;
public int BottomMargin = 80;
public bool Boundary;
public Brush Brush = new SolidBrush(Color.Black);
public Brush BrushCellBack = new SolidBrush(Color.White);
public Brush BrushCellFont = new SolidBrush(Color.Black);
public Brush BrushHeaderBack = new SolidBrush(Color.White);
public Brush BrushHeaderFont = new SolidBrush(Color.Black);
public Font CellFont = new Font("arial", 9);
public int CellLeftMargin = 40;
public int CellTopMargin = 6;
public int CenterAlignment;
public int ColGap = 5;
public Font FooterFont = new Font("arial", 8);
public int HeaderAlignment;
public Font HeaderFont = new Font("arial", 9, FontStyle.Bold);
public int HeaderHeight = 30;
public bool IsAutoPageRowCount = true;
public bool IsEveryPagePrintTitle;
public int LeftAlignment = 50;
public int LeftMargin = 50;
public Color LineColor = Color.Black;
public bool LineLeft = true;
public bool LineUnit = true;
public bool LineUp = true;
public bool NeedPrintPageIndex = true;
public bool PageAspect;
public int PageRowCount = 7;
public int PageSheet;
public int PrintPageHeight = 1169;
public int PrintPageWidth = 827;
public int RightAlignment = 50;
public int RowGap = 23;
public Font TextFont = new Font("arial", 10);
public Font TitleFont = new Font("arial", 14);
public int TopMargin = 60;
public Font UnderlineFont = new Font("arial", 8);
public int UnitAlignment;
public Font UplineFont = new Font("arial", 9, FontStyle.Bold);
public int X;
private int currentPageIndex;
private int pageCount;
private int rowCount;
private int xOffset;
#endregion
#region 打印信息的初始化
public PrintClass(DataGridView dataGridView, string title, int titleSize, int pageSheet, bool landScape)
{
this.title = title;
this.titleSize = titleSize;
this.dataGridView = dataGridView;
PageSheet = pageSheet;
printDocument = new PrintDocument();
pageSetupDialog = new PageSetupDialog();
pageSetupDialog.Document = printDocument;
printPreviewDialog = new PrintPreviewDialog();
printPreviewDialog.Document = printDocument;
printPreviewDialog.FormBorderStyle = FormBorderStyle.Fixed3D;
if (pageSheet >= 0)
{
if (landScape)
{
printDocument.DefaultPageSettings.Landscape = landScape;
}
else
{
printDocument.DefaultPageSettings.Landscape = landScape;
}
}
pageSetupDialog.Document = printDocument;
printDocument.PrintPage = PrintPage;
}
#endregion
#region 纸张大小的设置
public string PageSize(int n)
{
string pageSize = "";
switch (n)
{
case 1:
{
pageSize = "A5";
PrintPageWidth = 583;
PrintPageHeight = 827;
break;
}
case 2:
{
pageSize = "A6";
PrintPageWidth = 413;
PrintPageHeight = 583;
break;
}
case 3:
{
pageSize = "B5(ISO)";
PrintPageWidth = 693;
PrintPageHeight = 984;
break;
}
case 4:
{
pageSize = "B5(JIS)";
PrintPageWidth = 717;
PrintPageHeight = 1012;
break;
}
case 5:
{
pageSize = "Double Post Card";
PrintPageWidth = 583;
PrintPageHeight = 787;
break;
}
case 6:
{
pageSize = "Envelope #10";
PrintPageWidth = 412;
PrintPageHeight = 950;
break;
}
case 7:
{
pageSize = "Envelope B5";
PrintPageWidth = 693;
PrintPageHeight = 984;
break;
}
case 8:
{
pageSize = "Envelope C5";
PrintPageWidth = 638;
PrintPageHeight = 902;
break;
}
case 9:
{
pageSize = "Envelope DL";
PrintPageWidth = 433;
PrintPageHeight = 866;
break;
}
case 10:
{
pageSize = "Envelope Monarch";
PrintPageWidth = 387;
PrintPageHeight = 750;
break;
}
case 11:
{
pageSize = "ExeCutive";
PrintPageWidth = 725;
PrintPageHeight = 1015;
break;
}
case 12:
{
pageSize = "Legal";
PrintPageWidth = 850;
PrintPageHeight = 1400;
break;
}
case 13:
{
pageSize = "Letter";
PrintPageWidth = 850;
PrintPageHeight = 1100;
break;
}
case 14:
{
pageSize = "Post Card";
PrintPageWidth = 394;
PrintPageHeight = 583;
break;
}
case 15:
{
pageSize = "16K";
PrintPageWidth = 775;
PrintPageHeight = 1075;
break;
}
case 16:
{
pageSize = "8.5x13";
PrintPageWidth = 850;
PrintPageHeight = 1300;
break;
}
}
return pageSize;
}
#endregion
#region 页边距的设置
public void PrintSetUp(string[] margins)
{
if (margins[0] == "true")
{
TopMargin = Int32.Parse(margins[1]);
LeftMargin = Int32.Parse(margins[2]);
BottomMargin = Int32.Parse(margins[3]);
AlignmentSgin = -1;
}
}
#endregion
#region 文字的位置
private int AlignmentMode(int cellWidth, int stringWidth, int colLeftPadding, int alignment)
{
int enumAlignment = 0;
switch (alignment)
{
case 0:
{
enumAlignment = colLeftPadding;
break;
}
case 1:
{
enumAlignment = ((cellWidth - stringWidth)/2);
break;
}
case 2:
{
enumAlignment = cellWidth - stringWidth - colLeftPadding;
break;
}
}
return enumAlignment;
}
#endregion
#region 页的打印事件
private void PrintPage(object sender, PrintPageEventArgs e)
{
PrintPageWidth = e.PageBounds.Width;
PrintPageHeight = e.PageBounds.Height;
if (IsAutoPageRowCount)
PageRowCount = ((PrintPageHeight - TopMargin - titleSize - HeaderFont.Height - HeaderHeight -
BottomMargin)/RowGap);
pageCount = (rowCount/PageRowCount);
pageSetupDialog.AllowOrientation = true;
if (rowCount%PageRowCount > 0)
pageCount ;
int colCount = 0;
int Y = TopMargin;
string cellValue = "";
int startRow = currentPageIndex*PageRowCount;
int endRow = startRow PageRowCount < rowCount ? startRow PageRowCount : rowCount;
int currentPageRowCount = endRow - startRow;
colCount = dataGridView.ColumnCount;
X = LeftMargin;
int reportWidth = 0;
for (int j = 0; j < colCount; j )
{
if (dataGridView.Columns[j].Width > 0)
{
reportWidth = dataGridView.Columns[j].Width ColGap;
}
}
if (AlignmentSgin >= -1)
{
int headerWidth = (int) (e.Graphics.MeasureString(title, TitleFont).Width);
switch (AlignmentSgin)
{
case 0:
{
X = LeftAlignment;
LeftMargin = X;
if (headerWidth > reportWidth)
{
xOffset = LeftMargin;
}
else
{
xOffset = (PrintPageWidth - (PrintPageWidth - 50) (reportWidth - headerWidth)/2);
}
break;
}
case 1:
{
X = (PrintPageWidth - reportWidth)/2;
LeftMargin = X;
xOffset = ((PrintPageWidth - headerWidth)/2);
break;
}
case 2:
{
X = PrintPageWidth - reportWidth - RightAlignment;
LeftMargin = X;
if (headerWidth > reportWidth)
{
xOffset = (PrintPageWidth - headerWidth);
}
else
{
xOffset = (PrintPageWidth - 50 - reportWidth (reportWidth - headerWidth)/2);
}
break;
}
case -1:
{
if (headerWidth > reportWidth)
{
if ((headerWidth - reportWidth)/2 < LeftMargin)
{
xOffset = (LeftMargin - (headerWidth - reportWidth)/2);
}
else
{
xOffset = LeftMargin;
}
}
else
xOffset = (X (reportWidth - headerWidth)/2);
break;
}
}
}
if (currentPageIndex == 0 || IsEveryPagePrintTitle)
{
e.Graphics.DrawString(title, TitleFont, Brush, xOffset, Y);
Y = titleSize;
}
Y = RowGap;
Point headUp = new Point(X, Y);
Point headDown = new Point(reportWidth, HeaderHeight);
DrawRectangle(BrushHeaderBack, headUp, headDown, e.Graphics);
headUp = new Point(X, Y HeaderHeight);
headDown = new Point(reportWidth, (endRow - startRow)*RowGap);
DrawRectangle(BrushCellBack, headUp, headDown, e.Graphics);
if (LineLeft)
{
DrawLine(new Point(X, Y), new Point(X, Y currentPageRowCount*RowGap HeaderHeight), e.Graphics, 0);
}
for (int j = 0; j < colCount; j )
{
int colWidth = dataGridView.Columns[j].Width;
if (colWidth > 0)
{
cellValue = dataGridView.Columns[j].HeaderText;
int height = AlignmentMode(dataGridView.Columns[j].Width,
(int) (e.Graphics.MeasureString(cellValue, CellFont).Width),
CellLeftMargin, HeaderAlignment);
e.Graphics.DrawString(cellValue, HeaderFont, BrushHeaderFont, X height, Y CellTopMargin);
X = colWidth ColGap;
if (LineUnit)
{
DrawLine(new Point(X, Y), new Point(X, Y currentPageRowCount*RowGap HeaderHeight),
e.Graphics, 0);
}
if (LineLeft && (j == (colCount - 1)))
{
DrawLine(new Point(X, Y), new Point(X, Y currentPageRowCount*RowGap HeaderHeight),
e.Graphics, 0);
}
int nnp = Y currentPageRowCount*RowGap HeaderHeight;
}
}
int rightbound = X;
if (LineUp)
{
DrawLine(new Point(LeftMargin, Y), new Point(rightbound, Y), e.Graphics, 0);
}
headUp = new Point(LeftMargin, Y);
Y = HeaderHeight;
for (int i = startRow; i < endRow; i )
{
X = LeftMargin;
for (int j = 0; j < colCount; j )
{
if (dataGridView.Columns[j].Width > 0)
{
cellValue = dataGridView.Rows[i].Cells[j].Value.ToString();
int Ua = AlignmentMode(dataGridView.Columns[j].Width,
(int) (e.Graphics.MeasureString(cellValue, CellFont).Width),
CellLeftMargin, UnitAlignment);
e.Graphics.DrawString(cellValue, CellFont, BrushHeaderFont, X Ua, Y CellTopMargin);
X = dataGridView.Columns[j].Width ColGap;
Y = Y RowGap*(cellValue.Split(new[] {'\r', '\n'}).Length - 1);
}
}
if (LineUnit)
{
DrawLine(new Point(LeftMargin, Y), new Point(rightbound, Y), e.Graphics, 0);
}
if (Boundary && i == startRow)
{
DrawLine(new Point(LeftMargin, Y), new Point(rightbound, Y), e.Graphics, 1);
}
Y = RowGap;
}
if (LineUp)
{
DrawLine(new Point(LeftMargin, Y), new Point(rightbound, Y), e.Graphics, 0);
}
currentPageIndex ;
if (NeedPrintPageIndex)
{
e.Graphics.DrawString("共 " pageCount.ToString() " 页 第 " currentPageIndex.ToString() " 页",
FooterFont, Brush, PrintPageWidth - 200,
(PrintPageHeight - BottomMargin/2 - FooterFont.Height));
}
if (currentPageIndex < pageCount)
{
e.HasMorePages = true;
}
else
{
e.HasMorePages = false;
currentPageIndex = 0;
}
}
#endregion
#region 绘制边线
private void DrawLine(Point startPoint, Point endPoint, Graphics graphics, int flag)
{
int width = 1;
if (flag == 1)
{
width = 2;
}
Pen pen = new Pen(LineColor, width);
graphics.DrawLine(pen, startPoint, endPoint);
}
#endregion
#region 绘制填充的矩形框
private void DrawRectangle(Brush backColor, Point point1, Point point2, Graphics graphics)
{
graphics.FillRectangle(backColor, point1.X, point1.Y, point2.X, point2.Y);
}
#endregion
#region 显示打印预览窗体
public void Print()
{
rowCount = 0;
string paperName = PageSize(PageSheet);
PageSettings storePageSetting = new PageSettings();
foreach (PaperSize paperSize in printDocument.PrinterSettings.PaperSizes)
{
if (paperName == paperSize.PaperName)
{
storePageSetting.PaperSize = paperSize;
}
}
if (dataGridView.DataSource.GetType().ToString() == "System.Data.DataTable")
{
rowCount = ((DataTable) dataGridView.DataSource).Rows.Count;
}
else if (dataGridView.DataSource.GetType().ToString() == "System.Collections.ArrayList")
{
rowCount = ((ArrayList) dataGridView.DataSource).Count;
}
try
{
printDocument.DefaultPageSettings.Landscape = PageScape;
pageSetupDialog.Document = printDocument;
printPreviewDialog.ShowDialog();
}
catch (Exception e)
{
throw new Exception("Printer Error." e.Message);
}
}
#endregion
}
}