纯手动写的打印代码。(高手近来指点下,将不胜感激!)

qzh1119 2006-09-06 10:32:38
打印表格,不用水晶报表(因它有期限限制),表是从数据库调用。程序总不能实现打印预览。高手帮帮忙吧!
代码如下:
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)
{

}

}
}

...全文
451 9 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
Ice_wang 2006-09-07
  • 打赏
  • 举报
回复
给个邮箱我给你发过去
ilove8 2006-09-06
  • 打赏
  • 举报
回复
比较纯啊!
jackyped 2006-09-06
  • 打赏
  • 举报
回复
用.net的打印预览不行?》
Knight94 2006-09-06
  • 打赏
  • 举报
回复
参看别人的例子
http://www.codeproject.com/cs/miscctrl/SUPrintPreviewDialog.asp
jointan 2006-09-06
  • 打赏
  • 举报
回复
PrintPreviewDialog不能用你那种方式用,PrintPreviewDialog在每次Show完后,会自动销毁,所以不能用做成员变量
用如下方式:
PrintPreviewDialog dlg = new PrintPreviewDialog();
dlg.Document = this.printDocument1;
dlg.Show();

如果在非MDI环境下,要用dlg.ShowDialog()否则窗口一但失去焦点,就不好找了,MDI环境下则需要加一句
dlg.MdiParent = this.MdiParent;
zhyzhr 2006-09-06
  • 打赏
  • 举报
回复
楼上的,干嘛不问“你的电脑装CPU了吗?”
zxcayumi 2006-09-06
  • 打赏
  • 举报
回复
安装打印机了吗?
qzh1119 2006-09-06
  • 打赏
  • 举报
回复
本人连续发了好几个贴,解决关于打印的问题,但就是没弄明白啊。我的分都没了。好心人帮帮忙
qzh1119 2006-09-06
  • 打赏
  • 举报
回复
给点中文资料吧。

111,097

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧