求助!!! winfrom的打印与套打

ai6541796 2010-10-20 01:27:22
小弟对winfrom的打印从来没接触过
网上找了好久 也搞不清

特在此向各位大小求助

公司给了一个 excel的模板 和一张套打的图片

希望 哪位好心人 给点 实例代码 (有注释的) 我只有这么一点分了

麻烦各位了
...全文
283 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
梦想家 2011-06-02
  • 打赏
  • 举报
回复


public class PrintBonus//打印分红
{
private System.Drawing.Printing.PrintDocument printdoc;

private decimal decX = 0.4m;
private decimal decY = 0.4m; //修正
System.Drawing.Font printFont = new Font("Trebuchet MS", 7);//默认的数字字体
System.Drawing.SolidBrush brush = new SolidBrush(System.Drawing.Color.Black);
FormShareHolderManage frm = new FormShareHolderManage();

//输入厘米转换成像素
private Point GetPoint(Decimal x, Decimal y)
{
return new Point((int)(x * 0.3937m * 100), (int)(y * 0.3937m * 100));
}

private RectangleF GetRect(Point point)
{
return new RectangleF(point.X, point.Y, (float)(15 * 0.3937m * 100), printFont.Height + 1);
}

private void DrawString(System.Drawing.Printing.PrintPageEventArgs e, string s, Point point)
{
e.Graphics.DrawString(s, printFont, brush, GetRect(point));
}

int page = 0;//开始打印的页数
int line = 0;//开始打印的行数

DataTable printData = null;
decimal offset = 0;
public PrintBonus(int page, int line, DataTable pd , decimal os)
{
this.page = page;
this.line = line;
this.printData = pd;
offset = os;
}

public void PrintPreview()
{
this.printdoc = new System.Drawing.Printing.PrintDocument();
this.printdoc.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(printdoc_PrintPage);
PrintPreviewDialog cppd = new PrintPreviewDialog();
cppd.Document = printdoc;
cppd.ShowDialog();
}


public void Print()
{
this.printdoc = new System.Drawing.Printing.PrintDocument();
this.printdoc.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(printdoc_PrintPage);
this.printdoc.Print();
}

int cLine = 0;//计数器
int count = 0;
private void printdoc_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{

while (count + line - 1 < 12 && printData.Rows.Count > cLine)
{
decimal d = 9.35m + Convert.ToDecimal((count + line) * 0.38);
if (page % 2 == 0)
{
d = 1.9m + Convert.ToDecimal((count + line) * 0.38);
}
//string date = Convert.ToDateTime(printData.Rows[count]["日期"]).ToString("yyMM");//居中对齐
//DrawString(e, date, GetPoint(0.9m + (1.1m - frm.GetFontWidth(printFont, date)) / 2 - decX, d - decY));
//string amount = printData.Rows[count]["股份数量"].ToString();//右对齐
//DrawString(e, amount, GetPoint(3.6m - frm.GetFontWidth(printFont, amount) - decX, d - decY));
//string bonus = printData.Rows[count]["每股分红"].ToString();
//DrawString(e, bonus, GetPoint(3.7m + (1.6m - frm.GetFontWidth(printFont, bonus)) / 2 - decX, d - decY));
//string total = printData.Rows[count]["分红总额"].ToString();
//DrawString(e, total, GetPoint(6.9m - frm.GetFontWidth(printFont, total) - decX, d - decY));
string tax = printData.Rows[count]["扣税"].ToString();
if (tax == "0")
{
DrawString(e, "", GetPoint(7.5m - decX, d - decY));
}
else
{
DrawString(e, tax, GetPoint(8.5m - frm.GetFontWidth(printFont, tax) - decX, d - decY + offset));
}

string aTax = printData.Rows[count]["税后分红"].ToString();
// DrawString(e, aTax, GetPoint(9.3m - frm.GetFontWidth(printFont, aTax) - decX, d - decY));
DrawString(e, aTax, GetPoint(9.3m - frm.GetFontWidth(printFont, aTax) - decX, d - decY + offset));
//DrawString(e, printData.Rows[count]["备注"].ToString(), GetPoint(10.35m - decX, d - decY));
count++;
cLine++;
}
if (printData.Rows.Count > cLine)
{
e.HasMorePages = true;
line = 1;
page++;
}
else
{
e.HasMorePages = false;
}
}
}



爱睡觉的猫 2010-10-23
  • 打赏
  • 举报
回复
http://wenku.baidu.com/view/328a9c22192e45361066f5ab.html 打印流程
freeboy827 2010-10-20
  • 打赏
  • 举报
回复
你需要什么条形,生成图片,打印图片就行了
freeboy827 2010-10-20
  • 打赏
  • 举报
回复

public class PrintBonus//打印分红
{
private System.Drawing.Printing.PrintDocument printdoc;

private decimal decX = 0.4m;
private decimal decY = 0.4m; //修正
System.Drawing.Font printFont = new Font("Trebuchet MS", 7);//默认的数字字体
System.Drawing.SolidBrush brush = new SolidBrush(System.Drawing.Color.Black);
FormShareHolderManage frm = new FormShareHolderManage();

//输入厘米转换成像素
private Point GetPoint(Decimal x, Decimal y)
{
return new Point((int)(x * 0.3937m * 100), (int)(y * 0.3937m * 100));
}

private RectangleF GetRect(Point point)
{
return new RectangleF(point.X, point.Y, (float)(15 * 0.3937m * 100), printFont.Height + 1);
}

private void DrawString(System.Drawing.Printing.PrintPageEventArgs e, string s, Point point)
{
e.Graphics.DrawString(s, printFont, brush, GetRect(point));
}

int page = 0;//开始打印的页数
int line = 0;//开始打印的行数

DataTable printData = null;
decimal offset = 0;
public PrintBonus(int page, int line, DataTable pd , decimal os)
{
this.page = page;
this.line = line;
this.printData = pd;
offset = os;
}

public void PrintPreview()
{
this.printdoc = new System.Drawing.Printing.PrintDocument();
this.printdoc.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(printdoc_PrintPage);
PrintPreviewDialog cppd = new PrintPreviewDialog();
cppd.Document = printdoc;
cppd.ShowDialog();
}


public void Print()
{
this.printdoc = new System.Drawing.Printing.PrintDocument();
this.printdoc.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(printdoc_PrintPage);
this.printdoc.Print();
}

int cLine = 0;//计数器
int count = 0;
private void printdoc_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{

while (count + line - 1 < 12 && printData.Rows.Count > cLine)
{
decimal d = 9.35m + Convert.ToDecimal((count + line) * 0.38);
if (page % 2 == 0)
{
d = 1.9m + Convert.ToDecimal((count + line) * 0.38);
}
//string date = Convert.ToDateTime(printData.Rows[count]["日期"]).ToString("yyMM");//居中对齐
//DrawString(e, date, GetPoint(0.9m + (1.1m - frm.GetFontWidth(printFont, date)) / 2 - decX, d - decY));
//string amount = printData.Rows[count]["股份数量"].ToString();//右对齐
//DrawString(e, amount, GetPoint(3.6m - frm.GetFontWidth(printFont, amount) - decX, d - decY));
//string bonus = printData.Rows[count]["每股分红"].ToString();
//DrawString(e, bonus, GetPoint(3.7m + (1.6m - frm.GetFontWidth(printFont, bonus)) / 2 - decX, d - decY));
//string total = printData.Rows[count]["分红总额"].ToString();
//DrawString(e, total, GetPoint(6.9m - frm.GetFontWidth(printFont, total) - decX, d - decY));
string tax = printData.Rows[count]["扣税"].ToString();
if (tax == "0")
{
DrawString(e, "", GetPoint(7.5m - decX, d - decY));
}
else
{
DrawString(e, tax, GetPoint(8.5m - frm.GetFontWidth(printFont, tax) - decX, d - decY + offset));
}

string aTax = printData.Rows[count]["税后分红"].ToString();
// DrawString(e, aTax, GetPoint(9.3m - frm.GetFontWidth(printFont, aTax) - decX, d - decY));
DrawString(e, aTax, GetPoint(9.3m - frm.GetFontWidth(printFont, aTax) - decX, d - decY + offset));
//DrawString(e, printData.Rows[count]["备注"].ToString(), GetPoint(10.35m - decX, d - decY));
count++;
cLine++;
}
if (printData.Rows.Count > cLine)
{
e.HasMorePages = true;
line = 1;
page++;
}
else
{
e.HasMorePages = false;
}
}
}
ai6541796 2010-10-20
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 ai6541796 的回复:]
引用 7 楼 freeboy827 的回复:
C# code

private void printdoc_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
while (count + line - 1 < 12 &amp;&amp; printData.Rows.Count……
[/Quote]

没用过这 东西

谁能给个源码吗
mark620 2010-10-20
  • 打赏
  • 举报
回复
用第三方插件试试 dExpress
ai6541796 2010-10-20
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 freeboy827 的回复:]
C# code

private void printdoc_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
while (count + line - 1 < 12 && printData.Rows.Count > cLine)
……
[/Quote]
我看了你给的 谢谢 我想请问您 我上面发的那生成的条码你知道那是什么条码吗?
ai6541796 2010-10-20
  • 打赏
  • 举报
回复

还有我想问下 各位 这下面这 生成的是什么条码 code39吗?

zf = zf.Replace("0", "_|_|__||_||_|");
zf = zf.Replace("1", "_||_|__|_|_||");
zf = zf.Replace("2", "_|_||__|_|_||");
zf = zf.Replace("3", "_||_||__|_|_|");
zf = zf.Replace("4", "_|_|__||_|_||");
zf = zf.Replace("5", "_||_|__||_|_|");
zf = zf.Replace("7", "_|_|__|_||_||");
zf = zf.Replace("6", "_|_||__||_|_|");
zf = zf.Replace("8", "_||_|__|_||_|");
zf = zf.Replace("9", "_|_||__|_||_|");
zf = zf.Replace("a", "_||_|_|__|_||");
zf = zf.Replace("b", "_|_||_|__|_||");
zf = zf.Replace("c", "_||_||_|__|_|");
zf = zf.Replace("d", "_|_|_||__|_||");
zf = zf.Replace("e", "_||_|_||__|_|");
zf = zf.Replace("f", "_|_||_||__|_|");
zf = zf.Replace("g", "_|_|_|__||_||");
zf = zf.Replace("h", "_||_|_|__||_|");
zf = zf.Replace("i", "_|_||_|__||_|");
zf = zf.Replace("j", "_|_|_||__||_|");
zf = zf.Replace("k", "_||_|_|_|__||");
zf = zf.Replace("l", "_|_||_|_|__||");
zf = zf.Replace("m", "_||_||_|_|__|");
zf = zf.Replace("n", "_|_|_||_|__||");
zf = zf.Replace("o", "_||_|_||_|__|");
zf = zf.Replace("p", "_|_||_||_|__|");
zf = zf.Replace("r", "_||_|_|_||__|");
zf = zf.Replace("q", "_|_|_|_||__||");
zf = zf.Replace("s", "_|_||_|_||__|");
zf = zf.Replace("t", "_|_|_||_||__|");
zf = zf.Replace("u", "_||__|_|_|_||");
zf = zf.Replace("v", "_|__||_|_|_||");
zf = zf.Replace("w", "_||__||_|_|_|");
zf = zf.Replace("x", "_|__|_||_|_||");
zf = zf.Replace("y", "_||__|_||_|_|");
zf = zf.Replace("z", "_|__||_||_|_|");
zf = zf.Replace("-", "_|__|_|_||_||");
zf = zf.Replace("*", "_|__|_||_||_|");
zf = zf.Replace("/", "_|__|__|_|__|");
zf = zf.Replace("%", "_|_|__|__|__|");
zf = zf.Replace("+", "_|__|_|__|__|");
zf = zf.Replace(".", "_||__|_|_||_|");
ai6541796 2010-10-20
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 freeboy827 的回复:]
C# code

private void printdoc_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
while (count + line - 1 < 12 && printData.Rows.Count > cLine)
……
[/Quote]

兄弟 你可不可以给一个完整的
CraxyMouse 2010-10-20
  • 打赏
  • 举报
回复
http://blog.csdn.net/dunao/archive/2009/02/05/3865053.aspx
freeboy827 2010-10-20
  • 打赏
  • 举报
回复

private void printdoc_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
while (count + line - 1 < 12 && printData.Rows.Count > cLine)
{
decimal d = 9.25m + Convert.ToDecimal((count + line) * 0.38);
if (page != 1)
{
if (page % 2 == 0)
{
d = 1.9m + Convert.ToDecimal((count + line) * 0.38);
}
else
{
d = 9.35m + Convert.ToDecimal((count + line) * 0.38);
}
}
printFont = new Font("Trebuchet MS", 7);
string date =Convert.ToDateTime(printData.Rows[count]["日期"]).ToString("yyMM");
DrawString(e, date, GetPoint(0.55m + (0.8m - frm.GetFontWidth(printFont, date)) / 2 - decX, d - decY));
string optionA = printData.Rows[count]["***"].ToString();//右对齐
DrawString(e, optionA, GetPoint(2.4m - frm.GetFontWidth(printFont, optionA) - decX, d - decY));
string optionL = printData.Rows[count]["***"].ToString();
DrawString(e, optionL, GetPoint(3.5m - frm.GetFontWidth(printFont, optionL) - decX, d - decY));
string gpA = printData.Rows[count]["***"].ToString();
DrawString(e, gpA, GetPoint(4.65m - frm.GetFontWidth(printFont, gpA) - decX, d - decY));
string stockP = printData.Rows[count]["***"].ToString();
DrawString(e, stockP, GetPoint(4.7m + (1.1m - frm.GetFontWidth(printFont, date)) / 2 - decX, d - decY));
string sAmount = printData.Rows[count]["***"].ToString();
DrawString(e, sAmount, GetPoint(6.9m - frm.GetFontWidth(printFont, sAmount) - decX, d - decY));
string gpL = printData.Rows[count]["***"].ToString();
DrawString(e, gpL, GetPoint(8.4m - frm.GetFontWidth(printFont, gpL) - decX, d - decY));
string optoinS = printData.Rows[count]["***"].ToString();
DrawString(e, optoinS, GetPoint(9.9m - frm.GetFontWidth(printFont, optoinS) - decX, d - decY));
printFont = new Font("宋体", 7);
DrawString(e, printData.Rows[count]["***"].ToString(), GetPoint(10.05m - decX, d - decY));

count++;
cLine++;
}
if (printData.Rows.Count > cLine)
{
e.HasMorePages = true;
line = 1;
page++;
}
else
{
e.HasMorePages = false;
}
}
yirenwang 2010-10-20
  • 打赏
  • 举报
回复
比较简单的方法,添加两个控件printDialog、printDocument
在PrintDocument.PrintPage设置要打印的内容
使用DialogResult获取打印机
DialogResult result = printDialog1.ShowDialog();
// 打印
if (result == DialogResult.OK)
{
printDocument1.Print();
}
ai6541796 2010-10-20
  • 打赏
  • 举报
回复
刚才问清楚了

不是让调用excel 打印

而是 用winfrom 直接打印

他给我的 只是一个样式而已

请问各位 该怎么做
l13873666736 2010-10-20
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 zhouzhangkui 的回复:]
“ 公司给了一个 excel的模板 和一张套打的图片”
你公司让你调用Excel去打印Excel?
[/Quote]


把要打印的内容往EXCEL里填
ai6541796 2010-10-20
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 zhouzhangkui 的回复:]
“ 公司给了一个 excel的模板 和一张套打的图片”
你公司让你调用Excel去打印Excel?
[/Quote]

我也不知道啊 因该不是吧

应该 是winfrom 程序直接打印成像excel 那种格式一样的
ai6541796 2010-10-20
  • 打赏
  • 举报
回复
我也不知道啊 因该不是吧

应该 是winfrom 程序直接打印成像excel 那种格式一样的
周药师 2010-10-20
  • 打赏
  • 举报
回复
“ 公司给了一个 excel的模板 和一张套打的图片”
你公司让你调用Excel去打印Excel?

110,534

社区成员

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

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

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