C# winform 编程 向ACCESS数据库导入EXCEL表使用心得

  1  public string MyConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=ErLake.mdb";
  2         /// <summary>
  3         /// 获取Excel文件
  4         /// </summary>
  5         /// <param name="sender"></param>
  6         /// <param name="e"></param>
  7 
  8         private void button1_Click(object sender, EventArgs e)
  9         {
 10 
 11 
 12             OpenFileDialog dlg = new OpenFileDialog();
 13             dlg.Filter = "Excel文件(*.xls)|*.xls";
 14             if (dlg.ShowDialog() == DialogResult.OK)
 15             {
 16                 string filePath = dlg.FileName;
 17                 this.textBox1.Text = filePath;
 18             }
 19 
 20 
 21         }
 22 
 23         private void button2_Click(object sender, EventArgs e)
 24         {
 25             if (textBox1.Text.Length == 0)
 26             {
 27                 MessageBox.Show("请选择导入数据的Execl文件");
 28             }
 29             else
 30             {
 31                 try
 32                 {
 33                     OleDbConnectionStringBuilder connectStringBuilder = new OleDbConnectionStringBuilder();
 34                     connectStringBuilder.DataSource = this.textBox1.Text.Trim();
 35                     connectStringBuilder.Provider = "Microsoft.Jet.OLEDB.4.0";
 36                     connectStringBuilder.Add("Extended Properties", "Excel 8.0");
 37                     using (OleDbConnection cn = new OleDbConnection(connectStringBuilder.ConnectionString))
 38                     {
 39                         DataSet ds = new DataSet();
 40                         string sql = "Select * from [Sheet1$]";
 41                         OleDbCommand cmdLiming = new OleDbCommand(sql, cn);
 42                         cn.Open();
 43                         using (OleDbDataReader drLiming = cmdLiming.ExecuteReader())
 44                         {
 45                             ds.Load(drLiming, LoadOption.OverwriteChanges, new string[] { "Sheet1" });
 46                             DataTable dt = ds.Tables["Sheet1"];
 47                             if (dt.Rows.Count > 0)
 48                             {
 49                                 for (int i = 0; i < dt.Rows.Count; i++)
 50                                 {
 51                                     //写入数据库数据
 52                                    // string MySql = "insert into 洱海各月出流流量 values('" + dt.Rows[i]["年"].ToString() + "','" + dt.Rows[i]["一月"].ToString()+ "','0','" + dt.Rows[i]["备注"].ToString() + "','0','" + i.ToString() + "')";
 53                                     string MySql = "insert into 洱海各月出流流量 values('" + dt.Rows[i][""].ToString() + "','" + 
 54                                         dt.Rows[i]["一月"].ToString() + "','" +
 55                                         dt.Rows[i]["二月"].ToString() + "','" +
 56                                         dt.Rows[i]["三月"].ToString() + "','" +
 57                                         dt.Rows[i]["四月"].ToString() + "','" + 
 58                                         dt.Rows[i]["五月"].ToString() + "','" + 
 59                                         dt.Rows[i]["六月"].ToString() + "','" + 
 60                                         dt.Rows[i]["七月"].ToString() + "','" + 
 61                                         dt.Rows[i]["八月"].ToString() + "','" +
 62                                         dt.Rows[i]["九月"].ToString() + "','" + 
 63                                         dt.Rows[i]["十月"].ToString() + "','" + 
 64                                         dt.Rows[i]["十一月"].ToString() + "','" + 
 65                                         dt.Rows[i]["十二月"].ToString() + "','" + 
 66                                         dt.Rows[i]["全年平均"].ToString() + "')";
 67                                     SQLExecute(MySql);
 68                                 }
 69                                 MessageBox.Show("数据导入成功!");
 70                             }
 71                             else
 72                             {
 73                                 MessageBox.Show("请检查你的Excel中是否存在数据");
 74                             }
 75                         }
 76                     }
 77                 }
 78                 catch (Exception ex)
 79                 {
 80                     MessageBox.Show(ex.ToString());
 81                 }
 82 
 83             }
 84         }
 85 
 86         /// <summary>
 87         /// 数据操作通用类
 88         /// </summary>
 89         /// <param name="sql"></param>
 90         /// <returns></returns>
 91         public bool SQLExecute(string sql)
 92         {
 93             try
 94             {
 95                 OleDbConnection conn = new OleDbConnection(MyConnectionString);
 96                 conn.Open();
 97                 OleDbCommand comm = new OleDbCommand();
 98                 comm.Connection = conn;
 99                 comm.CommandText = sql;
100                 comm.ExecuteNonQuery();
101                 comm.Connection.Close();
102                 conn.Close();
103                 return true;
104             }
105             catch
106             {
107                 return false;
108 
109             }
110         }
111     }
View Code

上面这段代码可以向ACCESS数据库表中导入EXCEL表,但是有个问题:导入数据后,查询数据表的数据发现新导入的数据出现在查询结果的前面,这不是我想要的,怎么办呢,我就采用字段升序排序的方式解决该问题!结果证明是有效的!

1        string MySQL = "Select * from 洱海各月出流流量 order by 年 asc";
View Code

 

转载于:https://www.cnblogs.com/yuhuameng/p/3659208.html

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值