|
楼主 |
发表于 2015-12-25 18:49
|
显示全部楼层
完整代码(包括删除及新建Access数据代码)如下:
- string accessFilePath = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory) + @"\数据源\省市区三级联动.mdb";
- private void button2_Click(object sender, EventArgs e)
- {
- newAccess();
- object o = System.Runtime.InteropServices.Marshal.GetActiveObject("Excel.Application");
- Microsoft.Office.Interop.Excel._Application app = o as Microsoft.Office.Interop.Excel._Application;
- Microsoft.Office.Interop.Excel.Workbook srcbook = app.ActiveWorkbook;//得到当前活动的excel文档
- Microsoft.Office.Interop.Excel.Worksheet sh = (Microsoft.Office.Interop.Excel.Worksheet)srcbook.ActiveSheet;//得到当前Sheet
- try
- {
- OleDbConnectionStringBuilder connectStringBuilder = new OleDbConnectionStringBuilder();
- connectStringBuilder.DataSource = srcbook.FullName;
- connectStringBuilder.Provider = "Microsoft.Ace.OleDb.12.0";
- connectStringBuilder.Add("Extended Properties", "Excel 12.0");
- using (OleDbConnection cn = new OleDbConnection(connectStringBuilder.ConnectionString))
- {
- cn.Open();
- DataSet ds = new DataSet();
- string sql = "Select * from [" +sh.Name + "$]";
- OleDbCommand cmdLiming = new OleDbCommand(sql, cn);
- using (OleDbDataReader drLiming = cmdLiming.ExecuteReader())
- {
- ds.Load(drLiming, LoadOption.OverwriteChanges, new string[] { sh.Name });
- DataTable dt = ds.Tables[sh.Name];
- if (dt.Rows.Count > 0)
- {
- for (int i = 0; i < dt.Rows.Count; i++)
- {
- //写入数据库数据
- string MySql = "insert into VBA实现省市区三级联动(省,市,区) values('" + dt.Rows[i]["省"].ToString() + "','" + dt.Rows[i]["市"].ToString() + "','" + dt.Rows[i]["区"].ToString() + "')";
- OleDbConnection connct = new OleDbConnection();
- string oleDB = "Provider=Microsoft.Ace.OleDb.12.0;Data Source=" + accessFilePath;
- connct.ConnectionString = oleDB;
- //打开数据库
- connct.Open();
- OleDbCommand command = new OleDbCommand(MySql, connct);
- command.Connection = connct;
- int res = command.ExecuteNonQuery();
- // 关闭连接
- connct.Close();
- //return res;
- }
- MessageBox.Show("数据导入成功!");
- }
- else
- {
- MessageBox.Show("请检查你的Excel中是否存在数据");
- }
- }
- }
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.ToString());
- }
- }
- private void newAccess()
- {
- if (System.IO.File.Exists(accessFilePath) == true)
- {
- System.IO.File.Delete(accessFilePath);
- }
- //以上代码删除已存在的同路径同名的的;以下的代码为新建,需添加“Microsoft ADO Ext. 2.8 for DDL and Security”及“Microsoft ActiveX Data Objects 2.8 Library”两个引用
- ADOX.Catalog catalog = new Catalog();
- catalog.Create("Provider=Microsoft.Ace.OleDb.12.0;Data Source=" + accessFilePath + ";Jet OLEDB:Engine Type=5");
-
- ADODB.Connection cn = new ADODB.Connection();
- cn.Open("Provider=Microsoft.Ace.OleDb.12.0;Data Source=" + accessFilePath, null, null, -1);
- catalog.ActiveConnection = cn;
- ADOX.Table table = new ADOX.Table();
- table.Name = "VBA实现省市区三级联动";
- ADOX.Column column = new ADOX.Column();
- column.ParentCatalog = catalog;
- column.Name = "ID";
- column.Type = DataTypeEnum.adInteger;
- column.DefinedSize = 9;
- column.Properties["AutoIncrement"].Value = true;
- table.Columns.Append(column, DataTypeEnum.adInteger, 9);
- table.Keys.Append("FirstTablePrimaryKey", KeyTypeEnum.adKeyPrimary, column, null, null);
- table.Columns.Append("省", DataTypeEnum.adVarWChar, 50);
- table.Columns.Append("市", DataTypeEnum.adVarWChar, 50);
- table.Columns.Append("区", DataTypeEnum.adVarWChar, 50);
- catalog.Tables.Append(table);
- cn.Close();
- }
复制代码 |
|