[英]GetColumnName() LinqToExcel for csv file throw error The Microsoft Jet database engine could not find the object 'Sheet1$.txt'
I am using linqToExcel dll from to read a csv file and query the csv file the data . 我正在使用linqToExcel dll从读取csv文件并查询csv文件中的数据。 I also neeed the names of columns (Header ) from csv . 我还从csv修改了列的名称(Header)。
when i try to run the following code as per the document mentioned 当我尝试根据提到的文档运行以下代码时
its throw an error message as follow : 它抛出一条错误消息,如下所示:
The Microsoft Jet database engine could not find the object 'Sheet1$.txt'. Microsoft Jet数据库引擎找不到对象“ Sheet1 $ .txt”。 Make sure the object exists and that you spell its name and the path name correctly. 确保对象存在,并且正确拼写了它的名称和路径名。
my code is as follow : 我的代码如下:
string pathToExcelFile = ""
+ @"c:\users\rahul\documents\visual studio 2013\Projects\WebApplication1\WebApplication1\csv\student.csv";
string sheetName = "student";
var excelFile = new ExcelQueryFactory(pathToExcelFile);
//get all sheet names
var sheetNames = excelFile.GetWorksheetNames();
//get column name from csv
var columnNames = excelFile.GetColumnNames(sheetName);
To get the correct sheet name i tried to use excelFile.GetWorksheetNames() but its return zero records. 为了获得正确的工作表名称,我尝试使用excelFile.GetWorksheetNames(),但它返回零记录。
Note: I am using csv file and when i open same csv file in MS Excel its shows me student as Sheet Name even i tried with sheet1 as well. 注意:我正在使用csv文件,当我在MS Excel中打开相同的csv文件时,即使我也尝试了sheet1,它也向我显示了学生的工作表名称。
Here's my solution. 这是我的解决方案。
IEnumerable<string> columnNames;
var workSheetName = excel.GetWorksheetNames().FirstOrDefault();
if (string.IsNullOrEmpty(workSheetName))
{
//For CSV files there are not worksheet names
var worksheet = excel.Worksheet();
if (worksheet == null)
{
throw new ApplicationException("Unable to extract data. The file contains no data");
}
var row = worksheet.FirstOrDefault();
if (row == null)
{
throw new ApplicationException("Unable to extract data. The file contains no rows");
}
columnNames = row.ColumnNames;
}
else
{
columnNames = excel.GetColumnNames(workSheetName);
}
if (columnNames == null || !columnNames.Any())
{
throw new ApplicationException("Unable to extract column information.");
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.