winfor应用程序打印报表清单
最近一周竟然有2位以前的同事问我在winfor应用程序里面打印怎么搞,所以才有了写这篇文章的打算,索性现在没事就写出来
在窗体上简单的布局设置一下如图
定义一个Model 我在里面放了属性之外还从写了ToString方法和返回模型的方法。
代码如下
#region Model
public DateTime CreateDate = DateTime.Today;
public string DocNumber;
public string Title { get; set; }
public string Consignee { get; set; }
public string ProductName { get; set; }
public string ProductLot { get; set; }
public string Specification { get; set; }
public string Factory { get; set; }
public string RegNo { get; set; }
public string PermitNo { get; set; }
public DateTime ExpirationDate { get; set; }
public DateTime DeliveryDate { get; set; }
public string Unit { get; set; }
public int Count { get; set; }
public float Price { get; set; }
public float Money { get; set; }
#endregion private const char Spliter = '|';//分割txt里面的值 public override string ToString()
{
StringBuilder sb = new StringBuilder();
sb.Append(CreateDate);
sb.Append(Spliter);
sb.Append(DocNumber);
sb.Append(Spliter);
sb.Append(Title);
sb.Append(Spliter);
sb.Append(Consignee);
sb.Append(Spliter);
sb.Append(ProductName);
sb.Append(Spliter);
sb.Append(ProductLot);
sb.Append(Spliter);
sb.Append(Specification);
sb.Append(Spliter);
sb.Append(Factory);
sb.Append(Spliter);
sb.Append(RegNo);
sb.Append(Spliter);
sb.Append(PermitNo);
sb.Append(Spliter);
sb.Append(ExpirationDate);
sb.Append(Spliter);
sb.Append(DeliveryDate);
sb.Append(Spliter);
sb.Append(Unit);
sb.Append(Spliter);
sb.Append(Count);
sb.Append(Spliter);
sb.Append(Price);
sb.Append(Spliter);
sb.Append(Money);
sb.Append(Spliter);
return sb.ToString();
} public static OutshipDoc FromString(string s)
{
try
{
int i = ; OutshipDoc doc = new OutshipDoc();
string[] pars = s.Split(new char[] { Spliter });
doc.CreateDate = DateTime.Parse(pars[i++]);
doc.DocNumber = pars[i++];
doc.Title = pars[i++];
doc.Consignee = pars[i++];
doc.ProductName = pars[i++];
doc.ProductLot = pars[i++];
doc.Specification = pars[i++];
doc.Factory = pars[i++];
doc.RegNo = pars[i++];
doc.PermitNo = pars[i++];
doc.ExpirationDate = DateTime.Parse(pars[i++]);
doc.DeliveryDate = DateTime.Parse(pars[i++]);
doc.Unit = pars[i++];
doc.Count = int.Parse(pars[i++]);
doc.Price = float.Parse(pars[i++]);
doc.Money = float.Parse(pars[i++]);
return doc;
}
catch (System.Exception ex)
{
return null;
}
}
Model里面的东西已经准备完了,现在就开始前台的代码了
OutshipDoc doc; internal OutshipDoc Doc
{
get { return doc; }
set { doc = value; }
}
以上是model,接下来就是定义公用的
PrintDocument
private System.Drawing.Printing.PrintDocument docToPrint =
new System.Drawing.Printing.PrintDocument();//创建一个PrintDocument的实例
定义好了就用构造函数初始化Model
public PrintPreview()
{
InitializeComponent();
doc = new OutshipDoc();
doc.Title = "广州市xx医疗器械有限公司送货清单";
doc.Consignee = "阳江人民医院";
doc.ProductName = "银尔通活性银离子抗菌液III型";
doc.DocNumber = "X2012040201";
doc.DeliveryDate = DateTime.Parse("2012/4/2");
doc.Factory = "西安康旺抗菌科技股份有限公司";
doc.Specification = "1200ml/盒";
doc.RegNo = "陕食药监械(准)字2008第2640064号";
doc.PermitNo = "陕食药监械生产许20052286号";
doc.ProductLot = "";
doc.ExpirationDate = DateTime.Parse("2014/04/01");
doc.Unit = "盒";
doc.Count = ;
doc.Price = 39.00F;
doc.Money = 37441.18F; docToPrint.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(docToPrint_PrintPage);
}
//画打印格式
void docToPrint_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
int margin = ;
int width = ;
int height = e.PageBounds.Height;
int left = (e.PageBounds.Width - width) / ;
int top = ;
renderDocument(doc, e.Graphics, left, top, width, height); }
public static void renderDocument(OutshipDoc d, Graphics g, int left, int top, int width, int height)
{
width = ; Brush br = Brushes.Black; // 打印抬头
Font ft;
StringFormat sf; ft = new Font("宋体", );
sf = new StringFormat();
sf.Alignment = StringAlignment.Center;
sf.FormatFlags = StringFormatFlags.NoClip; g.DrawString(d.Title, ft, br
, new RectangleF(new PointF(left, top), new SizeF(width , height ))
, sf); string info1 = "收货单位:" + d.Consignee + " " + "品名:" + d.ProductName;
ft.Dispose();
ft = new Font("宋体", );
g.DrawString(info1, ft, br, new PointF(left, top + )); info1 = "编号:" + d.DocNumber;
g.DrawString(info1, ft, br, new PointF(left + width - , top + )); ft.Dispose();
ft = new Font("宋体", );
info1 = "生产厂家:" + d.Factory;
g.DrawString(info1, ft, br, new PointF(left, top + ));
info1 = "送货日期:" + d.DeliveryDate.Year + "年" + d.DeliveryDate.Month + "月" + d.DeliveryDate.Day + "日";
g.DrawString(info1, ft, br, new PointF(left + width - , top + )); int x = left, y = top + ; Pen pn = Pens.Black;
g.DrawLine(pn, new Point(left, y), new Point(left + width, y));
y += ;
g.DrawLine(pn, new Point(left, y), new Point(left + width, y));
y += ;
g.DrawLine(pn, new Point(left, y), new Point(left + , y));
g.DrawLine(pn, new Point(left + + , y), new Point(left + width, y));
y += ;
g.DrawLine(pn, new Point(left, y), new Point(left + width, y));
y += ;
g.DrawLine(pn, new Point(left, y), new Point(left + width, y)); int y1 = top + ;
int y2 = top + ;
int y3 = top + ;
g.DrawLine(pn, new Point(x, y1), new Point(left, y3));
x += ;
g.DrawLine(pn, new Point(x, y1), new Point(x, y3));
x += ;
g.DrawLine(pn, new Point(x, y1), new Point(x, y2));
x += ;
g.DrawLine(pn, new Point(x, y1), new Point(x, y2));
x += ;
g.DrawLine(pn, new Point(x, y1), new Point(x, y2));
x += ;
g.DrawLine(pn, new Point(x, y1), new Point(x, y2));
x += ;
g.DrawLine(pn, new Point(x, y1), new Point(x, y2));
x += ;
g.DrawLine(pn, new Point(x, y1), new Point(x, y2));
x += ;
g.DrawLine(pn, new Point(x, y1), new Point(x, y3));
x = left + width;
g.DrawLine(pn, new Point(x, y1), new Point(x, y3)); sf.Alignment = StringAlignment.Center;
sf.LineAlignment = StringAlignment.Center; ft.Dispose();
ft = new Font("宋体", ); x = left;
g.DrawString("规格", ft, br, new RectangleF(x, y1, , ), sf);
x += ;
g.DrawString("产品注册证号", ft, br, new RectangleF(x, y1, , ), sf);
x += ;
g.DrawString("医疗器械生产企业许可证号", ft, br, new RectangleF(x, y1, , ), sf);
x += ;
g.DrawString("产品批号", ft, br, new RectangleF(x, y1, , ), sf);
x += ;
g.DrawString("失效日期", ft, br, new RectangleF(x, y1, , ), sf);
x += ;
g.DrawString("单位", ft, br, new RectangleF(x, y1, , ), sf);
x += ;
g.DrawString("数量", ft, br, new RectangleF(x, y1, , ), sf);
x += ;
g.DrawString("单价", ft, br, new RectangleF(x, y1, , ), sf);
x += ;
g.DrawString("金额", ft, br, new RectangleF(x, y1, , ), sf);
x += ; x = left;
g.DrawString(d.Specification, ft, br, new RectangleF(x, y1 + , , ), sf);
x += ;
g.DrawString(d.RegNo, ft, br, new RectangleF(x, y1 + , , ), sf);
x += ;
g.DrawString(d.PermitNo, ft, br, new RectangleF(x, y1 + , , ), sf);
x += ;
g.DrawString(d.ProductLot, ft, br, new RectangleF(x, y1 + , , ), sf);
x += ;
g.DrawString(d.ExpirationDate.ToString("yyyyMMdd"), ft, br, new RectangleF(x, y1 + , , ), sf);
x += ;
g.DrawString(d.Unit, ft, br, new RectangleF(x, y1 + , , ), sf);
x += ;
g.DrawString(d.Count.ToString(), ft, br, new RectangleF(x, y1 + , , ), sf);
x += ;
g.DrawString(d.Price.ToString("#.00"), ft, br, new RectangleF(x, y1 + , , ), sf);
x += ;
g.DrawString(d.Money.ToString("#.00"), ft, br, new RectangleF(x, y1 + , , ), sf);
x += ; x = left;
sf.Alignment = StringAlignment.Near;
g.DrawString("合计:", ft, br, new RectangleF(x, y2, , ), sf);
x += ;
g.DrawString("人民币(大写)" + moneyToString(d.Money), ft, br, new RectangleF(x, y2, , ), sf);
x += ;
x += ;
x += ;
x += ;
x += ;
x += ;
x += ;
sf.Alignment = StringAlignment.Center;
g.DrawString(d.Money.ToString("#.00"), ft, br, new RectangleF(x, y2, , ), sf);
x += ; x = left;
sf.Alignment = StringAlignment.Near;
g.DrawString("收货单位:", ft, br, new RectangleF(x, y3, , ), sf);
x += ;
g.DrawString("送货单位:", ft, br, new RectangleF(x, y3, , ), sf); }
//货币大写转换
private static string moneyToString(double p)
{
string[] numbers = new string[] { "零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "镹"};
string[] unit = new string[] { "元", "拾", "佰", "仟", "万", "拾万", "佰万", "仟万" };
string[] xunit = new string[] { "角", "分" }; int x = (int)p; string s = "";
int i = ;
for (i = ; i < && x > ; i++)
{
if (x % != )
s = numbers[x % ] + unit[i] + s;
x /= ;
}
if (s == "")
s = "零";
if (!s.EndsWith("元"))
s += "元"; double y = p - (int)p;
if (y != 0.0f)
{
y *= ;
if ((int)y != )
s += numbers[(int)y] + "角";
y = y - (int)y;
y *= ;
if ((int)y != )
s += numbers[(int)y] + "分";
} if (s.EndsWith("元"))
s += "整";
return s;
}
输出的格式浏览就已经写完最好就差真正的打印了
private void btnPrint_Click(object sender, EventArgs e)
{
PrintDialog pd = new PrintDialog();
pd.Document = docToPrint;
if (pd.ShowDialog() == DialogResult.OK)
{
docToPrint.Print();
}
}
到此就已经写完了看看效果了,如图下面就是最后的效果
winfor应用程序打印报表清单的更多相关文章
- C# WPF打印报表
前天我的一个同学由于打印报表而苦恼,所以就介绍了一下WPF的打印报表,希望能帮助到大家. 展示报表 1. 首先新建项“报表”,选定项目,右击,点击“添加”->“新建项”->“报表”
- .Net_用控制台程序打印指定行数的三角型(面试题)
.Net_用控制台程序打印指定行数的三角型(面试题) 下面是一个由*号组成的4行倒三角形图案.要求: 1.输入倒三角形的行数,行数的取值3-21之间,对于非法的行数,要求抛出提示“非法行数!”: ...
- 如何在C/S下打印报表
java应用有不少是C/S模式,在C/S模式下,同样可以调用API接口运算报表.CSReport是C/S模式下的报表控件类,在这个类中可以获得报表的显示面板.获得报表的打印面板.显示报表打印窗口 ...
- 代码实现从键盘接收一个字符串, 程序对其中所有字符进行排序,例如键盘输入: helloitcast程序打印:acehillostt
package com.loaderman.test; import java.util.Comparator; import java.util.Scanner; import java.util. ...
- 最全的WEB前端开发程序员学习清单
史上最全的WEB前端开发程序员学习清单! 今天为什么要给大家分享这篇文章呢,我发现最近来学前端的特别多,群里面整天都有人问:前端好找工作吗?前端要怎么学啊?前端工资怎么样?前端XX,前端XXX,虽然我 ...
- java例题_47 读取 7 个数(1—50)的整数值,每读取一个值,程序打印出该值个数的*
1 /*47 [程序 47 打印星号] 2 题目:读取 7 个数(1-50)的整数值,每读取一个值,程序打印出该值个数的*. 3 */ 4 5 /*分析 6 * 1.多次读取---for循环 7 * ...
- IIS下打印报表到Excel
阅读本文之前,请先看上一篇文章<.NET下Excel报表的打印>. 上一篇文章<.NET下Excel报表的打印>介绍了关于报表打印到Excel文件中的方法.若要把项目通过IIS ...
- 使用功能强大的插件FastReport.Net打印报表实例
我第一次使用FastReport插件做的功能是打印一个十分复杂的excel表格,有几百个字段都需要绑定数据,至少需要4个数据源,而且用到横向.竖向合并单元格. 我不是直接连接数据库,而是使用Regis ...
- 12、借助Jacob实现Java打印报表(Excel、Word)
12.使用Jacob来处理文档 Word或Excel程序是以一种COM组件形式存在的.如果能够在Java中调用相应组件,便能使用它的方法来获取文档中的文本信息.Jacob是一个JAVA到微软的COM接 ...
随机推荐
- Centos 配置 Mysql 主从双向同步
配置之前,请先阅读mysql主从复制: Mysql-主从复制 原: 主从环境: 主服务器:192.168.153.130 从服务器:192.168.153.131 1.从数据库创建同步用户,将主数据 ...
- Java | 技术应用 | 利用Jsoup处理页面
根据微信公众号的推文链接地址,对文章内容进行爬取,利用jsoup解析文章源代码,加上结合xpth提取文文章信息, 利用正则表达式读取文章发表时间. Jsoup <!-- jsoup HTML p ...
- bzoj1024 [SCOI2009]生日快乐【dfs】
传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=1024 普通的深搜. #include <cstdio> #include < ...
- C++类成员函数与成员变量的内存布局
一.成员函数 成员函数可以被看作是类作用域的全局函数,不在对象分配的空间里,只有虚函数才会在类对象里有一个指针,存放虚函数的地址等相关信息.
- WebForm中 页面传参的总结
页面与后台的数据传递是实现动态页面的前提---数据交互.无论是MVC还是WebFrom 都需要详细了解各种前后台的数据传输方式,熟悉每种方式的优缺点,这样才能提高网站的性能,技术上得到锻炼. 1.Fo ...
- html5新增的主题结构元素
article元素 article元素代表文档.页面或应用程序中独立的.完整的.可以独自被外部引用的内容. 它可以是一篇博客或者报刊中的文章,一篇论坛帖子.一段用户评论或独立的插件. 或其他任何独立的 ...
- mySQL 从删库到跑路
问题: 使用python实现load data infile ...向mySQL中导入数据.虽然成功执行但是数据库中没增加记录. 解决: zz的我execute之后没有commit.
- CF781A Andryusha and Colored Balloons
题意: Andryusha goes through a park each day. The squares and paths between them look boring to Andryu ...
- TigerGraph REST++API
简介 - 什么是REST ++? TigerGraph TM 系统使用着名的REpresentational State Transfer(REST)架构来管理与TigerGraph核心组件,图形处理 ...
- rabbitmq的知识点
rabbitmq,分为集群和主从2种. 主从式与集群式的速度差10倍. 每个rabittmq组需要3台机器. 集群式,稳定性高,主从式,速度快. 可以做任务分配,单点锁(二进制树实现). 只有当消息和 ...