在将excel数据导入到mvc中的db时出错“外部表格不是预期的格式”

我收到一个错误

外部表格不是预期的格式

同时在mvc中导入excel数据到db中。

来自entity framework的表

namespace MyCRS { using System; using System.Collections.Generic; using System.Linq; using System.Web; public partial class tblStoreRequest { public int TransactionId { get; set; } public string DocumentNo { get; set; } public System.DateTime RequestedDate { get; set; } public int StoreId { get; set; } public int ArticleId { get; set; } public int RequestedQty { get; set; } public string Remarks { get; set; } public string EnteredBy { get; set; } public System.DateTime EnteredOn { get; set; } public string UpdatedBy { get; set; } public Nullable<System.DateTime> UpdatedOn { get; set; } public virtual AspNetUser AspNetUser { get; set; } public virtual AspNetUser AspNetUser1 { get; set; } public virtual tblArticle tblArticle { get; set; } public virtual tblStore tblStore { get; set; } } } 

Request.xlsx

(在文本中的数据格式就像进入List<string>

 ## DocumentNo RequestedDate StoreId ArticleId RequestedQty Remarks ## ## REQ-5 02-09-2016 4 4 100 ## 

控制器代码

 using System.Collections.Generic; using System.Data; using System.Data.Entity; using System.Data.Entity.Validation; using System.Data.OleDb; using System.IO; using System.Linq; using System.Net; using System.Text.RegularExpressions; using System.Web; using System.Web.Mvc; using MyCRS.Models; using LinqToExcel; using System.Data.SqlClient; namespace MyCRS.Contollers { public class StoreRequestBulkController : Controller { MyCRSEntities db = new MyCRSEntities(); public ActionResult Index() { return View(); } [HttpPost] public JsonResult UploadExcel(tblStoreRequest storerequests, HttpPostedFileBase FileUpload) { List<string> data = new List<string>(); if (FileUpload != null) { if (FileUpload.ContentType == "application/vnd.ms-excel" || FileUpload.ContentType == "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet") { string filename = FileUpload.FileName; string targetpath = Server.MapPath("~/Doc/"); FileUpload.SaveAs(targetpath + filename); string pathToExcelFile = targetpath + filename; var connectionString = ""; if (filename.EndsWith(".xls")) { connectionString = string.Format("Provider=Microsoft.Jet.OLEDB.4.0; data source={0}; Extended Properties=Excel 8.0;", pathToExcelFile); } else if (filename.EndsWith(".xlsx")) { connectionString = string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=\"Excel 14.0;HDR=YES;IMEX=1\";", pathToExcelFile); } var adapter = new OleDbDataAdapter("SELECT * FROM [Sheet1$]", connectionString); var ds = new DataSet(); adapter.Fill(ds, "ExcelTable"); DataTable dtable = ds.Tables["ExcelTable"]; string sheetName = "Sheet1"; var excelFile = new ExcelQueryFactory(pathToExcelFile); var artistAlbums = from a in excelFile.Worksheet<tblStoreRequest>(sheetName) select a; foreach (var a in artistAlbums) { try { if (a.DocumentNo != null && a.RequestedDate != null) { tblStoreRequest TU = new tblStoreRequest(); TU.DocumentNo = a.DocumentNo; TU.RequestedDate = a.RequestedDate; TU.StoreId = a.StoreId; TU.ArticleId = a.ArticleId; TU.RequestedQty = a.RequestedQty; TU.Remarks = a.Remarks; TU.EnteredBy = Session["U"].ToString(); TU.EnteredOn = DateTime.Now; db.tblStoreRequests.Add(TU); db.SaveChanges(); } else { data.Add("<ul>"); if (a.DocumentNo == "" || a.DocumentNo == null) data.Add("<li> DocumentNo is required</li>"); if (a.RequestedDate.ToString() == "" || a.RequestedDate == null) data.Add("<li> RequestedDate is required</li>"); if (a.RequestedQty.ToString() == "") data.Add("<li> RequestedQty is required</li>"); data.Add("</ul>"); data.ToArray(); return Json(data, JsonRequestBehavior.AllowGet); } } catch (DbEntityValidationException ex) { foreach (var entityValidationErrors in ex.EntityValidationErrors) { foreach (var validationError in entityValidationErrors.ValidationErrors) { Response.Write("Property: " + validationError.PropertyName + " Error: " + validationError.ErrorMessage); } } } } //deleting excel file from folder if ((System.IO.File.Exists(pathToExcelFile))) { System.IO.File.Delete(pathToExcelFile); } return Json("success", JsonRequestBehavior.AllowGet); } else { //alert message for invalid file format data.Add("<ul>"); data.Add("<li>Only Excel file format is allowed</li>"); data.Add("</ul>"); data.ToArray(); return Json(data, JsonRequestBehavior.AllowGet); } } else { data.Add("<ul>"); if (FileUpload == null) data.Add("<li>Please choose Excel file</li>"); data.Add("</ul>"); data.ToArray(); return Json(data, JsonRequestBehavior.AllowGet); } } } } 

视图

 @{ ViewBag.Title = "Index"; } <h4>Add Users via Excel</h4> <hr /> @using (Html.BeginForm("UploadExcel", "StoreRequestBulk", FormMethod.Post, new { enctype = "multipart/form-data", onsubmit = "return myFunction()" })) { <div class="form-horizontal"> <div class="form-group"> <div class="control-label col-md-2">Download Format:</div> <div class="col-md-10"> <a href="/StoreRequestBulk/DownloadExcel/"><img src="~/excel.ico" width="25" height="25" title="Download Excel format" alt="excel" /></a> </div> </div> <div class="form-group"> <div class="control-label col-md-2">Excel:</div> <div class="col-md-10"> <input type="file" id="FileUpload" name="FileUpload" class="" /> </div> </div> <div class="form-group"> <div class="col-md-offset-2 col-md-10"> <input type="submit" value="Upload" id="btnSubmit" class="btn btn-primary" /> </div> </div> </div> }