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

 

C# winform 编程 向ACCESS数据库导入EXCEL表使用心得,古老的榕树,5-wow.com

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。