猫猫要努力学打印系列之网格数据
来源:互联网 发布:wps表格数据连接 编辑:程序博客网 时间:2024/06/06 18:02
这是我写的第一个打印程序,虽然很基本,但是我很高兴,拿出来和初学者和新人一起分享吧!
我做了个简单的Demo有需要的朋友可以下载:ftp://leike@211.137.100.89:2124/猫猫要努力学打印系列之网格数据.rar
提示:下面的类代码是没有任何Bug的(至少作者没有发现),Demo里面会出现一些Bug。
原因是我更新了类代码,没有去更新Demo 呵呵 不好意思了。如果有Bug欢迎指出谢谢.
using System.Drawing;
using System.Drawing.Printing;
namespace 企业资源计划
{
/// <summary>
/// 打印数组
/// </summary>
class PrintArray
{
int t = 0;
int rowIndex = 0 //行索引
, currentRowIndex = 0;//当前行索引
/// <summary>
/// 打印二维数组的方法
/// </summary>
/// <param name="e">为 System.Drawing.Printing.PrintDocument.PrintPage 事件提供数据</param>
/// <param name="Array2">string类型的二维数组 string[,] Array2</param>
public void PrintArray2(PrintPageEventArgs e, string[,] Array2)
{
float x, y;
//string[,] printTable = ToStringArray(dataGridView1, true);//dataGridView 转换为二维数组
SolidBrush s = new SolidBrush(Color.Red);//字体颜色
Font fnt = new Font("宋体", 9F, FontStyle.Regular);//字体大小和样式
y = e.MarginBounds.Top;//上边距
x = e.MarginBounds.Left;//左边距
float[] column = new float[Array2.GetLength(1)];//列宽
float x1Row = (float)e.MarginBounds.Left
, yRow = (float)e.MarginBounds.Top
, x2Row = (float)e.MarginBounds.Right;
for (int i = 0; i < Array2.GetLength(1); i++)
{
column[i] = 0;//列宽
for (int j = rowIndex; j < Array2.GetLength(0); j++)
{
if (column[i] < e.Graphics.MeasureString(Array2[j, i], fnt).Width)//取出当前列宽度最宽的字符串
{
column[i] = e.Graphics.MeasureString(Array2[j, i], fnt).Width;
}
yRow = yRow + fnt.GetHeight(e.Graphics) + 10;//当前行y点 + 字高 + 字行距 = 下行y点
if (yRow + 10 + fnt.GetHeight(e.Graphics) > e.MarginBounds.Height)//判断是否到了打印页最后一行
{
currentRowIndex = j;
break;
}
}
yRow = (float)e.MarginBounds.Top;
}
rowIndex = currentRowIndex;//记录当前行索引
for (; t < Array2.GetLength(0); t++)
{
for (int j = 0; j < Array2.GetLength(1); j++)
{
e.Graphics.DrawString(Array2[t, j], fnt, s, x + 5, y + 6);//字体边距 + 5,字体行距 + 6;
x = x + column[j] + 10;//字体与字体距离 + 10;
//if (x > e.MarginBounds.Width)
//{
// break;
//}
}
e.Graphics.DrawLine(new Pen(Color.Red), x1Row, yRow, x, yRow);//横线
yRow = yRow + fnt.GetHeight(e.Graphics) + 10;//当前行y点 + 字高 + 字行距 = 下行y点
y = y + fnt.GetHeight(e.Graphics) + 10;//当前字y点 + 字高 + 字行距 = 下行字y点
if (yRow + 10 + fnt.GetHeight(e.Graphics) > e.MarginBounds.Height)//判断下一行y点是否超出了打印页面的高度
{
e.HasMorePages = true;
t++;
break;
}
x = e.MarginBounds.Left;
}
x = e.MarginBounds.Left;
for (int i = 0; i < column.Length; i++)
{
x = x + column[i] + 10;//字体与字体距离 + 10;
e.Graphics.DrawLine(new Pen(Color.Red), x, e.MarginBounds.Top, x, yRow);//竖线(右端线包含在内)
//if (x > e.MarginBounds.Width)
//{
// //MessageBox.Show("您打印的行超出了页面");
// break;
//}
}
e.Graphics.DrawLine(new Pen(Color.Red), e.MarginBounds.Left, e.MarginBounds.Top, e.MarginBounds.Left, yRow);//左端线
e.Graphics.DrawLine(new Pen(Color.Red), x1Row, yRow, x, yRow);//低线
if (e.HasMorePages == false)//如果是最后一页了,重置全局变量
{
t = 0;
rowIndex = 0; //行索引
currentRowIndex = 0;//当前行索引
}
}
}
}
- 猫猫要努力学打印系列之网格数据
- HGE系列之管中窥豹(变形网格)
- 继续努力学编程
- 努力学Ajax
- 努力学自个的
- 努力学吧
- 努力学英语
- 关于打印系列之二
- 关于打印系列之三
- 编程系列之:矩阵打印
- GEF学习系列之二:网格吸附与几何对齐
- OpenGL系列教程之十一:OpenGL网格化
- OpenGL系列教程之十一:OpenGL网格化
- SilverLight:一步一步学Silverlight 2系列(1):数据与通信之WebClient
- 一步一步学Silverlight 2系列(12):数据与通信之WebClient
- 一步一步学Silverlight 2系列(13):数据与通信之WebRequest
- 一步一步学Silverlight 2系列(14):数据与通信之WCF
- 一步一步学Silverlight 2系列(15):数据与通信之ASMX
- (c# Data Access 1) 与数据库连接
- 被一个印度人缠上了>_<
- 在Linux和windows上搞C开发有何区别?
- SQL Server 2005中设置Reporting Services发布web报表的匿名访问
- 【转载】比尔·盖茨是如何花钱的
- 猫猫要努力学打印系列之网格数据
- [-生活随笔-] 何去何从?
- C语言嵌入式系统编程修炼
- 在读一本书:《应用框架的设计与实现.NET平台》
- 成为嵌入式程序员应知道的问题
- Tomcat5.5启动时提示“The Apache Tomcat Native library which allows optimal performance in production environments was not found”
- C语言函数指针参数也是传值调用
- MODBUS通讯协议及编程
- MODBUS-RTU通讯协议简介