ASP.NET中的Excel操作(OLEDB方式)

來源:互聯網
上載者:User

標籤:

一:OLEDB方式操作Excel的個人理解

        就是把要操作的Excel當作一個資料庫,所有對Excel的操作,就變成了對“資料庫”的操作。那麼這時就需要有一個資料庫的連接字串。

    代碼如下:

connStr = "Provider=Microsoft.ACE.OLEDB.12.0;" + "Data Source=" + strFileName + ";" + ";Extended Properties=\"Excel 12.0;HDR=YES\"“;

    其中的strFileName是指的Excel檔案名稱。

二:讀取Excel檔案,並將內容讀到DataTable中。

            代碼如下:

/// <summary>        /// 讀取Excel        /// </summary>        /// <param name="strFileName">Excel檔案名稱</param>        /// <param name="fileType"></param>        /// <param name="sheetName">Excel中的sheet的名字</param>        public static DataTable ExcelReader(string strFileName, string fileType, string sheetName)        {            string connStr = "";            DataTable _table;                connStr = "Provider=Microsoft.ACE.OLEDB.12.0;" + "Data Source=" + strFileName + ";" + ";Extended Properties=\"Excel 12.0;HDR=YES\"";            try            {                using (OleDbConnection conn = new OleDbConnection(connStr))                {                    conn.Open();                    OleDbDataAdapter myCommand = new OleDbDataAdapter(string.Format("SELECT * FROM [{0}$]", sheetName), conn);                    DataSet myDataSet = new DataSet();                    int i = myCommand.Fill(myDataSet);                    _table = myDataSet == null ? null : myDataSet.Tables.Count == 0 ? null : myDataSet.Tables[0];                }                return _table;            }            catch (Exception ex)            {                return null;            }        }

  返回的結果就是一個DataTable.
三:將DataTable儲存為Excel檔案

    代碼如下:

 

        /// <summary>        /// 從DataTable中讀取資料到excel中        /// </summary>        /// <param name="dt">傳入的DataTable</param>        public static void DTToExcel(DataTable dt)        {            string fileName = ((string.IsNullOrEmpty(dt.TableName)) ? "Excel" : dt.TableName) + ".xls";            HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + fileName);            HttpContext.Current.Response.Charset = "UTF-8";            HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.Default;            HttpContext.Current.Response.ContentType = "application/vnd.ms-excel";            GridView GridView1 = new GridView();            GridView1.DataSource = dt;            GridView1.DataBind();            StringWriter tw = new StringWriter();            HtmlTextWriter hw = new HtmlTextWriter(tw);            GridView1.RenderControl(hw);            HttpContext.Current.Response.Write(tw.ToString());            HttpContext.Current.Response.End();        }

 在使用DataTable匯出Excel檔案時,需要先建立DataTable,下面是DataTable的建立小執行個體:

 

            DataTable dt = new DataTable();            string[] strArr = new string[]            {                            "1",                            "2222",                            "333333333",                            "444"            };            for (int i = 0; i < strArr.Count(); i++)            {                dt.Columns.Add(strArr[i]);            }            for (int i = 0; i < strArr.Count(); i++)            {                DataRow dr2 = dt.NewRow();                dr2[0] = "總記";                dr2[1] = "11";                dr2[2] = "22";                dr2[3] = "33";                dt.Rows.Add(dr2);           }            

 

ASP.NET中的Excel操作(OLEDB方式)

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.