动态生成文档

    技术2024-12-09  16

    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 bufferResponse.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;

        }

    最新回复(0)