当前位置:软件学习 > Excel >>

C# 导出和导入excel

print?#region  导出Excel  
        /// <summary>  
        /// 导出Excel  
        /// </summary>  
        /// <param name="page">请求的页面this</param>  
        /// <param name="dataTable">导出的数据源</param>  
        /// <param name="fileName">保存文件名称</param>  
        /// <returns>布尔值</returns>  
        public bool ExportExcel(Page page, DataTable dataTable, string fileName) 
        { 
            try 
            { 
                HttpContext.Current.Response.Clear(); 
                HttpContext.Current.Response.Buffer = true; 
                HttpContext.Current.Response.ContentType = "application/vnd.ms-excel.numberformat:@"; 
                page.EnableViewState = false; 
                HttpContext.Current.Response.Charset = "UTF-8"; 
                HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");//设置输出流为简体中文  
                HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + fileName + ".xls"); 
                //输出列名  
                for (int i = 0; i < dataTable.Columns.Count; i++) 
                    HttpContext.Current.Response.Write(dataTable.Columns[i].ColumnName + "\t"); 
                HttpContext.Current.Response.Write("\r\n"); 
                //输出数据  
                for (int i = 0; i < dataTable.Rows.Count; i++) 
                { 
                    for (int j = 0; j < dataTable.Columns.Count; j++) 
                    { 
                        HttpContext.Current.Response.Write(dataTable.Rows[i][j].ToString() + "\t"); 
                    } 
                    HttpContext.Current.Response.Write("\r\n"); 
                } 
                //输出当前缓存内容  
                //HttpContext.Current.Response.Flush();  
                HttpContext.Current.Response.End(); 
                return true; 
            } 
            catch 
            { 
                return false; 
            } 
        } 
        #endregion 

#region  导出Excel
        /// <summary>
        /// 导出Excel
        /// </summary>
        /// <param name="page">请求的页面this</param>
        /// <param name="dataTable">导出的数据源</param>
        /// <param name="fileName">保存文件名称</param>
        /// <returns>布尔值</returns>
        public bool ExportExcel(Page page, DataTable dataTable, string fileName)
        {
            try
            {
                HttpContext.Current.Response.Clear();
                HttpContext.Current.Response.Buffer = true;
                HttpContext.Current.Response.ContentType = "application/vnd.ms-excel.numberformat:@";
                page.EnableViewState = false;
                HttpContext.Current.Response.Charset = "UTF-8";
                HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");//设置输出流为简体中文
                HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + fileName + ".xls");
                //输出列名
                for (int i = 0; i < dataTable.Columns.Count; i++)
        &nbs

补充:软件开发 , C# ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,