简体   繁体   English

从Excel读取(.xlsx)

[英]Reading from Excel (.xlsx)

I followed a few examples on the internet. 我在互联网上遵循了一些示例。 Installed AccesDataBaseEngine. 已安装AccesDataBaseEngine。

But still I get this error message: 但是我仍然收到此错误消息:

System.Data.OleDb.OleDbException: 'External table is not in the expected format.'

I tried some things that people suggestes, but nothing works. 我尝试了人们建议的一些方法,但是没有任何效果。

This is the code: 这是代码:

string path = @"C:\...\...\Desktop\MyFile.xlsx";
           string connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties=Excel 12.0;";

            var dataAdapter = new OleDbDataAdapter("SELECT * FROM [HHH$]", connectionString);
            var dataTable = new DataTable();
            dataAdapter.Fill(dataTable);

What did I forgot? 我忘了什么

Without knowing the version of the Excel file your working with or it's data contents it's hard to say for sure. 在不知道您使用的Excel文件版本或数据内容的情况下,很难确定。 Have you tried including HDR and IMEX options in your connection string? 您是否尝试过在连接字符串中包含HDR和IMEX选项?

HDR=Yes; - Indicates the first row is a header row containing column names and not data -表示第一行是包含列名而不是数据的标题行

HDR=No; - Indicates there is no header row -表示没有标题行

IMEX=1; - Indicates to always read mixed data columns (columns containing numbers, dates, strings, etc.) as text. -表示始终将混合数据列(包含数字,日期,字符串等的列)读取为文本。 Note that this option may cause problems if you're writing back to the Excel sheet since it's essentially ignoring types on read. 请注意,如果您要写回Excel工作表,此选项可能会导致问题,因为它实际上会忽略读取时的类型。 An alternative would be to explicitly define the columns of your DataTable. 一种替代方法是显式定义DataTable的列。

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

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