資源簡介
文件為實現源碼,內容詳細,適合學習和解燃眉之急。
代碼片段和文件信息
//從excel導入到數據庫。
????????????OleDbConnection?conExcel?=?new?OleDbConnection();
????????????try
????????????{
????????????????OpenFileDialog?openFile?=?new?OpenFileDialog();//打開文件對話框。
????????????????openFile.Filter?=?(“Excel?文件(*.xls)|*.xls“);//后綴名。
????????????????if?(openFile.ShowDialog()?==?DialogResult.OK)
????????????????{
????????????????????string?filename?=?openFile.FileName;
????????????????????int?index?=?filename.LastIndexOf(“\\“);//截取文件的名字
????????????????????filename?=?filename.Substring(index?+?1);
????????????????????conExcel.ConnectionString?=?“Provider=Microsoft.Jet.Oledb.4.0;Data?Source=“?+?Application.StartupPath?+?“\\Appdata.mdb“;
??????????????????//將excel導入access
????????????????????//distinct?:刪除excel重復的行.
????????????????????//[excel名].[sheet名]?已有的excel的表要加$
????????????????????//where?not?in?:?插入不重復的記錄。
????????????????????string?sql?=?“insert?into?用戶表?select?distinct?*?from?[Excel?8.0;database=“?+?filename?+?“].[用戶表$]?where?記錄編號???not?IN?(select?記錄編號?from?用戶表)“;
????????????????????OleDbCommand?com?=?new?OleDbCommand(sql?conExcel);
????????????????????conExcel.Open();
????????????????????com.ExecuteNonQuery();
????????????????????MessageBox.Show(“導入數據成功““導入數據“?MessageBoxButtons.OK?MessageBoxIcon.Information?);
????????????????}
????????????}
????????????catch?(Exception?ex)
????????????{
????????????????MessageBox.Show(ex.ToString());
????????????}
????????????finally
????????????{
????????????????conExcel.Close();
????????????}
//導出到excel操作。
OleDbConnection?conExcel?=?new?OleDbConnection();
????????????try
?????
評論
共有 條評論