您的位置:首页 > 编程语言 > C#

C#打印表格

2017-07-11 15:59 155 查看


C#-WinForm-打印控件

打印控件(全添加)



绘制如下窗体



一、PrintDocument -打印的基础  首先放置PrintDocument控件,双击事件PrintPage设置要打印的样式(李献策lxc)


  




//第一步 设置打印属性
private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
//设置字体样式
Font f = new System.Drawing.Font("微软雅黑", 16);
f = richTextBox1.Font;
//设置字体颜色
Brush b = new SolidBrush(richTextBox1.ForeColor);

//e.绘制.画字符串(要打印的文本,文本格式,画刷-颜色和纹理,位置坐标)
e.Graphics.DrawString(richTextBox1.Text, f, b, 20, 10);
}
//字体样式设置
private void button4_Click(object sender, EventArgs e)
{
DialogResult dr = fontDialog1.ShowDialog();
if (dr == DialogResult.OK)
{
richTextBox1.Font = fontDialog1.Font;
richTextBox1.ForeColor = fontDialog1.Color;
}
}


二、PageSetupDialog - 打印页面设置



//第二步 设置打印页面设置
private
4000
void button1_Click(object sender, EventArgs e)
{
pageSetupDialog1.Document = printDocument1;
pageSetupDialog1.ShowDialog();
}




三、1、PrintPreviewContol - 打印预览格式一,在窗体中设置预览区域



//第三步 打印预览一
private void button2_Click(object sender, EventArgs e)
{
printPreviewControl1.Document = printDocument1;
}




2、PrintPreviewDialog - 打印预览格式二,在弹窗中预览



//第三步 打印预览二
private void button2_Click(object sender, EventArgs e)
{
printPreviewDialog1.Document = printDocument1;
printPreviewDialog1.ShowDialog();

}






四、PrintDialog - 开始打印



 //第四步 开始打印
private void button3_Click(object sender, EventArgs e)
{
printDialog1.Document = printDocument1;
DialogResult dr = printDialog1.ShowDialog();
if (dr == DialogResult.OK)
{
printDocument1.Print();}}




 

Pen blackPen = new Pen(Color.Black, 1);//黑色,1像素            //打印样式            System.Drawing.Font f = new System.Drawing.Font("黑体", 10, System.Drawing.FontStyle.Regular);            System.Drawing.Font TableFont = new Font("宋体", 10, FontStyle.Regular);            System.Drawing.Font boldFont = new System.Drawing.Font("黑体", 12, System.Drawing.FontStyle.Bold);            System.Drawing.Font msgf = new System.Drawing.Font("黑体", 14, System.Drawing.FontStyle.Regular);
string[,] strArr = new string[2, 6]; //声明一个二维数组. strArr[0, 0] = "类别"; strArr[0, 1] = "老师"; strArr[0, 2] = "工作单位"; strArr[0, 3] = "信息大学"; strArr[0, 4] = "收费标准"; strArr[0, 5] = 12 + "元";
strArr[1, 0] = "姓名"; strArr[1, 1] = "陈小莉"; strArr[1, 2] = "性别"; strArr[1, 3] = "女"; strArr[1, 4] = "年龄"; strArr[1, 5] = "" + 20;
int docX = 20; int docY = 50; int tableW = 600;//表宽 int tableH = 2 * 50;//表高 int tableX = docX;//表的起点X坐标 int tableY = docY;//表的起点Y坐标 int rowWidth = 50;//行宽 int colwidth = tableW / 6;//列宽 for (int i = 0; i <= 2; i++) { e.Graphics.DrawLine(blackPen, tableX, tableY + i * rowWidth, tableX + tableW, tableY + i * rowWidth); for (int j = 0; j <= 6; j++) { if (i != 2) { e.Graphics.DrawLine(blackPen, tableX + j * colwidth, tableY + i * rowWidth, tableX + j * colwidth, tableY + i * rowWidth + rowWidth); if (j != 6) { if (j % 2 == 0) { int textWidth = Convert.ToInt32(e.Graphics.MeasureString(strArr[i, j], boldFont).Width); int textHeight = Convert.ToInt32(e.Graphics.MeasureString(strArr[i, j], boldFont).Height); int centerX = (colwidth - textWidth) / 2; //字体X轴居中 int centerY = (rowWidth - textHeight) / 2; //字体Y轴居中 e.Graphics.DrawString(strArr[i, j], boldFont, Brushes.Black, tableX + j * colwidth + centerX, tableY + i * rowWidth + centerY);//写列名 } else {
e.Graphics.DrawString(strArr[i, j], TableFont, Brushes.Black, tableX + j * colwidth + 15, tableY + i * rowWidth + 15);//写列名 } } } } }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  c# 控件