把execl导入到数据库中

代码如下:

     private void btnChange_Click(object sender, EventArgs e)
        {
            if (txtFile.Text != "")
            {
                if (textBox1.Text != "")
                {
                    string strname = textBox1.Text;
                    string filePath = txtFile.Text;
                    DataSet ds = LoadDataFromExcel(filePath, strname);
                    bool isOk = ExcelToArray(ds.Tables[0]);
                    if (isOk)
                    {
                        MessageBox.Show("修改成功");
                    }
                }
                else
                {
                    MessageBox.Show("请选择 Excel的表名 ,亲");
                }
            }
            else
            {
                MessageBox.Show("请选择 Excel ,亲");
            }
        }

#region 加载 excel
        //加载 excel
        public static DataSet LoadDataFromExcel(string filePath,string strname)
        {
            try
            {
                string strConn;
                strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filePath + ";Extended Properties='Excel 8.0;HDR=YES;IMEX=1'";
                OleDbConnection OleConn = new OleDbConnection(strConn);
                OleConn.Open();
                String sql = "SELECT * FROM  [Sheet2$]";// +strname;//可是更改Sheet名称,比如sheet2,等等    

                OleDbDataAdapter OleDaExcel = new OleDbDataAdapter(sql, OleConn);
                DataSet OleDsExcle = new DataSet();
                //OleDaExcel.Fill(OleDsExcle, "Sheet1");
                OleDaExcel.Fill(OleDsExcle);

                OleConn.Close();
                return OleDsExcle;
            }
            catch (Exception err)
            {
                MessageBox.Show("数据绑定Excel失败!失败原因:" + err.Message, "提示信息",
                    MessageBoxButtons.OK, MessageBoxIcon.Information);

                return null;
            }
        }
        #endregion

        #region 将 读取出的 excel 列保存 自定义数组 或字符串
        public static bool ExcelToArray(System.Data.DataTable excelTable)
        {
            Microsoft.Office.Interop.Excel.Application app =
                new Microsoft.Office.Interop.Excel.Application();
            try
            {
                List<string> listStr = new List<string>();
                int rowCount = excelTable.Rows.Count;
                int colCount = excelTable.Columns.Count;
                if (rowCount > 0)
                {
                    Dictionary<string, string> dics = new Dictionary<string, string>();
                    for (int i = 0; i < rowCount; i++)
                    {
                        string ReportDate = excelTable.Rows[i][0].ToString(); dics.Add("ProductID", ReportDate);
                        string EnabledState = excelTable.Rows[i][1].ToString(); dics.Add("Title", EnabledState);
                        try
                        {
                            UpdatePro(dics);
                            dics.Clear();
                        }
                        catch
                        {
                        }
                    }
                }


                return true;
            }
            catch (Exception err)
            {
                MessageBox.Show("导出Excel出错!错误原因:" + err.Message, "提示信息",
                    MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
            finally
            {

            }
        }

        #endregion

        public static bool UpdatePro(Dictionary<string,string> dics)
        {
            string sql = @" INSERT dbo.tb_linshi
         (
           ProductID,
           Title
         )
 VALUES  (
          @ProductID ,
          @Title
         )";

            SqlParameter[] param = new SqlParameter[]{
              new SqlParameter("@ProductID",dics["ProductID"]),
              new SqlParameter("@Title",dics["Title"])
            };

            bool isOk = false;
            if (SqlHelper.ExecuteNonQuery(sql, param) > 0)
            {
                isOk = true;
            }
            return isOk;
        }

posted @ 2012-11-08 14:43  待重逢  阅读(224)  评论(0)    收藏  举报