Linq to Excel,从 CSV 文件中获取列名
Linq to Excel, get column names from CSV file
我需要使用 LINQ to Excel
获取 CSV 文件的列 headers
我使用 https://github.com/paulyoder/LinqToExcel
上指定的代码
这适用于 .XLSX 文件,但不适用于 CSV
//Select File
var book = new LinqToExcel.ExcelQueryFactory(link + @"\" + fileName);
//Select firtworkbook
var query = (from row in book.Worksheet(0) select row).ToList();
var workSheetName = book.GetWorksheetNames();
var columnNames = (from row in book.GetColumnNames(workSheetName.FirstOrDefault()) select row).ToList();
我还尝试对 sheet 名称进行硬编码并调用 CSV sheet1
var columnNames = (from row in book.GetColumnNames("Sheet1") select row).ToList();
这中断并给我这个错误:
Message = "'54733658.csv' is not a valid worksheet name in file...
我仔细检查了它是正确的路径。
然后我尝试了:(需要工作sheet 与文件名相同的名称 - 扩展名)
string extension = System.IO.Path.GetExtension(fileName);
string result = fileName.Substring(0, fileName.Length - extension.Length);
var colNames = book.GetColumnNames(result, "A1:F1").ToList();
这给了我以下错误:
The Microsoft Jet database engine could not find the object '02119249$A1_Z1.txt'. Make sure the object exists and that you spell its name and the path name correctly.
我用谷歌搜索了那个错误,这些结果不适用。
我没有足够的时间弄清楚为什么我无法阅读 CSV 列 headers
遇到同样问题的朋友:
读取第一行并制作一个字符串列表,这是我的方法:
public List<string> ColumnNameGenerator(string FilePath)
{
string firstLine = "";
using (StreamReader reader = new StreamReader(FilePath))
{
firstLine = reader.ReadLine() ?? "";
}
return firstLine.Split(',').ToList();
}
我需要使用 LINQ to Excel
获取 CSV 文件的列 headers我使用 https://github.com/paulyoder/LinqToExcel
上指定的代码这适用于 .XLSX 文件,但不适用于 CSV
//Select File
var book = new LinqToExcel.ExcelQueryFactory(link + @"\" + fileName);
//Select firtworkbook
var query = (from row in book.Worksheet(0) select row).ToList();
var workSheetName = book.GetWorksheetNames();
var columnNames = (from row in book.GetColumnNames(workSheetName.FirstOrDefault()) select row).ToList();
我还尝试对 sheet 名称进行硬编码并调用 CSV sheet1
var columnNames = (from row in book.GetColumnNames("Sheet1") select row).ToList();
这中断并给我这个错误:
Message = "'54733658.csv' is not a valid worksheet name in file...
我仔细检查了它是正确的路径。
然后我尝试了:(需要工作sheet 与文件名相同的名称 - 扩展名)
string extension = System.IO.Path.GetExtension(fileName);
string result = fileName.Substring(0, fileName.Length - extension.Length);
var colNames = book.GetColumnNames(result, "A1:F1").ToList();
这给了我以下错误:
The Microsoft Jet database engine could not find the object '02119249$A1_Z1.txt'. Make sure the object exists and that you spell its name and the path name correctly.
我用谷歌搜索了那个错误,这些结果不适用。
我没有足够的时间弄清楚为什么我无法阅读 CSV 列 headers
遇到同样问题的朋友:
读取第一行并制作一个字符串列表,这是我的方法:
public List<string> ColumnNameGenerator(string FilePath)
{
string firstLine = "";
using (StreamReader reader = new StreamReader(FilePath))
{
firstLine = reader.ReadLine() ?? "";
}
return firstLine.Split(',').ToList();
}