博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
把execl导入到数据库中
阅读量:5280 次
发布时间:2019-06-14

本文共 2512 字,大约阅读时间需要 8 分钟。

代码如下:

     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;
        }

转载于:https://www.cnblogs.com/lx773984283/archive/2012/11/08/2760378.html

你可能感兴趣的文章
Magento开发文档(三):Magento控制器
查看>>
性能调优攻略
查看>>
ie6解决png图片透明问题
查看>>
瞬间的永恒
查看>>
2019-8-5 考试总结
查看>>
JS中实现字符串和数组的相互转化
查看>>
web service和ejb的区别
查看>>
Windows Azure Cloud Service (29) 在Windows Azure发送邮件(下)
查看>>
CS61A Efficiency 笔记
查看>>
微信上传素材返回 '{"errcode":41005,"errmsg":"media data missing"}',php5.6返回
查看>>
div或者p标签单行和多行超出显示省略号
查看>>
Elasticsearch 滚动重启 必读
查看>>
Hadoop基本概念
查看>>
java.util.zip压缩打包文件总结一:压缩文件及文件下面的文件夹
查看>>
浅说 apache setenvif_module模块
查看>>
MySQL--数据插入
查看>>
重新学习python系列(二)? WTF?
查看>>
shell脚本统计文件中单词的个数
查看>>
SPCE061A学习笔记
查看>>
sql 函数
查看>>