Linq到CSV错误“读取数据时出现1个或多个异常” [英] Linq to CSV error "1 or more exceptions while reading data"
本文介绍了Linq到CSV错误“读取数据时出现1个或多个异常”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我收到错误:使用CSVClass类型读取数据时有1个或更多异常。读取文件C:\Users\admin \Desktop\Project\Project\App_Data\demo.csv。
以下是我的代码:
I got an error: There were 1 or more exceptions while reading data using type "CSVClass". Reading file "C:\Users\admin\Desktop\Project\Project\App_Data\demo.csv".
Here are my code:
if (filMyFile.PostedFile != null)
{
HttpPostedFile myFile = filMyFile.PostedFile;
string fname = Path.GetFileName(myFile.FileName);
string path = Server.MapPath(Path.Combine("~/App_Data/", fname));
myFile.SaveAs(path);
try
{
CsvFileDescription cv = new CsvFileDescription
{
SeparatorChar = ',',
FirstLineHasColumnNames = true,
EnforceCsvColumnAttribute = true
};
CsvContext cc = new CsvContext();
List<CSVClass> csvData = (cc.Read<CSVClass>(path, cv)).ToList();
foreach (var ss in csvData)
{
// Console.WriteLine(ss._strNO + "---" + ss._strCity);
}
}
catch (Exception ex)
{
}
}
错误发生在
The error happen in
List<CSVClass> csvData = (cc.Read<CSVClass>(path, cv)).ToList();
我的班级在这里:
My class is here:
public class CSVClass
{
[CsvColumn(FieldIndex = 0)]
public string Survey { get; set; }
[CsvColumn(FieldIndex = 1)]
public string Table_name { get; set; }
[CsvColumn(FieldIndex = 2)]
public string Code { get; set; }
[CsvColumn(FieldIndex = 3)]
public string YY { get; set; }
[CsvColumn(FieldIndex = 4)]
public string MM { get; set; }
[CsvColumn(FieldIndex = 5)]
public string Desc { get; set; }
[CsvColumn(FieldIndex = 6)]
public string Start_Value { get; set; }
[CsvColumn(FieldIndex = 7)]
public string End_Value { get; set; }
[CsvColumn(FieldIndex = 8)]
public string Remark { get; set; }
[CsvColumn(FieldIndex = 9)]
public bool Is_default { get; set; }
[CsvColumn(FieldIndex = 10)]
public int Order_no { get; set; }
[CsvColumn(FieldIndex = 11)]
public int? ParentId { get; set; }
[CsvColumn(FieldIndex = 12)]
public string Parent_Table { get; set; }
}
我的csv:
My csv:
Survey,Table_name,Code,YY,MM,Desc,Start_Value,End_Value,Remark,Is_default,Order_no,ParentId,Parent_Table
SEV,sevcodetable,00,2015,04,test,,,,1,1,,
更多例外是什么意思?我该如何解决?
谢谢
What does it mean more exceptions? how can I fix it?
Thanks
推荐答案
从CSV文件中读取
Read From a CSV File
public List<string[]> DelimeterFileToList(string strFileName,string[] aDelimeters)
{
List<string[]> lstData = new List<string[]>();
try
{
using (TextFieldParser parser = new TextFieldParser(strFileName))
{
parser.Delimiters = aDelimeters;
while (true)
{
string[] parts = parser.ReadFields();
if (parts == null)
{
break;
}
lstData.Add(parts);
}
}
}
catch (Exception ex)
{
//Log Exception
}
return lstData;
}
从列表中写入CSV
public WriteListToCSV(List<CSVClass> lstCsvClass,string FileName)
{
var engine = new FileHelperEngine(typeof(CSVClass))
{
HeaderText = typeof(CSVClass).GetCsvHeader()
};
if (!Directory.Exists(path.GetDirectory(FileName)))
{
Directory.CreateDirectory(path.GetDirectory(FileName));
}
engine.WriteFile(FileName, lstCsvClass.ToArray());
lstCsvClass.Clear();
}
名称空间必填:
using FileHelpers;
using FileHelpers.RunTime;
这篇关于Linq到CSV错误“读取数据时出现1个或多个异常”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文