winfrom中EXCEL数据导入ACCESS和ACCESS数据导出EXCEL,有源码最好啦 有源码最好啦,我第一次用winfrom和access,代码写哪里都不知道,先谢谢大家 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 http://blog.sina.com.cn/s/blog_4b191d2f0100am3l.html http://www.cnblogs.com/zhongcj/archive/2007/07/15/819082.aspxhttp://www.cnblogs.com/aaliujing/archive/2008/08/30/634383.html 1\创建winform程序2\添加一button控件button1,button1.text="将excel写入access"3\双击button1在生成的button1_click事件中写以下代码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(); } 4\返回form视图,添加一button控件button2,button2.text="将access写入excel" 5\双击button2在生成的button2_click事件中写以下代码 OleDbConnection conExcel = 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的路径) string sql = "select top 65535 * into [Excel 8.0;database=" + filename + "].[用户表] from 用户表"; conExcel.ConnectionString = "Provider=Microsoft.Jet.Oledb.4.0;Data Source=" + Application.StartupPath + "\\Appdata.mdb";//将数据库放到debug目录下。 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(); } http://www.cnblogs.com/aaliujing/archive/2007/01/30/634383.html 6\需要添加引用Microsoft Office 11.0 Object Library组件7\前面还要代码引用using Excel = Microsoft.Office.Interop.Excel; OLEDB 有相应的连接 EXCEL的连接字符串的,连接后,可以使用SQL进行操作 数据导出到Excel(或Word)源代码大全http://blog.csdn.net/wonsoft/archive/2008/11/16/3311769.aspx SQL Server 与 Excel 相互调用(ACCESS也可以参考,只是驱动不同罢了。)http://blog.csdn.net/wonsoft/archive/2008/11/16/3312320.aspx 数据导出到Excel(或Word)源代码大全http://blog.csdn.net/wonsoft/archive/2008/11/16/3311769.aspx SQL Server 与 Excel 相互调用(ACCESS也可以参考,只是驱动不同罢了。)http://blog.csdn.net/wonsoft/archive/2008/11/16/3312320.aspx 知道换肤的帮帮忙哦 请各位高手提供一个下拉出复选框的下拉列表 c# Excel获取chartobjects对象的个数 winform如何弹出一个包含控件和对应逻辑代码的窗口 datagrid控件的几个操作问题 急!有关于SQL语句问题 c# MDI 仿桌面,仿任务栏,仿桌面图标 C#做一个和360 软件管家差不多的软件 DataTable中Select问题 泛型如何读取变量?这里这么写肯定是不对的。请问怎么写? SmartClient发布的时候如何手动添加一些文件? c#操作Access总结
http://www.cnblogs.com/aaliujing/archive/2008/08/30/634383.html
2\添加一button控件button1,button1.text="将excel写入access"
3\双击button1在生成的button1_click事件中写以下代码
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();
}
5\双击button2在生成的button2_click事件中写以下代码
OleDbConnection conExcel = 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的路径)
string sql = "select top 65535 * into [Excel 8.0;database=" + filename + "].[用户表] from 用户表";
conExcel.ConnectionString = "Provider=Microsoft.Jet.Oledb.4.0;Data Source=" + Application.StartupPath + "\\Appdata.mdb";//将数据库放到debug目录下。
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();
}
7\前面还要代码引用using Excel = Microsoft.Office.Interop.Excel;
http://blog.csdn.net/wonsoft/archive/2008/11/16/3311769.aspx
SQL Server 与 Excel 相互调用(ACCESS也可以参考,只是驱动不同罢了。)
http://blog.csdn.net/wonsoft/archive/2008/11/16/3312320.aspx
http://blog.csdn.net/wonsoft/archive/2008/11/16/3311769.aspx
SQL Server 与 Excel 相互调用(ACCESS也可以参考,只是驱动不同罢了。)
http://blog.csdn.net/wonsoft/archive/2008/11/16/3312320.aspx