You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Ubuntu服务器上C#用OLEDB更新Excel表时遇对象找不到错误

问题:Ubuntu服务器上通过OLEDB更新Excel工作表提示找不到对象"Customers"

我尝试更新Ubuntu服务器上Excel文件中已存在的「Customers」工作表,但通过OLEDB连接时出现错误:

The Microsoft Access Database engine could not find the object "Customers"

我已经在更新语句里加了$符号,也确认过服务器端的文件访问权限,但错误依旧。

使用的Provider是Provider=Microsoft.ACE.OLEDB.12.0,连接字符串如下:

$@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source ={" +DataSource + "};Extended Properties='Excel 12.0'"

DataSource的值为:"\\serverIP\\home\\data\\fileName.xlsx

相关代码如下:

string editString = "update [Customers$] set Name = '"+ gunaLineTextBox2.Text + "', phone ='"+ gunaLineTextBox3.Text + "', CSer1 ='"+ Stype1 + "', MainM3u ='"+ Murl + "', CSer2 ='"+ Stype2 + "', BackM3u='"+ Burl + "', CSer3 ='"+ Stype3 + "', secretM3u ='"+Surl+ "', Ex = '"+ gunaLineTextBox4.Text + "', notes='"+ gunaLineTextBox11.Text + "' where id ='"+id+"'";

SqlHelper.botEdit(editString, @"\\serverIP\home\data\m3u.xlsx", 0);

public static void botEdit(string editString, string DataSource, int x)
{
     try
     {
         OleDbCommand com = new OleDbCommand();
         OleDbConnection con = new OleDbConnection($@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source ={" +DataSource + "};Extended Properties='Excel 12.0'");// Xml;HDR=YES;

         con.Open();
         com.Connection = con;
         com.CommandText = editString;
         com.ExecuteNonQuery();

         if (x > 1)
         {
             MessageBox.Show("Done");
         }

         con.Close();
     }
     catch (Exception ex) 
     {
         MessageBox.Show(ex.Message);
     }
}

解决要点

  1. ACE引擎不支持Linux:Microsoft ACE OLEDB是Windows专属组件,Ubuntu系统无法运行,这是核心问题。别再尝试用它操作Linux上的Excel文件。
  2. 换用跨平台Excel库:改用EPPlus、NPOI这类原生支持.NET跨平台的库,它们不需要依赖ACE引擎,能直接在Ubuntu上读写Excel。
  3. 补全连接字符串参数(若换回Windows环境):如果后续切换回Windows环境,连接字符串要加上HDR=YES(指定第一行为表头),避免解析错误:
    $@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source={DataSource};Extended Properties='Excel 12.0;HDR=YES;'"
    
  4. 核对工作表名称大小写:Linux环境下文件名和工作表名称区分大小写,确认Excel里的工作表确实是Customers(注意大小写和无额外空格)。
  5. 修复SQL注入漏洞:当前代码直接拼接用户输入到SQL语句里,风险极高,必须改用参数化查询,无论用哪种方式操作数据都要这么做。

内容的提问来源于stack exchange,提问作者TREX 20

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.17 19:52:38