简体   繁体   English

使用C#读取错误的标题行读取Excel文件

[英]Reading an Excel file with C# with misplaced header row

I have been using OleDbConnection to read an Excel files from my C# code. 我一直在使用OleDbConnection从我的C#代码读取Excel文件。

Everything work great when the file has a top header row and the data following it down. 当文件具有顶部标题行且数据紧随其后时,一切工作都很好。

The following connection string is used: 使用以下连接字符串:

 var query = "SELECT [Col1],[Col2],[Col3] FROM [Sheet1$]";

Now I need to process an Excel file that has an obligatory content from the first down to the 15th row. 现在,我需要处理一个从第一行到第15行必须包含内容的Excel文件。 The 15th row is a header row and the rest is the data for the rows. 第15行是标题行,其余的是行的数据。

How can I read such a file? 我如何读取这样的文件?

To read from 15th row, modify your query to use table name as FROM [Sheet1$A15:ZZ] - 要从第15行进行读取,请修改查询以将表名用作FROM [Sheet1$A15:ZZ] -

var query = string.Format("select [Col1],[Col2],[Col3] from [{0}${1}]", "Sheet1","A15:ZZ");

A15 represents header row. A15代表标题行。

Make sure that you have set "HDR=YES" in your connection string. 确保已在连接字符串中设置"HDR=YES"

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

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