欢迎加入站点基地设计开发交流群:30720409
详细内容
.net导出word文件的方法
发布日期:2010-06-29     点击:3134     字体:[ ]

这里介绍.net利用xsl文件定义模板,根据xsl文件定义的样式,导出word文件的方法

1。导出文件页WRexport.aspx.cs代码如下(我这里继承了自己定义的BasePage,引用时可直接引用System.Web.UI.Page):

public partial class WRexport : BasePage
    {
        //周报管理
        WeekReportController wrc = new WeekReportController();

        //现场记录管理
        WeekReportSceneController sc = new WeekReportSceneController();

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (!this.IsPostBack)
                {
                    //这里是添加临时文件权限,不然有可能导出的文件无法直接打开
                    HttpContext.Current.Response.ClearHeaders();
                    HttpContext.Current.Response.AppendHeader("Expires ", System.DateTime.Now.AddMinutes(30).ToString());
                    HttpContext.Current.Response.AppendHeader("Pragma ", "public ");
                    HttpContext.Current.Response.AppendHeader("Cache-Control ", "must-revalidate,   post-check=0,   pre-check=0 ");
                    HttpContext.Current.Response.AppendHeader("Cache-Control ", "public ");

                    string strProjectGuid = "";
                    string strWeekReportGuid = "";

                    //这里是获取参数
                    if (this.Request.QueryString["ProjectGuid"] != null)
                    {
                        strProjectGuid = this.Request.QueryString["ProjectGuid"].ToString();
                    }

                    if (this.Request.QueryString["WeekReportGuid"] != null)
                    {
                        strWeekReportGuid = this.Request.QueryString["WeekReportGuid"].ToString();
                    }

                    //执行导出操作
                    ExportWeekReportXml(strProjectGuid, strWeekReportGuid);
                }
            }
            catch
            { Response.Write("错误发生。"); }
        }//end Page_Load

        //导出数据
        public void ExportWeekReportXml(string strProjectGuid, string strWeekReportGuid)
        {
            try
            {
                XmlDocument pXmlDoc = GetWeekReportXml11(strProjectGuid, strWeekReportGuid);

                //pXmlDoc.Load(Server.MapPath("ExportWeekReport.xml"));

                MSXML2.DOMDocument pXmlDocShow = new DOMDocumentClass();
                pXmlDocShow.async = false;
                pXmlDocShow.loadXML(pXmlDoc.InnerXml);

                XmlDocument pXslDoc = new XmlDocument();
                pXslDoc.Load(Server.MapPath("ExportWeekReport.xsl"));

                MSXML2.DOMDocument pXslDocShow = new DOMDocumentClass();
                pXslDocShow.async = false;
                pXslDocShow.loadXML(pXslDoc.InnerXml);


                string strTemp = pXmlDocShow.transformNode(pXslDocShow);
                int strTempBegin = strTemp.IndexOf("<?xml");
                int strTempEnd = strTemp.LastIndexOf("?>");
                if (strTempEnd > -1)
                {
                    strTemp = strTemp.Remove(strTempBegin, strTempEnd - strTempBegin + 2);
                }
                strTemp = @"<?xml versio

用户评论
昵称 
内容  *
验证码   
   
Copyright © 2010 zdbase.com All Rights Reserved. 苏ICP备15039389号-1 可人软件设计