简体   繁体   English

ODBC驱动程序无法读取Excel中添加的行

[英]ODBC driver cannot read rows added in Excel

I create xls/xlsx file from C# using ODBC (with Provider=Microsoft.ACE.OLEDB.12.0 ). 我使用ODBC(使用Provider=Microsoft.ACE.OLEDB.12.0 )从C#创建xls/xlsx文件。 The result table has 4 rows (for example). 结果表有4行(例如)。 I open the file with Excel, add 5-th row and save the file. 我用Excel打开文件,添加第5行并保存文件。 When try to read it from C# over ODBC with SELECT * FROM [table] I get only the original 4 rows without 5th. 当尝试使用SELECT * FROM [table] ODBC通过C#读取它时,我只得到原始的4行,而不是5行。 It seems ODBC stores somewhere in XLS file the number of rows and later reads only them without new data entered from Excel or LibreOffice. 似乎ODBC在XLS文件中的某处存储了行数,以后只读取它们,而没有从Excel或LibreOffice输入新数据。 Is this known problem and can I solve it? 这是已知问题,我可以解决吗? If I create new spreadsheet in Excel, all its rows are read fron C#. 如果我在Excel中创建新的电子表格,则其所有行均从C#中读取。

EDIT: I found some useful information. 编辑:我发现了一些有用的信息。 When the XLS file is first created from C#/ODBC, there are 2 tables (sheets). 首次从C#/ ODBC创建XLS文件时,有2个表(表格)。 If the table name is TABLE, DataTable sheets = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null) will contain sheets.Rows[0] == "TABLE" and sheets.Rows[1] == "TABLE$". 如果表名是TABLE,则DataTable sheets = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null)将包含sheets.Rows[0] ==“ TABLE”和sheets.Rows[1] ==“ TABLE $”。 Excel will show only one sheet "TABLE". Excel将仅显示一张工作表“ TABLE”。 After edit the changes (5th row) exist only in "TABLE$" sheet. 编辑后,更改(第5行)仅存在于“ TABLE $”表中。

Are you adding the 5th row by code if yes, could you please share the code lines which you are using for doing the same. 如果是,您是否按代码添加第五行,请您分享用于执行此操作的代码行。 There might be following issue in your code. 您的代码中可能存在以下问题。

  1. Save commit not done properly. 保存提交未正确完成。
  2. Before reading the file connection refresh not done. 在读取文件连接之前,刷新未完成。

I think I found the problem. 我想我找到了问题。 It seems that internal spreadsheet names created by Excel have "$" sign at the end. 似乎由Excel创建的内部电子表格名称的末尾带有“ $”符号。 The sheet name generated by ODBC are the exact string given in CREATE TABLE . ODBC生成的工作表名称是CREATE TABLE给出的确切字符串。 On the other hand, Excel (and LibreOffice) show only one sheet for both TABLE and TABLE$ sheets. 另一方面,Excel(和LibreOffice)对于TABLETABLE$表仅显示一个工作TABLE If I edit the table in Excel, after save the changes are only in TABLE$ . 如果我在Excel中编辑表,则保存后所做的更改仅在TABLE$ The other sheet TABLE is unchanged. 另一张TABLE保持不变。 When I do SELECT * FROM [TABLE] the result is from the original ODBC generated table without Excel changes. 当我执行SELECT * FROM [TABLE] ,结果来自原始ODBC生成的表,而没有Excel更改。 Now I enumerate the available sheets inside XLS file and if first sheet name does not end with "$" and sheets are more than 1, I add "$" to first sheet name and open the correct table. 现在,我枚举XLS文件中的可用工作表,如果第一个工作表名称不以“ $”结尾并且工作表大于1,则在第一个工作表名称中添加“ $”并打开正确的表。 I suppose ODBC connection string may include option to work with "$"-ending tables... 我想ODBC连接字符串可能包括用于以“ $”结尾的表的选项...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM