.net导入excel到数据库在前台页面如何导入excel中需要注意什么??

解决方案 »

  1.   

    using System;
    using System.Data;
    using System.Configuration;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.HtmlControls;
    using System.IO;/// <summary>
    /// ToExcel 的摘要说明
    /// </summary>
    public class ToExcel
    {
    public ToExcel()
    {
    //
    // TODO: 在此处添加构造函数逻辑
    //
    }    /// <summary>
        /// 将DataTable 导出为EXCEL,并直接提供下载
        /// </summary>
        /// <param name="ds">需要导处的DataTable</param>
        /// <param name="fileName">到处生成的文件名</param>
        /// 
        public bool ExportExcelByDataTable(DataTable dt, string fileName)
        {
            try
            {
                HttpContext.Current.Response.Clear();
                HttpContext.Current.Response.Charset = "utf-7";
                HttpContext.Current.Response.ContentEncoding=System.Text.Encoding.GetEncoding("utf-7");            HttpContext.Current.Response.ContentType = "application/vnd.ms-excel";
                StringWriter stringWrite = new StringWriter();
                HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
    DataGrid dg=new DataGrid();
    dg.HeaderStyle.CssClass="dgHead";
    dg.DataSource=dt;
    dg.DataBind();
    dg.RenderControl(htmlWrite);
    //            GridView gv = new GridView();
    //            gv.HeaderStyle.CssClass = "gvHead";
    //            gv.DataSource = dt;
    //            gv.DataBind();
    //            gv.RenderControl(htmlWrite);
                HttpContext.Current.Response.Write(stringWrite.ToString());
                HttpContext.Current.Response.AddHeader("content-disposition","attachment; filename=" + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8)+".xls");
                HttpContext.Current.Response.Charset = "gb2312";
                HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");
                HttpContext.Current.Response.End();
                return true;
            }
            catch
            {
                return false;
            }
        }
        /// <summary>
        /// --重载方法,将GRIDVIEW 导出为EXCEL,并直接提供下载
        /// </summary>
        /// <param name="ds">需要导处的DataTable</param>
        /// <param name="fileName">到处生成的文件名</param>
        /// 
        public bool ExportExcelByDataGrid(DataGrid dg, string fileName)
        {
            try
            {
                HttpContext.Current.Response.Clear();
                HttpContext.Current.Response.Charset = "gb2312";
                HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");            HttpContext.Current.Response.ContentType = "application/vnd.ms-excel";
                StringWriter stringWrite = new StringWriter();
                HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
                dg.RenderControl(htmlWrite);
                HttpContext.Current.Response.Write(stringWrite.ToString());
                HttpContext.Current.Response.AddHeader("content-disposition", "attachment; filename=" + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8) + ".xls");
                HttpContext.Current.Response.Charset = "gb2312";
                HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");
                HttpContext.Current.Response.End();
                return true;
            }
            catch
            {
                return false;
            }
        }
    }