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)//通過構(gòu)造函數(shù)傳入客戶名和商品列表
{
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);//創(chuàng)建一個新的出貨單
foreach (string str in System.IO.File.ReadAllLines("../../print.html", Encoding.Default)) //讀取出貨單模板
{
string aline = str;
aline.Replace("&form["+i+"]$",list[i]);//將占位符替換為輸入的數(shù)據(jù)
i++;
byte[] data = System.Text.Encoding.Default.GetBytes(aline); //獲得字節(jié)數(shù)組
fs.Write(data, 0, data.Length); //開始寫入
fs.Flush(); //清空緩沖區(qū)
}
fs.Close();//關(guān)閉流
}
}
}
-----------------------------------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>