OLEDB with updating excel cells

亡梦爱人 提交于 2019-12-07 18:19:56

问题


I wrote this method to update an excel cell:

public void update(string fileName, string sheetName)
{
   string connString = connString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + Server.MapPath(fileName) + ";Extended Properties=Excel 12.0";

try
{
   OleDbConnection oledbConn = new OleDbConnection(connString);

   oledbConn.Open();

  OleDbCommand cmd = new OleDbCommand("UPDATE ["+sheetName+"$B5:B5] SET F1=17", oledbConn);

  cmd.ExecuteNonQuery();

  oledbConn.Close();
}
catch(Exception ex)
{
  Debug.Write("Error: " + ex.Message);
}
}

I called it like this:

update("test.xls", "test");

The B5 cell is available in "test" sheet, but the value never gets updated.

I even tried with this one:

UPDATE ["+sheetName+"$B5:B5] SET F1='17'

and I always got this exception: No value given for one or more required parameters.

Any idea?

Thanks in advance.


回答1:


EDIT I notice you have missed HDR=No.

"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + FileName + 
";Extended Properties=""Excel 12.0;HDR=No"""

EDIT Tested in C# Express

Either:

"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=z:\\docs\\myspreadsheet.xls;Extended Properties='Excel 12.0 xml;HDR=No'"

Note xml

Or

"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=z:\\docs\\myspreadsheet.xls;Extended Properties='Excel 8.0;HDR=No'"

For *.xls



来源:https://stackoverflow.com/questions/9160275/oledb-with-updating-excel-cells

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!