我发生错误时的环境:Windows 7,Framework 4、0,Microsoft Office 2007,VS2010,c# WinForm;
部分代码:
string strConn = "Provider=Microsoft.Ace.OleDb.12.0;Persist Security Info=False;" + "data source=" + @excelPath + ";Extended Properties='Excel 12.0; HDR=yes; IMEX=2'";
OleDbConnection conn = new OleDbConnection();
conn.ConnectionString = strConn;
try
{
OleDbCommand cmd = null;
try
{
cmd = new OleDbCommand("Insert Into [Sheet1$] Values('abc','bac','0','123456','test','测试','aa')",conn);//(A,B,C,D,E,F,G)
cmd.ExecuteNonQuery();
}
catch (System.Exception ex)
{
textBox1.Text += ("插入数据失败:" + ex.Message);
textBox1.Text += ("\r\n");
}
遇到此错误的时候第一想到的就是没有权限,但使用管理员身份运行依然是相同的错误!
又通过以下代码添加权限,还是一样的错误:
FileInfo fi = new FileInfo(excelPath);
System.Security.AccessControl.FileSecurity fileSecurity = fi.GetAccessControl();
f