这个是自定义的类 在解决方案里 添这个类OleDbConnection con = new OleDbConnection(); " />
乔山办公网我们一直在努力
您的位置:乔山办公网 > excel表格制作 > VS2010下,c#中添加什么引用才能使用formtable...-c 嵌入excel,怎么在excel

VS2010下,c#中添加什么引用才能使用formtable...-c 嵌入excel,怎么在excel

作者:乔山办公网日期:

返回目录:excel表格制作




这个是自定义的类 在解决方案里 添这个类


OleDbConnection con = new OleDbConnection();
try
{
OpenFileDialog openFile = new OpenFileDialog();//打开文件对话框。
openFile.Filter = ("所有文件(*.*)|*.*|Excel 文件(*.xls)|*.xls|Excel 文件(*.xlsx)|*.xlsx");//后缀名。
//openFile.Filter = ("Excel 文件(*.xls)|*.xls");//后缀名。
if (openFile.ShowDialog() == DialogResult.OK)
{
string filename = openFile.FileName;
int index = filename.LastIndexOf("//");//截取文件的名字
filename = filename.Substring(index + 1);
con.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;" + @"Data Source=test.accdb;";
//将excel导入access
//distinct :删除excel重复的行.
//[excel名].[sheet名] 已有的excel的表要加$
//where not in : 插入不重复的记录。

string sql = "insert into 学生表 select distinct * from [Excel 8.0;database=" +
filename + "].[Sheet1$] where ID not in (select ID from 学生表) ";
string sql1 = "delete from 学生表";
OleDbCommand com1 = new OleDbCommand(sql1, con);
OleDbCommand com = new OleDbCommand(sql, con);
con.Open();
com1.ExecuteNonQuery();
com.ExecuteNonQuery();

MessageBox.Show("导入数据e69da5e6ba90e799bee5baa6e79fa5e98193364成功", "导入数据", MessageBoxButtons.OK, MessageBoxIcon.Information);
}

相关阅读

关键词不能为空
极力推荐

ppt怎么做_excel表格制作_office365_word文档_365办公网