700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > C#实现Access导入导出Excel

C#实现Access导入导出Excel

时间:2023-09-16 14:13:26

相关推荐

C#实现Access导入导出Excel

数据库|mysql教程

实现,Access,导入,导出,Excel,Access,E

数据库-mysql教程

菜单环绕源码,安装Ubuntu重启没有,tomcat9配置web,meta爬虫工具,nginx开启php,景德镇抖音seo推广优化公司lzw

A、 Access 从 Excel 中 导入 数据 1. 用到的 Excel 表的格式及内容 实现 OleDbConnection con = new OleDbConnection(); try { OpenFileDialog openFile = new OpenFileDialog();//打开文件对话框。 openFile.Filter = (“Excel 文件(*.xls)|*.xls”);//后缀

全网最大的网站源码,ubuntu模拟鼠标移动,基础爬虫可以干啥,php hprose 原理,亚马逊seo服务lzw

php 图文投票系统源码,vscode 更改主题,ubuntu srm,tomcat中打印,sqlite-shell,把服务器做成多个vps,安卓好看时间插件,大前端知识框架,爬虫与反爬虫pdf下载,php把逗号,新网站怎么优化seo,政府部门网站模板,discuz网页标签怎么改,国徽cms模板,php登录页面代码,公司网站管理系统,微赞程序lzw

A、Access从Excel中导入数据

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();

}

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();

}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。