【发布时间】:2017-05-02 04:02:19
【问题描述】:
我有以下平面文件:
https://www.screencast.com/t/42mFZIrB04b
如您所见,有一个 ROW 显然格式不正确。
我有以下代码:
public class FlatFileModel
{
public string Key { get; set; }
public string ArticleNumber { get; set; }
public string ColorCode { get; set; }
public string Description { get; set; }
public string Price { get; set; }
public string ActionPrice { get; set; }
public string Delivery { get; set; }
public string Q1 { get; set; }
public string Size { get; set; }
public string Color { get; set; }
}
[HttpPost]
public ActionResult Upload()
{
if (Request.Files.Count > 0)
{
var httpPostedFileBase = Request.Files[0];
if (httpPostedFileBase != null && httpPostedFileBase.ContentLength > 0)
{
//read data from input stream
using (var csvReader = new System.IO.StreamReader(httpPostedFileBase.InputStream))
{
string inputLine = "";
List<FlatFileModel> lineaArchivo = new List<FlatFileModel>();
//read each line
while ((inputLine = csvReader.ReadLine()) != null)
{
//get lines values
string[] values = inputLine.Split(new char[] { ',' });
for (int x = 0; x < values.Length; x++)
{
lineaArchivo.Add(new FlatFileModel()
{
Key = values[0],
ArticleNumber = values[1],
ColorCode = values[2],
Description = values[3],
Price = values[4],
ActionPrice = values[5],
Delivery = values[6],
Q1 = values[7],
Size = values[8],
Color = values[9]
});
}
}
csvReader.Close();
}
}
//var file = Request.Files[0];
//if (file != null && file.ContentLength > 0)
//{
// var fileName = Path.GetFileName(file.FileName);
// var path = Path.Combine(Server.MapPath("~/CSVs/"), fileName);
// file.SaveAs(path);
//}
}
return RedirectToAction("Index");
}
我需要一种简单的方法来检测不符合格式的 ROWS,因此我可以忽略它们,或者将它们记录为错误,但我不知道该怎么做。
谢谢
【问题讨论】:
-
特别是对于您的情况,
string[] values中的元素数量将不是 8。为什么不把它放进去
标签: c# .net asp.net-mvc entity-framework