使用C#实现操作Word打印-创新互联
使用C#实现操作Word打印?相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。

class PrintClass
{
#region 全局变量
private DataGridView datagrid;//需要打印的数据来源
private PageSetupDialog pagesetupdialog;
private PrintPreviewDialog printpreviewdialog;
int currentpageindex = 0;//当前页的编号
int rowcount = 0;//数据的行数
public Size PaperSize = new Size(827, 1169);//答应的纸张大小
public int headerheight = 30;//标题高度
Margins margins = new Margins(50, 60, 50, 80);
public int celltopmargin = 6;//单元格顶边距
public int pagerowcount = 7;//每页行数
public int rowgap = 23;//行高
public int colgap = 5;//每列间隔
public Font headerfont = new Font("Arial", 9, FontStyle.Bold);//列名标题字体
public Brush brushHeaderFont = new SolidBrush(Color.Black);//列名字体画刷
public Font Cellfont = new Font("Arial", 9);//单元格字体
public bool isautopagerowcount = true;//是否自动计算行数
public bool PageAspect = false;//打印的方向
public static bool PageScape = false;//打印方向
public string paperName = string.Empty;
#endregion
#region 打印信息的初始化
///
/// 打印信息的初始化
///
/// 打印数据
/// 纸张大小
/// 是否横向打印
public PrintClass(DataGridView datagrid, string paperName, bool lendscape)
{
this.datagrid = datagrid;//获取打印数据
this.paperName = paperName;
PrintDocument printdocument = new PrintDocument();//实例化PrintDocument类
printpreviewdialog = new PrintPreviewDialog();//实例化PrintPreviewDialog类
printpreviewdialog.Document = printdocument;//获取预览文档的信息
printpreviewdialog.FormBorderStyle = FormBorderStyle.Fixed3D;//设置窗体的边框样式
//横向打印的设置
if (!string.IsNullOrEmpty(paperName) )
{
if (lendscape == true)
{
printdocument.DefaultPageSettings.Landscape = lendscape;//横向打印
}
else
{
printdocument.DefaultPageSettings.Landscape = lendscape;//纵向打印
}
}
pagesetupdialog = new PageSetupDialog();//实例化PageSetupDialog类
pagesetupdialog.Document = printdocument;//获取当前页的设置
printdocument.PrintPage += new PrintPageEventHandler(this.printdocument_printpage);//事件的重载
}
#endregion
#region 页的打印事件
///
/// 页的打印事件(主要用于绘制打印报表)
///
private void printdocument_printpage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
if (this.isautopagerowcount)//自动计算页的行数
{
double countHeight = e.PageBounds.Height - this.margins.Top - this.headerfont.Height - this.headerheight - this.margins.Bottom;
pagerowcount = (int)Math.Ceiling(countHeight / this.rowgap);//获取每页的行数
}
int pagecount = (int)(rowcount / pagerowcount);//获取打印多少页
pagesetupdialog.AllowOrientation = true;//启动打印页面对话框的方向部分
int colcount = 0;//记录数据的列数
int y = margins.Top;//获取表格的顶边距
string cellvalue = "";//记录文本信息(单元格的文本信息)
int startrow = currentpageindex * pagerowcount;//设置打印的初始页数
int endrow = startrow + this.pagerowcount < rowcount ? startrow + pagerowcount : rowcount;//设置打印的大页数
int currentpagerowcount = endrow - startrow;//获取打印页数
colcount = datagrid.ColumnCount;//获取打印数据的列数
int x = margins.Left;//获取表格的左边距 绘画时的x轴位置
//获取报表的宽度
int cwidth = 0;
for (int j = 0; j < colcount; j++)//循环数据的列数
{
if (datagrid.Columns[j].Width > 0)//如果列的宽大于0
{
cwidth += datagrid.Columns[j].Width + colgap;//累加每列的宽度
}
}
y += rowgap;//设置表格的上边线的位置
//设置标题栏中的文字
for (int j = 0; j < colcount; j++)//遍历列数据
{
int colwidth = datagrid.Columns[j].Width;//获取列的宽度
if (colwidth > 0)//如果列的宽度大于0
{
cellvalue = datagrid.Columns[j].HeaderText;//获取列标题
//绘制标题栏文字
e.Graphics.DrawString(cellvalue, headerfont, brushHeaderFont, x, y + celltopmargin);//绘制列标题
x += colwidth + colgap;//横向,下一个单元格的位置
int nnp = y + currentpagerowcount * rowgap + this.headerheight;//下一行线的位置
}
}
//打印所有的行信息
for (int i = startrow; i < endrow; i++) //对行进行循环
{
x = margins.Left; //获取线的X坐标点
for (int j = 0; j < colcount; j++)//对列进行循环
{
if (datagrid.Columns[j].Width > 0)//如果列的宽度大于0
{
cellvalue = datagrid.Rows[i].Cells[j].Value.ToString();//获取单元格的值
e.Graphics.DrawString(cellvalue, Cellfont, brushHeaderFont, x, y + celltopmargin+rowgap);//绘制单元格信息
x += datagrid.Columns[j].Width + colgap;//单元格信息的X坐标
y = y + rowgap * (cellvalue.Split(new char[] { '\r', '\n' }).Length - 1);//单元格信息的Y坐标
}
}
y += rowgap;//设置下行的位置
}
currentpageindex++;//下一页的页码
if (currentpageindex < pagecount)//如果当前页不是最后一页
{
e.HasMorePages = true;//打印副页
}
else
{
e.HasMorePages = false;//不打印副页
this.currentpageindex = 0;//当前打印的页编号设为0
}
}
#endregion
#region 显示打印预览窗体
///
/// 显示打印预览窗体
///
public void print()
{
rowcount = 0;//记录数据的行数
PageSettings storePageSetting = new PageSettings();//实列化一个对PageSettings对象
PrintDocument printdocument = pagesetupdialog.Document;
foreach (PaperSize ps in printdocument.PrinterSettings.PaperSizes)//查找当前设置纸张
{
if (paperName == ps.PaperName)//如果找到当前纸张的名称
{
printdocument.DefaultPageSettings.PaperSize = ps;//获取当前纸张的信息
}
}
if (datagrid.DataSource is System.Data.DataTable)//判断数据类型
{
rowcount = ((DataTable)datagrid.DataSource).Rows.Count;//获取数据的行数
}
else if (datagrid.DataSource is System.Collections.ArrayList)//判断数据类型
{
rowcount = ((ArrayList)datagrid.DataSource).Count;//获取数据的行数
}
try
{
printdocument.DefaultPageSettings.Landscape = PageScape;//设置横向打印
printpreviewdialog.ShowDialog();//显示打印预览窗体
}
catch (Exception e)
{
throw new Exception("printer error." + e.Message);
}
}
#endregion
} 分享文章:使用C#实现操作Word打印-创新互联
文章网址:http://www.jxjierui.cn/article/deigdc.html


咨询
建站咨询
