动态生成文档

1.实例化xml数据

static string ExportTemplate
    {
        get
        {
            if (__ExportTemplate == null)
            {
                string path = HttpContext.Current.Server.MapPath(Path.Combine("doc", "export.xml"));
                using (StreamReader sr = new StreamReader(path))
                {
                    __ExportTemplate = sr.ReadToEnd();
                    sr.Close();
                    sr.Dispose();
                }
            }
            return __ExportTemplate;
        }
    }

private static string __ExportTemplate = null;

2.写入xml数据方法

String toWriteTxt = ;//写入word的字符串

 // clear the current output content from the buffer
Response.Clear();
Response.AddHeader("Content-Disposition", "attachment; filename=/"" + FileName + ".doc/""); 

 // 根据实际下载FileName修改,最好全部是英文
Response.ContentType = "application/octet-stream";
Response.Write(toWriteTxt);

Response.End();

 

3.动态加载

private String Export(String id)
    {
        /* 注释 */
        View_Export cf = new View_Export();//实例化的对象
        cf.Load(string.Format("ID='{0}'", new Guid(id)));

        //特殊字符处理
        if(!string.IsNullOrEmpty(cf.ChineseName))
        {
        cf.ChineseName = cf.ChineseName.Replace("&", "&");
        }
        if (!string.IsNullOrEmpty(cf.EnglishName))
        {
            cf.EnglishName = cf.EnglishName.Replace("&", "&");
        }
         return toWriteTxt;

    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值