EXCEL数据导入数据库的另一种方式

一、直接代码

 1        var fileResult = openFileDialog1.ShowDialog();
 2             if (fileResult == System.Windows.Forms.DialogResult.OK)
 3             {
 4                 var fileName = openFileDialog1.FileName;
 7                 OleDbConnection conn = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + fileName + ";Extended Properties=\"Excel 12.0             Xml;HDR=YES;\"");
 8                 OleDbDataAdapter da = new OleDbDataAdapter("select * from [Sheet1$]", conn);
 9                 da.Fill(dataTable);
10                 dataGridView1.AutoGenerateColumns = true;
11                 dataGridView1.DataSource = dataTable.DefaultView;
12                 progressBar1.Maximum = dataTable.Rows.Count;
13                 label1.Text = dataTable.Rows.Count.ToString();
14             }

 

 1 foreach (DataRow dataRow in dataTable.Rows)
 2             {
 3                 index++;
 4                 if (dataRow[1].ToString() == null && dataRow[1].ToString().Trim() == "")
 5                 {
 6                     continue;
 7                 }
20                 User user= new User();
21                 //省略代码
84                 context.ArchivesBases.Add(archivesBase);87                 if (index >= 100 && index % 100 == 0)
88                 {
89                     context.SaveChanges();
90                     //释放持久层已经存在数据
91                     GC.Collect();//垃圾回收器
92                     context = new DAL.TodaySoftHRAMSContext();
93                 }
94             }

 

 

posted @ 2013-11-12 16:01  情若天_RunUp  阅读(262)  评论(0编辑  收藏  举报