日期:2014-05-17  浏览次数:20455 次

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";
            objPa