LinqToExcel 返回 .csv 文件的空白行

LinqToExcel returning blank rows for .csv files

一段时间以来,我一直在使用 LinqToExcel 从 .xlsx 文件中成功导入数据。但是最近,我收到了一个 .csv 文件,我无法读取其中的数据。

假设文件包含以下数据:

Col1 Col2 Col3
 A    B    C
 D    E    F

我创建了一个 class 来映射列:

public class Test
{
    [ExcelColumn("Col1")]
    public string Col1 { get; set; }

    [ExcelColumn("Col2")]
    public string Col2 { get; set; }

    [ExcelColumn("Col3")] 
    public string Col3 { get; set; }
}

然后我尝试像这样读取数据:

var test = from c in excel.Worksheet<Test>()
           select c;

查询成功returns两个Test-objects,但所有属性值为null。

我什至尝试在没有 class 和 header 的情况下读取数据:

var test = from c in excel.WorksheetNoHeader()
select c;

在本例中,查询也returns两行,均有3行cells/values。但同样所有这些值都是空的。这可能是什么问题?

我还应该注意到文件在 Excel 中打开并且看起来非常好。此外,使用 StreamReader,我能够读取它的所有行和值。

您是否尝试在最后调用 ToListToArray 来具体化您的查询?

我尝试重新创建您的案例,使用以下代码片段从 Excel 文件中读取数据没有遇到任何问题:

var excel = new ExcelQueryFactory(FilePath);
List<Test> tests = (
    from c in excel.Worksheet<Test>()
    select c
)
.ToList();

它 returns 两个所有属性都正确填充的对象。

一件小事,当我最初添加 ToList 时,出现以下异常:

The 'Microsoft.ACE.OLEDB.12.0' provider is not registered on the local machine.'

根据他们在我机器上 the official docs seems reasonable since I was missing Microsoft Access Database Engine 2010 Distributable 中所说的内容。

每一列中的数据类型是什么? (字符串、数字、...)

根据Initializing the Microsoft Excel driver

TypeGuessRows

The number of rows to be checked for the data type. The data type is determined given the maximum number of kinds of data found. If there is a tie, the data type is determined in the following order: Number, Currency, Date, Text, Boolean. If data is encountered that does not match the data type guessed for the column, it is returned as a Null value. On import, if a column has mixed data types, the entire column will be cast according to the ImportMixedTypes setting. The default number of rows to be checked is 8. Values are of type REG_DWORD.

参见 post Can I specify the data type for a column rather than letting linq-to-excel decide?

post Setting TypeGuessRows for excel ACE Driver 说明了如何更改 TypeGuessRows 的值。

When the driver determines that an Excel column contains text data, the driver selects the data type (string or memo) based on the longest value that it samples. If the driver does not discover any values longer than 255 characters in the rows that it samples, it treats the column as a 255-character string column instead of a memo column. Therefore, values longer than 255 characters may be truncated. To import data from a memo column without truncation, you must make sure that the memo column in at least one of the sampled rows contains a value longer than 255 characters, or you must increase the number of rows sampled by the driver to include such a row. You can increase the number of rows sampled by increasing the value of TypeGuessRows under the HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Jet.0\Engines\Excel registry key.

One more thing we need to keep in mind is that the registry HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Jet.0\Engines\Excel\TypeGuessRows only applies to Excel 97- 2003. For Excel 2007 and higher version, Excel Open XML (.XLSX extension) actually uses ACE OLE DB provider rather JET provider. If you want to keep the file extension as .XLSX, you need to modify the following registry key according to your Excel version:

Excel 2007: HKEY_LOCAL_MACHINE\Software\Microsoft\Office.0\Access Connectivity Engine\Engines\Excel\TypeGuessRows Excel 2010: HKEY_LOCAL_MACHINE\Software\Microsoft\Office.0\Access Connectivity Engine\Engines\Excel\TypeGuessRows Excel 2013: HKEY_LOCAL_MACHINE\Software\Microsoft\Office.0\Access Connectivity Engine\Engines\Excel\TypeGuessRows