两年前大四 去实习前帮别人 做的一个东西
一个很老的 C/S架构的库管软件 数据库使用的是一个本地部署的sqlserver
总之要求就是sql查询拿到数据后,以拼贴字符串的形式 替换模板html中的占位符,这样一个html格式的单据就在本地出现了。
为什么要这么做?html浏览器打开直接就打印了 。转换成pdf也方便。
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace WindowsFormsApplication2
{
class PrintBill {
string name = null;
List<string> list;
public PrintBill(string name, List<string> list)//通过构造函数传入客户名和商品列表
{
this.name = name;
this.list = list;
}
public void ToPrint()
{
int i=0;
FileStream fs = new FileStream("../../出货单-" + name + "-" + DateTime.Now.ToString("yyyy年MM月dd日HH点mm分ss秒")+ ".html", FileMode.Create);//创建一个新的出货单
foreach (string str in System.IO.File.ReadAllLines("../../print.html", Encoding.Default)) //读取出货单模板
{
string aline = str;
aline.Replace("&form["+i+"]$",list[i]);//将占位符替换为输入的数据
i++;
byte[] data = System.Text.Encoding.Default.GetBytes(aline); //获得字节数组
fs.Write(data, 0, data.Length); //开始写入
fs.Flush(); //清空缓冲区
}
fs.Close();//关闭流
}
}
}
这是html部分 通过上面的循环操作把里面的的占位符 删掉就行了 自己注意 i 变量对应的是哪一项数据即可——下面是html部分
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>销售单</title>
</head>
<style type="text/css">
table.gridtable {
font-family: verdana,arial,sans-serif;
font-size:15px;
color:#333333;
border-width: 1px;
border-color: #666666;
border-collapse: collapse;
}
table.gridtable th {
border-width: 1px;
padding: 8px;
border-style: solid;
border-color: #666666;
background-color: #dedede;
}
table.gridtable td {
border-width: 1px;
padding: 8px;
border-style: solid;
border-color: #666666;
background-color: #ffffff;
}
</style>
<body>
<table width="912" height="588" border="1" bordercolor="#000000" class="gridtable">
<tbody>
<tr>
<th height="53" colspan="3" scope="col" ><strong style="font-size: 18px">商品出货单 </strong></th>
</tr>
<tr>
<th width="247" height="35" scope="col">客户名: $form[0]$</th>
<th width="266" height="35" scope="col">客户电话:$form[1]$</th>
<th width="287" height="35" scope="col">日期:$form[2]$</th>
</tr>
<tr>
<th colspan="3" scope="col"> $form[6]$</th>
</tr>
<tr>
<th colspan="3" scope="col"> $form[7]$</th>
</tr>
<tr>
<th colspan="3" scope="col"> $form[8]$</th>
</tr>
<tr>
<th colspan="3" scope="col"> $form[9]$</th>
</tr> <tr>
<th colspan="3" scope="col"> $form[10]$</th>
</tr> <tr>
<th colspan="3" scope="col"> $form[11]$</th>
</tr>
<tr>
<th colspan="3" scope="col"> $form[12]$</th>
</tr>
<tr>
<th colspan="3" scope="col"> $form[13]$</th>
</tr>
<tr>
<th height="3" colspan="3" scope="col"> $form[14]$</th>
</tr>
<tr>
<th colspan="3" scope="col"> $form[15]$</th>
</tr>
<tr>
<th height="39" scope="col">总计:$form[3]$</th>
<th colspan="2" align="left" scope="col">备注:$form[4]$</th>
</tr>
<tr>
<th height="70" colspan="2" align="left" scope="col"> </th>
<th height="70" align="left" scope="col"> 客户签字:$form[5]$</th>
</tr>
</tbody>
</table>
</body>
</html>