我去了 GitHub,按照 youtube 上关于 CSV 和 MVC 的“One”教程进行操作。这些都不起作用。(据我所知!)是否需要特别配置某些东西才能在 MVC 5 C#、Visual Studio 2013 上使用 CsvHelper?我有一个项目可以简单地将 csv、txt 文件(制表符分隔)和(管道分隔)读取到 SQL Server 2014 表中。我不明白为什么这如此困难。每篇文章都说“简单”,或“容易实现”。安装软件包后,您应该能够编写代码。现在我遇到了错误并寻找解决方案。“映射”、ini 文件等。是否有将 CsvHelper 与 C# MVC 5 结合使用的从头到尾教程?所有信息或示例均为 2012 年或更早。任何帮助,将不胜感激。这是我的代码。
Controller:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.IO;
using CsvHelper.Configuration;
using CsvHelper;
using System.Linq.Expressions;
using System.Reflection;
using ReadCSV.Models;
namespace ReadCSV.Controllers
{
public class HomeController : Controller
{
public ActionResult Index()
{
return View();
}
public class Upload1ClassMap : CsvClassMap<Upload1List>
{
public override void CreateMap()
{
Map(m => m.scansource).Name("scansource file");
Map(m => m.Manufacturer).Name("manufacturer");
Map(m => m.SKU).Name("SKU");
Map(m => m.ScansourceSKU).Name("ScansourceSKU");
Map(m => m.MSRP).Name("MSRP");
Map(m => m.RoughSummary).Name("Rough Summary");
Map(m => m.COST).Name("Cost");
Map(m => m.Inventory).Name("Inventory");
Map(m => m.Weight).Name("Weight");
Map(m => m.CAT4th).Name("CAT 4th");
Map(m => m.CAT1st).Name("CAT 1st");
Map(m => m.CAT2nd).Name("CAT 2nd");
Map(m => m.CAT3rd).Name("CAT 3rd");
Map(m => m.ProductName).Name("Product Name");
Map(m => m.Genre).Name("Genre");
Map(m => m.ProductName2).Name("Prodcut Name2");
Map(m => m.RougherSummary).Name("Rougher Summary");
Map(m => m.Cat_4th).Name("Cat 4th");
base.CreateMap();
}
}
[HttpPost]
public ActionResult Index(HttpPostedFileBase file)
{
string path = null;
List<Upload1List> upload1List = new List<Upload1List>();
try
{
if (file.ContentLength > 0)
{
var fileName = Path.GetFileName(file.FileName);
path = AppDomain.CurrentDomain.BaseDirectory + "upload\\" + fileName;
file.SaveAs(path);
var csv = new CsvReader(new StreamReader(path));
var Upload1List = csv.GetRecords<Upload1>();
foreach (var c in Upload1List)
{
Upload1List upload1Lists = new Upload1List();
try
{
upload1Lists.SKU = c.SKU;
}
catch
{
//Do Nothing
}
upload1Lists.scansource = c.scansource;
upload1Lists.Manufacturer= c.Manufacturer;
upload1Lists.ScansourceSKU = c.ScansourceSKU;
upload1Lists.MSRP = c.MSRP;
upload1Lists.RoughSummary = c.RoughSummary;
upload1Lists.COST = c.COST;
upload1Lists.Inventory = c.Inventory;
upload1Lists.Weight = c.Weight;
upload1Lists.CAT4th = c.CAT4th;
upload1Lists.CAT1st = c.CAT1st;
upload1Lists.CAT2nd = c.CAT2nd;
upload1Lists.CAT3rd = c.CAT3rd;
upload1Lists.ProductName = c.ProductName;
upload1Lists.Genre = c.Genre;
upload1Lists.ProductName2 = c.ProductName2;
upload1Lists.RougherSummary = c.RougherSummary;
upload1Lists.Cat_4th = c.Cat_4th;
upload1List.Add(upload1Lists);
}
}
}
catch
{
ViewData["error"] = "Upload failed";
}
return View();
}
}
}
Model:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace ReadCSV.Models
{
public class Upload1
{
public int scansource { get; set; }
public int Manufacturer { get; set; }
public string SKU { get; set; }
public string ScansourceSKU { get; set; }
public decimal MSRP { get; set; }
public string RoughSummary { get; set; }
public decimal COST { get; set; }
public decimal Inventory { get; set; }
public decimal Weight { get; set; }
public string CAT4th { get; set; }
public string CAT1st { get; set; }
public string CAT2nd { get; set; }
public string CAT3rd { get; set; }
public string ProductName { get; set; }
public string Genre { get; set; }
public string ProductName2 { get; set; }
public string RougherSummary { get; set; }
public string Cat_4th { get; set; }
}
public class Upload1List
{
public int scansource { get; set; }
public int Manufacturer { get; set; }
public string SKU { get; set; }
public string ScansourceSKU { get; set; }
public decimal MSRP { get; set; }
public string RoughSummary { get; set; }
public decimal COST { get; set; }
public decimal Inventory { get; set; }
public decimal Weight { get; set; }
public string CAT4th { get; set; }
public string CAT1st { get; set; }
public string CAT2nd { get; set; }
public string CAT3rd { get; set; }
public string ProductName { get; set; }
public string Genre { get; set; }
public string ProductName2 { get; set; }
public string RougherSummary { get; set; }
public string Cat_4th { get; set; }
}
}
Index:
{
@ViewBag.Title = "Home Page";
}
@model List<ReadCSV.Models.Upload1>
<h2>@ViewBag.Message</h2>
<p>
Upload your file
</p>
<table>
<tr>
<th>scansource</th>
<th>Manufacturer</th>
<th>SKU</th>
<th>ScansourceSKU</th>
<th>MSRP</th>
<th>RoughSummary</th>
<th>COST</th>
<th>Inventory</th>
<th>Weight</th>
<th>CAT4th</th>
<th>CAT1st</th>
<th>CAT2nd</th>
<th>CAT3rd</th>
<th>ProductName</th>
<th>Genre</th>
<th>ProductName2</th>
<th>RougherSummary</th>
<th>Cat_4th</th>
</tr>
@if (Model != null)
{
foreach (var c in Model)
{
<tr>
<th>@c.scansource</th>
<th>@c.Manufacturer</th>
<th>@c.SKU</th>
<th>@c.ScansourceSKU</th>
<th>@c.MSRP</th>
<th>@c.RoughSummary</th>
<th>@c.COST</th>
<th>@c.Inventory</th>
<th>@c.Weight</th>
<th>@c.CAT4th</th>
<th>@c.CAT1st</th>
<th>@c.CAT2nd</th>
<th>@c.CAT3rd</th>
<th>@c.ProductName</th>
<th>@c.Genre</th>
<th>@c.ProductName2</th>
<th>@c.RougherSummary</th>
<th>@c.Cat_4th</th>
</tr>
}
}
</table>
<form action="" method="post" enctype="multipart/form-data">
<table style="margin-top:150px">
<tr>
<td><label for="file">Filename:</label></td>
<td><input type="file" name="file" id="file" /></td>
<td><input type="submit" value="Upload" /></td>
</tr>
</table>
</form>
这是错误:
在 mscorlib.dll 中发生“System.IO.DirectoryNotFoundException”