ASP.NET中的Excel操作,OLEDB方式

一: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);
           }