我正在尝试读取excel CSV文件中的特定列。因为文件已上传,所以我不想将文件路径硬编码到特定位置。当我运行代码时,出现错误“ System.IO.FileNotFoundException:'找不到文件'C:\ Program Files(x86)\ IIS Express \ North_HCA_Texts3.csv”。 该文件存在是因为我是从桌面上选择的
HTML
<div class="col-md-10">
<input type="file" name="attachmentcsv" onchange="ValidateSingleInput(this);" class="form-control" id="attachmentcsv" />
</div>
C#
struct LineInMyCsvFile
{
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; }
}
string filePath = string.Empty;
string path = Server.MapPath(@"~/Uploads/");
filePath = Path.GetFileName(attachmentcsv.FileName);
IEnumerable<LineInMyCsvFile> allRecords = null;
using (var reader = System.IO.File.OpenText(filePath)) // error here
{
var csvParser = new CsvParser(reader);
CsvReader r = new CsvReader(csvParser);
allRecords = r.GetRecords<LineInMyCsvFile>().ToArray();
}
foreach (var record in allRecords)
{
// Imagine you only care about the value in MobileNumber:
listmobilenumber.Add(record.MobileNumber);
}
答案 0 :(得分:0)
出现异常的原因是您未在代码中使用path = Server.MapPath(@"~/Uploads/");
。