gpt4 book ai didi

c# - CSV实际上是....分号分隔值...(AZERTY上的Excel导出)

转载 作者:行者123 更新时间:2023-11-30 13:29:04 28 4
gpt4 key购买 nike

我在这里有点困惑。

当我使用 Excel 2003 将工作表导出为 CSV 时,它实际上使用分号 ...

Col1;Col2;Col3
shfdh;dfhdsfhd;fdhsdfh
dgsgsd;hdfhd;hdsfhdfsh

现在,当我使用 Microsoft 驱动程序读取 csv 时,它需要逗号并将列表视为一个大列???

我怀疑 Excel 导出时带有分号,因为我有 AZERTY 键盘。但是,CSV 阅读器是否也必须考虑不同的分隔符?

我如何知道合适的分隔符,和/或正确读取 csv ??

    public static DataSet ReadCsv(string fileName)
{
DataSet ds = new DataSet();
string pathName = System.IO.Path.GetDirectoryName(fileName);
string file = System.IO.Path.GetFileName(fileName);
OleDbConnection excelConnection = new OleDbConnection
(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + pathName + ";Extended Properties=Text;");
try
{
OleDbCommand excelCommand = new OleDbCommand(@"SELECT * FROM " + file, excelConnection);
OleDbDataAdapter excelAdapter = new OleDbDataAdapter(excelCommand);
excelConnection.Open();
excelAdapter.Fill(ds);
}
catch (Exception exc)
{
throw exc;
}
finally
{
if(excelConnection.State != ConnectionState.Closed )
excelConnection.Close();
}
return ds;
}

最佳答案

一种方法是只使用 decent CSV library ;一个让你指定分隔符的:

using (var csvReader = new CsvReader("yourinputfile.csv"))
{
csvReader.ValueSeparator = ';';
csvReader.ReadHeaderRecord();

while (csvReader.HasMoreRecords)
{
var record = csvReader.ReadDataRecord():
var col1 = record["Col1"];
var col2 = record["Col2"];
}
}

关于c# - CSV实际上是....分号分隔值...(AZERTY上的Excel导出),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2919591/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com