我已经整理了一个我认为有效的CSV导入器,虽然我收到了这个错误,但是如何让这个列为null,所以当它将它添加到表时会自动设置ID?我试过了:
csv.Configuration.WillThrowOnMissingFields = false;
但它无法识别,这是我在尝试上传时遇到的错误:
CsvHelper.ValidationException:找不到索引为0的标头匹配['ID']名称。如果您希望丢失某些标头并且想要忽略此验证,请将配置HeaderValidated设置为null。您还可以更改功能以执行其他操作,例如记录问题。'
[HttpPost]
[ActionName("CreateBulk")]
public ActionResult CreateBulkUpload()
{
object db;
var file = Request.Files["attachmentcsv"];
using (var csv = new CsvReader(new StreamReader(file.InputStream), true))
{
var records = csv.GetRecords<Client>().ToList();
foreach (var item in records)
{
var strip = item.homePage.Replace("https://www.", "").Replace("http://www.", "")
.Replace("https://", "").Replace("http://", "").Replace("www.", "");
string[] URLtests =
{"https://www." + strip, "http://www." + strip, "https://" + strip, "http://" + strip};
string[] Metric = MajesticFunctions.MajesticChecker(URLtests);
var userId = User.Identity.GetHashCode();
var UserTableID = 1;
var newclient = new Client
{
clientN = item.clientN,
homePage = Metric[0],
clientEmail = item.clientEmail,
monthlyQuota = item.monthlyQuota,
TrustFlow = Int32.Parse(Metric[1]),
CitationFlow = Int32.Parse(Metric[2]),
RI = Int32.Parse(Metric[3]),
MJTopicsID = item.MJTopicsID,
UserTableID = UserTableID
};
ViewBag.newdomain = newclient;
return RedirectToAction("Index");
}
}
return RedirectToAction("Index");
}
答案 0 :(得分:1)
您是否尝试过错误消息中提到的建议? 像这样?
csv.configuration.HeaderValidated = null;
答案 1 :(得分:0)
确保同时包含以下两行:
csv.Configuration.HeaderValidated = null;
csv.Configuration.MissingFieldFound = null;