作者:乔山办公网日期:
返回目录: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的表要e799bee5baa6e78988e69d83366加$
//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("导入数据成功", "导入数据", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
你这个问题我也遇到 了,纠结了好久,office2003用WEBBROWSER 这个方法可以,可2007不行,不过我最后找到办法解决了zhidao,理解寻找解决办法人的心情,所以这里贡献出来给大家。
DsoFramer_KB311765_x86.exe
用这个控件,可以上网去下载,百度或到微软官网下载。如果下不到也可以找我发。
这个控件可以在WINFORM里面直接把EXCEL引用上版来显示在控件里面,就像在操作EXCEL一模一样。
可以在Excel里面先设置权好,把标题隐藏,状态栏,编辑栏,滚动条等这些隐藏,这样看起来也看不出是在Excel操作。也可以在Excel里设置好数据有效性,这样就只能选择,可以设置只允许用户选择指定单元格,其他单元格不能选择。包括颜色,工作表保护,禁止随便修改。也可以在Excel里面设置好宏,这里操作也会执行。