欢迎访问 生活随笔!

尊龙游戏旗舰厅官网

当前位置: 尊龙游戏旗舰厅官网 > 编程语言 > c# >内容正文

c#

c#实现access导入导出excel -尊龙游戏旗舰厅官网

发布时间:2025/1/21 c# 20 豆豆
尊龙游戏旗舰厅官网 收集整理的这篇文章主要介绍了 c#实现access导入导出excel 小编觉得挺不错的,现在分享给大家,帮大家做个参考.
一、access从excel中导入数据 1.用到的excel表的格式及内容实现[c-sharp] view plaincopyprint?oledbconnection con = 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 users2(用户编号,用户姓名) select distinct * from [excel 8.0;database=" filename "].[name$] where 用户编号 not in (select 用户编号 from users2) "; oledbcommand com = new oledbcommand(sql, con); con.open(); com.executenonquery(); messagebox.show("导入数据成功", "导入数据", messageboxbuttons.ok, messageboxicon.information); } } catch (exception ex) { messagebox.show(ex.tostring()); } finally { con.close(); } 二、access导出excel [c-sharp] view plaincopyprint?oledbconnection con = new oledbconnection(); try { savefiledialog savefile = new savefiledialog(); savefile.filter = ("excel 文件(*.xls)|*.xls");//指定文件后缀名为excel 文件。 if (savefile.showdialog() == dialogresult.ok) { string filename = savefile.filename; if (system.io.file.exists(filename)) { system.io.file.delete(filename);//如果文件存在删除文件。 } int index = filename.lastindexof("//");//获取最后一个/的索引 filename = filename.substring(index 1);//获取excel名称(新建表的路径相对于savefiledialog的路径) //select * into 建立 新的表。 //[[excel 8.0;database= excel名].[sheet名] 如果是新建sheet表不能加$,如果向sheet里插入数据要加$.  //sheet最多存储65535条数据。 string sql = "select top 65535 * into [excel 8.0;database=" filename "].[用户信息] from users2"; con.connectionstring = "provider=microsoft.jet.oledb.4.0;data source=" application.startuppath "//appdata.mdb";//将数据库放到debug目录下。 oledbcommand com = new oledbcommand(sql, con); con.open(); com.executenonquery(); messagebox.show("导出数据成功", "导出数据", messageboxbuttons.ok, messageboxicon.information); } } catch (exception ex) { messagebox.show(ex.tostring()); } finally { con.close(); }

 

总结

以上是尊龙游戏旗舰厅官网为你收集整理的c#实现access导入导出excel的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得尊龙游戏旗舰厅官网网站内容还不错,欢迎将尊龙游戏旗舰厅官网推荐给好友。

网站地图