纯手动写的打印代码。(高手近来指点下,将不胜感激!)
打印表格,不用水晶报表(因它有期限限制),表是从数据库调用。程序总不能实现打印预览。高手帮帮忙吧!
代码如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Printing;
using System.Collections;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Configuration;
using System.IO;
namespace Motuoxinyuan
{
public partial class Form1 : Form
{
private uint lineHeight;
int m=150, n=150;
int k=600,q=0;
int l = 0;
//(m,n)为表格的最左上角的一点。K为表格的行的长度
public int linesTable ;
public int columTable ;
public int pagesTable;
int a = 15; //表格中每个单元格的高
int b = 50;//表格中每个单元格的宽
Graphics g;
Pen p = new Pen(Color.Blue, 2);
public string[,] wo;
public static int shengLines;
private PageSettings pageSet;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
// TODO: 这行代码将数据加载到表“aaaDataSet.Motuo”中。您可以根据需要移动或移除它。
// this.motuoTableAdapter.Fill(this.aaaDataSet.Motuo);
string str = ConfigurationSettings.AppSettings["dsn"];
SqlConnection con = new SqlConnection(str);
con.Open();
string sqlstr = "select * from Motuo";
//SqlCommand com=new SqlCommand (sqlstr ,con )
SqlDataAdapter da = new SqlDataAdapter(sqlstr, con);
DataSet ds = new DataSet();
da.Fill(ds);
;
int i = 0;
int j = 0;
int u = (int)ds.Tables[0].Columns.Count + 1;
int v = (int)ds.Tables[0].Rows.Count + 1;
string[,] wo = new string[v, u];
//把数据库的表中的数据读入二维数组WO
foreach (DataColumn dc in ds.Tables[0].Columns)
{
wo[i, j] = dc.Caption.ToString();
j++;
}
i = 1;
foreach (DataRow dr in ds.Tables[0].Rows)
{
for (int q = 0; q < ds.Tables[0].Columns.Count; q++)
{
wo[i, q] = dr[q].ToString();
}
i++;
}
int linesTable = wo.GetLength(0);
int columTable = wo.GetLength(1);
int pagesTable = wo.GetLength(0) / 10;
int shengLines = wo.GetLength(0) % 10;
}
private void printDocument1_PrintPage_1(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
float height = e.MarginBounds.Height;
float leftMargine = e.MarginBounds.Left;//x=100,y=100,width=627,height=969
float topMargine = e.MarginBounds.Top;
int linesPerPage = (int)(height / lineHeight);
Graphics g =e.Graphics ;
tableStar(g);
tableEnd(g);
PaintDocument ( g);
}
private void PaintDocument(Graphics g)
{
for (int f = 0; f < pagesTable; f++)
{
tableStar(g);//打印表头
tablePrint(g);//打印表
tableEnd(g);//打印表尾
}
}
private void tableStar( Graphics g)
{
g.DrawString("姓名:王望",this .Font ,Brushes.Red , 100,100 );
;
g.DrawString("地址:河北", this.Font, Brushes.Blue, 100, 115);
g.DrawString("电话:5555555", this.Font, Brushes.Blue, 100, 130);
g.DrawString("From", this.Font, Brushes.Blue, 450, 100);
g.DrawString("单位:摩托配件有限公司", this.Font, Brushes.Blue, 500, 100);
g.DrawString("地址:开发区158号", this.Font, Brushes.Blue, 500, 115);
g.DrawString("电话:010-854568 859654", this.Font, Brushes.Blue, 500, 130);
Font titleFont = new Font("Arial", 15);
g.DrawString("摩托配件有限公司", titleFont, Brushes.Black, 250, 150);
}
private void tablePrint( Graphics g )
{
int x;
if ((linesTable - pagesTable * 10) > 10)
x = 10;
else
x = shengLines;
for (int i = 0; i <= x; i++)
{
g.DrawLine(p, m + a * i, n + a * i, k, n + a * i);
for (int j = 0; j <= columTable; j++)
{
g.DrawLine (p,m+a*i+b*j ,n +a *i ,m +b*j,n +a*(i+1));
string str = wo[l, j].ToString();
g.DrawString( str ,this .Font ,Brushes .Black , m + a * i + b * j + 5, n + a * i + 5);
}
l++;
}
}
private void tableEnd(Graphics g)
{
g.DrawString("制表人:天台 ", this .Font ,Brushes .Black , 600, 920);
g.DrawLine (p,100,940,650,940);
g.DrawString ("地址:开发区玉泰路158号",this .Font ,Brushes .Blue ,100,960);
}
private void 页面设置ToolStripMenuItem_Click(object sender, EventArgs e)
{
try
{
if (this.pageSet == null)
this.pageSet = new PageSettings();
pageSetupDialog1.PageSettings = this.pageSet;
pageSetupDialog1.ShowDialog();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void 打印预览toolStripMenuItem2_Click(object sender, EventArgs e)
{
printPreviewDialog1.Document = printDocument1;
printPreviewDialog1.ShowDialog();
}
private void 打印ToolStripMenuItem_Click(object sender, EventArgs e)
{
}
}
}