代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
CSharp
】
打印datatable
作者:
Ben
/ 发布于
2015/7/15
/
830
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows.Forms; using System.Drawing; using System.Drawing.Printing; using System.Data; namespace PrintDemo { class PrintInfoExt { public PrintInfoExt() { this.dataHead = "打印标题"; this.headFont = new Font("黑体", 18, FontStyle.Bold); this.dataFont = new Font("宋体", 10); this.landscape = false; } /// /// 获取设置标题头 /// public String dataHead { set; get; } /// /// 获取或设置标题格式 /// public Font headFont { set; get; } /// /// 获取或设置数据字体格式 /// public Font dataFont { set; get; } /// /// 设定是否是横向打印 /// public Boolean landscape { set; get; } /// <summary> /// 表内容,格式化后 /// </summary> public string[,] datas{ set; get; } /// <summary> /// 表列数 /// </summary> public int columns { set; get; } /// <summary> /// 表行数 /// </summary> public int rows { set; get; } /// <summary> /// 打印的当前行 /// </summary> public int currentRow = 0; /// <summary> /// 当前页号 /// </summary> public int currentPage = 0; /// <summary> /// 格式化表数据 /// </summary> public void initData(DataTable dt, int width=4, int flag=1) { string[,] DT_data = null; switch (flag) { case 0: columns = dt.Columns.Count; rows = dt.Rows.Count + 1; DT_data = new string[rows, columns]; for (int i = 0; i < dt.Columns.Count; i++) { DT_data[0, i] = dt.Columns[i].ColumnName; } for (int i = 0; i < dt.Rows.Count; i++) { for (int j = 0; j < dt.Columns.Count; j++) { DT_data[i + 1, j] = dt.Rows[i][j].ToString(); } } break; case 1: columns = dt.Rows.Count + 1; rows = dt.Columns.Count; DT_data = new string[rows, columns]; for (int i = 0; i < dt.Columns.Count; i++) { DT_data[i, 0] = dt.Columns[i].ColumnName; } for (int i = 0; i < dt.Rows.Count; i++) { for (int j = 0; j <dt.Columns.Count; j++) { DT_data[j, i + 1] = dt.Rows[i][j].ToString(); } } break; case 2: rows = dt.Rows.Count + 1; if (rows > width) { rows = width; } int colPlus = (dt.Rows.Count + 1) / rows; int colTail = (dt.Rows.Count + 1) % rows; if (colTail > 0) colPlus++; columns = dt.Columns.Count * colPlus; DT_data = new string[rows, columns]; for (int i = 0; i < dt.Columns.Count; i++) { for (int j = 0; j < colPlus; j++)//列 { for (int k = 0; k < rows; k++)//行 { if (j == 0 && k == 0) { DT_data[k, i * colPlus] = dt.Columns[i].ColumnName; continue; } //原所在行 int n = (rows - 1) * j + k - 1; if (k == 0 || n >= dt.Rows.Count) { DT_data[k, i * colPlus + j] = " "; } else { DT_data[k, i * colPlus + j] = dt.Rows[n][i].ToString(); } } } } break; default: MessageBox.Show("出错", "无打印类型", MessageBoxButtons.OK, MessageBoxIcon.Error); break; } if (DT_data != null) { datas = DT_data; } } public void printDataTable() { PrintDocument pd = new System.Drawing.Printing.PrintDocument(); PrinterSettings pss = new System.Drawing.Printing.PrinterSettings(); pss.DefaultPageSettings.Landscape = landscape; pd.PrinterSettings = pss; pd.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(pd_PrintPage); PrintPreviewDialog ppd = new PrintPreviewDialog(); ppd.Document = pd; if (datas == null) { MessageBox.Show("出错", "没有可以打印的数据", MessageBoxButtons.OK, MessageBoxIcon.Error); } else if (ppd.ShowDialog() == DialogResult.OK) { try { //pd.Print(); ppd.ShowDialog(); } catch (Exception ex) { MessageBox.Show("出错", ex.Message, MessageBoxButtons.OK, MessageBoxIcon.Error); } } } private void pd_PrintPage(object sender, PrintPageEventArgs e) { Graphics graphic = e.Graphics;//获取绘图对象 int leftMargin = e.MarginBounds.Left;//左边距 int topMargin = e.MarginBounds.Top;//上边距 int yPosition = 0;//绘制字符串的纵向位置 int xPosition = 0;//绘制字符串的横向位置 string line = string.Empty;//读取的行字符串 float lineHeight = dataFont.GetHeight(graphic) + 4; //行高 int charWidth = (int)(dataFont.GetHeight(graphic) + 1);//字符宽 SolidBrush brush = new SolidBrush(Color.Black);//刷子 Rectangle rect; //内容绘制矩形 int linesPerPage = (int)(e.MarginBounds.Height / lineHeight);//每页可打印的行数 int currentLine = 0; //如果首页 if (currentPage < 1) { //首先打印标题 StringFormat sf = new StringFormat(); sf.Alignment = StringAlignment.Center; rect = new Rectangle(leftMargin, topMargin, e.MarginBounds.Width, e.MarginBounds.Height); graphic.DrawString(dataHead, headFont, brush, rect, sf); topMargin += 60; currentLine = linesPerPage - (int)((e.MarginBounds.Height - 60) / lineHeight);//当前页的当前行;由于首页有标题 } //计算平均宽度--由于打印倒置,有行数计算平均宽度 int width = (int)(e.MarginBounds.Width / rows); // alinnment of data StringFormat sfData = new StringFormat(); sfData.Alignment = StringAlignment.Center; sfData.LineAlignment = StringAlignment.Center; //下面开始画表格 Point ptS; Point ptE; int rightMargin = leftMargin + width * rows; //先划封顶横线 ptS = new Point(leftMargin, topMargin); ptE = new Point(rightMargin, topMargin); graphic.DrawLine(Pens.Black, ptS, ptE); //int countNum = 0; //当前行 yPosition = topMargin; int maxHeight = 1; while (currentRow < columns) //按列打印 { if (currentLine < linesPerPage)//linesPerPage { xPosition = leftMargin; maxHeight = 1; int height = 0; //绘制当前行 for (int j = 0; j < rows; j++) { int length = 0; for (int k = 0; k < rows; k++) { int _length = datas[k, currentRow].Length * charWidth; if (length < _length) { length = _length; } } if (length > width) { int l = (int)(length / width) + 1; if (l > maxHeight) { maxHeight = l; } } line = datas[j, currentRow]; height = (int)(lineHeight * maxHeight + 1); if (j < 1) { ptS = new Point(xPosition, yPosition); ptE = new Point(xPosition, yPosition + height); graphic.DrawLine(Pens.Black, ptS, ptE); } graphic.DrawString(line, dataFont, brush, new Rectangle(xPosition, yPosition, width, height), sfData); xPosition += width; ptS = new Point(xPosition, yPosition); ptE = new Point(xPosition, yPosition + height); graphic.DrawLine(Pens.Black, ptS, ptE); } //当前行 currentRow++; currentLine += maxHeight; yPosition += height; ptS = new Point(leftMargin, yPosition); ptE = new Point(xPosition, yPosition); graphic.DrawLine(Pens.Black, ptS, ptE); } else { //分页处理 currentPage++; currentLine = 0; line = null; break; } }//while if (line == null) { e.HasMorePages = true; } else { e.HasMorePages = false; currentPage = 0; currentRow = 0; currentLine = 0; } }//func } }
试试其它关键字
打印
datatable
同语言下
.
文件IO 操作类库
.
Check图片类型[JPEG(.jpg 、.jpeg),TIF,GIF,BMP,PNG,P
.
机器名和IP取得(IPV4 IPV6)
.
Tiff转换Bitmap
.
linqHelper
.
MadieHelper.cs
.
RegHelper.cs
.
如果关闭一个窗体后激活另一个窗体的事件或方法
.
创建日志通用类
.
串口辅助开发类
可能有用的
.
C#实现的html内容截取
.
List 切割成几份 工具类
.
SQL查询 多列合并成一行用逗号隔开
.
一行一行读取txt的内容
.
C#动态修改文件夹名称(FSO实现,不移动文件)
.
c# 移动文件或文件夹
.
c#图片添加水印
.
Java PDF转换成图片并输出给前台展示
.
网站后台修改图片尺寸代码
.
处理大图片在缩略图时的展示
Ben
贡献的其它代码
(
12
)
.
单位转换类 DensityUtils
.
生成透明的二维码
.
实现爬虫下载美女图片
.
屏蔽关键字
.
利用js,无插件完成报表打印
.
ip地址和int相互转换
.
日期是否合法
.
打印datatable
.
webservice双工通讯
.
android下载工具类
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3