C# 拼接字符串方式生成html文件 winform程序生成html表单

        两年前大四 去实习前帮别人 做的一个东西

        一个很老的 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">&nbsp;$form[6]$</th>
    </tr>
    <tr>
      <th colspan="3" scope="col">&nbsp;$form[7]$</th>
    </tr>
       <tr>
      <th colspan="3" scope="col">&nbsp;$form[8]$</th>
    </tr>
       <tr>
      <th colspan="3" scope="col">&nbsp;$form[9]$</th>
    </tr> <tr>
      <th colspan="3" scope="col">&nbsp;$form[10]$</th>
    </tr> <tr>
      <th colspan="3" scope="col">&nbsp;$form[11]$</th>
    </tr>
       <tr>
      <th colspan="3" scope="col">&nbsp;$form[12]$</th>
    </tr>
       <tr>
      <th colspan="3" scope="col">&nbsp;$form[13]$</th>
    </tr>
       <tr>
      <th height="3" colspan="3" scope="col">&nbsp;$form[14]$</th>
    </tr>
       <tr>
      <th colspan="3" scope="col">&nbsp;$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">&nbsp;</th>
      <th height="70" align="left" scope="col">&nbsp; 客户签字:$form[5]$</th>
    </tr>
  </tbody>
</table>
</body>
</html>

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值