微信号:
当前位置:首页 > 资讯文摘 > 新闻资讯

网页内容保存或导出为word或excel

2015/10/28 10:30:51

        #region 网页内容保存或导出为word或excel
        /**//// <summary>
        /// 网页内容保存或导出为word或excel
        /// </summary>
        /// <param name="url">网页地址</param>
        /// <param name="num">0为导出word,1为导出excel</param>
        public static void SaveOrOutData( string url, int num )//导出数据的函数0为word,1为Excel
        {
            WebRequest req = WebRequest.Create( url );
            WebResponse resp = req.GetResponse();
            StreamReader sr = new StreamReader( resp.GetResponseStream(), System.Text.Encoding.UTF8 );
            string x = sr.ReadToEnd();
            System.Web.HttpContext.Current.Response.Clear();
            System.Web.HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding( "gb2312" );
            string fName = DateTime.Now.ToString( "yyyy-MM-dd-ss" );
            if( num == 0 )
            {
                fName = HttpUtility.UrlEncode( fName, System.Text.Encoding.GetEncoding( "gb2312" ) ) + ".doc";
                System.Web.HttpContext.Current.Response.ContentType = "application/ms-word";
            }
            else
            {
                fName = HttpUtility.UrlEncode( fName, System.Text.Encoding.GetEncoding( "gb2312" ) ) + ".xls";
                System.Web.HttpContext.Current.Response.ContentType = "application nd.xls";
            }
            System.Web.HttpContext.Current.Response.AddHeader( "content-disposition", "attachment;filename=" + fName );
            System.Web.HttpContext.Current.Response.Write( getBodyContent( x ) );//获取table标签
            System.Web.HttpContext.Current.Response.Flush();
            System.Web.HttpContext.Current.Response.End();
        }

相关新闻: