首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > asp.net >

asp.net中怎么生成excel文件并打开在网页中

2012-11-18 
asp.net中如何生成excel文件并打开在网页中如题!谢谢![解决办法]分享网页专用导出类C# codeusing Systemu

asp.net中如何生成excel文件并打开在网页中
如题!谢谢!

[解决办法]
分享网页专用导出类

C# code
using System;using System.Collections;namespace AppFrame{    /// <summary>    /// 数据导出文件格式    /// </summary>    public class DataExport    {        private string szType = "";        private Hashtable conType = new Hashtable();        public DataExport()        {            conType["xls"] = "application/ms-excel";            conType["doc"] = "application/msword";            conType["pdf"] = "application/pdf";            conType["ppt"] = "application/ms-powerpoint";            conType["zip"] = "application/zip";            conType["rar"] = "application/rar";            conType["gif"] = "image/gif";            conType["jpg"] = "image/jpeg";            conType["png"] = "image/png";            conType["txt"] = "text/plain";            conType["htm"] = "text/html";            conType["xml"] = "text/xml";        }        /// <summary>        /// Excel文件格式        /// </summary>        public string xls { get { return "xls"; } }        /// <summary>        /// Word文件格式        /// </summary>        public string doc { get { return "doc"; } }        /// <summary>        /// PDF文件格式        /// </summary>        public string pdf { get { return "pdf"; } }        /// <summary>        /// PowerPoint文件格式        /// </summary>        public string ppt { get { return "ppt"; } }        /// <summary>        /// ZIP压缩文件格式        /// </summary>        public string zip { get { return "zip"; } }        /// <summary>        /// RAR压缩文件格式        /// </summary>        public string rar { get { return "rar"; } }        /// <summary>        /// GIF图形文件格式        /// </summary>        public string gif { get { return "gif"; } }        /// <summary>        /// JPG图形文件格式        /// </summary>        public string jpg { get { return "jpg"; } }        /// <summary>        /// PNG图形文件格式        /// </summary>        public string png { get { return "png"; } }        /// <summary>        /// TEXT文件格式        /// </summary>        public string txt { get { return "txt"; } }        /// <summary>        /// HTM网页超文本格式        /// </summary>        public string htm { get { return "htm"; } }        /// <summary>        /// XML文件格式        /// </summary>        public string xml { get { return "xml"; } }        /// <summary>        /// 数据导出文件类型        /// </summary>        public string FileType        {            set { szType = value; }            get { return szType; }        }        /// <summary>        /// 数据流字串索引        /// </summary>        /// <param name="TypeName">导出文件类型</param>        /// <returns>返回导出文件类型的数据流字串</returns>        public string this[string TypeName] { get { return conType[TypeName].ToString(); } }        /// <summary>        /// DataGrid数据导出到指定文件 如:Excel、Word        /// </summary>        /// <param name="objPage">Page对象</param>        /// <param name="objGrid">DataGrid对象</param>        public void Export2File(System.Web.UI.Page objPage, System.Web.UI.WebControls.DataGrid objGrid, DataExport de)        {            objPage.Response.Clear();            objPage.Response.Buffer = false;            objPage.Response.Charset = "GB2312";            objPage.Response.AppendHeader("Content-Disposition", string.Format("\"attachment;filename=Export.{0}\"", de.FileType));            objPage.Response.ContentType = de[de.FileType];            objPage.EnableViewState = false;            System.IO.StringWriter objStringWriter = new System.IO.StringWriter();            System.Web.UI.HtmlTextWriter objHtmlTextWriter = new System.Web.UI.HtmlTextWriter(objStringWriter);            objGrid.RenderControl(objHtmlTextWriter);            objPage.Response.Write(objStringWriter.ToString());            objPage.Response.End();        }    }} 

热点排行