C# 使用C读取excel CSV文件中的特定列#
我有一个CSV电子表格,如下所示 我试图只从ColumnA读取数据。了解cvsHelper,但我不知道如何使用下面的代码来调整它。目前代码已阅读所有电子表格,请提供任何建议 C#代码C# 使用C读取excel CSV文件中的特定列#,c#,csv,model-view-controller,C#,Csv,Model View Controller,我有一个CSV电子表格,如下所示 我试图只从ColumnA读取数据。了解cvsHelper,但我不知道如何使用下面的代码来调整它。目前代码已阅读所有电子表格,请提供任何建议 C#代码 List listmobilenumber=new List(); string filePath=string.Empty; 如果(附件CSV!=null) { 字符串路径=Server.MapPath(“~/Uploads/”); 如果(!Directory.Exi
List listmobilenumber=new List();
string filePath=string.Empty;
如果(附件CSV!=null)
{
字符串路径=Server.MapPath(“~/Uploads/”);
如果(!Directory.Exists(path))
{
CreateDirectory(路径);
}
filePath=path+path.GetFileName(attachmentcsv.FileName);
字符串扩展名=Path.GetExtension(attachmentcsv.FileName);
attachmentcsv.SaveAs(文件路径);
字符串csvData=System.IO.File.ReadAllText(文件路径);
foreach(csvData.Split('\r','\n')中的字符串行)
{
如果(!string.IsNullOrEmpty(行))
{
添加(行);
}
}
}
您可以在添加中再次拆分该行:
if (!string.IsNullOrEmpty(row))
{
listmobilenumber.Add(row.Split(';').First());
}
首先查找“ColumnA”编号
你可以用这样的东西:
if (!string.IsNullOrEmpty(row))
{
string[] tokens = row.Split(';');
if(tokens.Length>=number)
listmobilenumber.Add(tokens[number-1]);
}
与其自己尝试解析CSV文件,不如使用类似的库。请注意,允许在CSV文件中出现
Abc、Def、“Ghi、jkl”、mno
,即逗号可以在值内找到,而不仅仅是分隔值。编写解析器非常困难
我已经知道了,但是这里有一个根据您的文件结构定制的示例,我假设您的列的名称与图像中给出的完全相同。从行string csvData=System.IO.File.ReadAllText(filePath)中替换代码>以后:
IEnumerable<LineInMyCsvFile> allRecords = null;
using (var reader = File.OpenText(filePath))
{
var csvParser = new CsvParser(reader);
CsvReader r = new CsvReader(csvParser);
allRecords = r.GetRecords<LineInMyCsvFile>().ToArray();
}
这是用于映射CSV文件中的列的结构,以便CsvHelper
可以处理它:
// I'm named badly, give me a sensible name that makes sense in context! :-)
struct LineInMyCsvFile
{
public string ColunmA { get; set; }
public string ColunmB { get; set; }
public string ColunmC { get; set; }
public string ColunmD { get; set; }
}
在@Rob的帮助下,我只是添加了我的最终代码,以便在将来帮助用户解决同样的问题
if (attachmentcsv != null)
{
List<string> listmobilenumber = new List<string>();
string filePath = string.Empty;
string path = Server.MapPath(@"~/Uploads/");
filePath = Path.Combine(path, attachmentcsv.FileName);
IEnumerable<CsvFileModel> allRecords = null;
StreamReader reader = new StreamReader(filePath);
using (CsvReader csvReader = new CsvReader(reader))
{
csvReader.Configuration.HeaderValidated = null;
csvReader.Configuration.MissingFieldFound = null;
allRecords = csvReader.GetRecords<CsvFileModel>().ToArray();
}
foreach (var record in allRecords)
{
// Passed Mobile number into a list
listmobilenumber.Add(record.MobileNumber);
}
}
而不是使用listmobilenumber.Add(row)添加整个行
,再次使用split
将行拆分为列在分号上拆分对CSV文件无效!此外,如果CSV文件中的值被引用,则在该值内使用逗号是完全合法的。回答很好,但不知何故,指向您文章的链接已失效。@MihirDave,奇怪,这对我来说很好-您能告诉我您看到了什么,以便我可以尝试对其进行排序吗?:)这就是谷歌Chrome所说的“无法访问此网站robertwray.co.uk目前无法访问”。请不要麻烦,我们办公室的防火墙正在阻止它,我可以通过我的手机访问它。对不起打扰了you@Rob谢谢但是我在这一行使用(var reader=File.OpenText(filePath))时出错,我将文件路径更改为this filePath=@“path+path.GetFileName(attachmentcsv.FileName)”;正如我在你的博客上看到的,但我仍然有错误
// I'm named badly, give me a sensible name that makes sense in context! :-)
struct LineInMyCsvFile
{
public string ColunmA { get; set; }
public string ColunmB { get; set; }
public string ColunmC { get; set; }
public string ColunmD { get; set; }
}
if (attachmentcsv != null)
{
List<string> listmobilenumber = new List<string>();
string filePath = string.Empty;
string path = Server.MapPath(@"~/Uploads/");
filePath = Path.Combine(path, attachmentcsv.FileName);
IEnumerable<CsvFileModel> allRecords = null;
StreamReader reader = new StreamReader(filePath);
using (CsvReader csvReader = new CsvReader(reader))
{
csvReader.Configuration.HeaderValidated = null;
csvReader.Configuration.MissingFieldFound = null;
allRecords = csvReader.GetRecords<CsvFileModel>().ToArray();
}
foreach (var record in allRecords)
{
// Passed Mobile number into a list
listmobilenumber.Add(record.MobileNumber);
}
}
struct CsvFileModel
{
public string MobileNumber { get; set; }
public string NAME { get; set; }
public string EMPLOYEENUMBER { get; set; }
public string JOBTITLEROLE { get; set; }
public string BAND { get; set; }
}