本文介绍了初始化字符串的格式不符合从索引0开始的规范。在MVC 5中,即时得到此错误,请帮助我。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞!public JsonResult UploadExcel(DepartmentVM deptVM, HttpPostedFileBase FileUpload) { List<string> data = new List<string>(); if(FileUpload != null) { // tdata.ExecuteCommand("truncate table OtherCompanyAssets"); if (FileUpload.ContentType == "application/vnd.ms-excel" || FileUpload.ContentType == "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet") { string filename = FileUpload.FileName; string targetpath = Server.MapPath("~/Docs/"); FileUpload.SaveAs(targetpath + filename); string pathToExcelFile = targetpath + filename; var connectionString = ""; if (filename.EndsWith(".xls")) { connectionString = string.Format(@"C:\src\RedirectApplication\RedirectApplication\301s.xlsx", pathToExcelFile); // public static string path = @"C:\src\RedirectApplication\RedirectApplication\301s.xlsx"; // public static string connStr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties=Excel 12.0;"; } else if (filename.EndsWith(".xlsx")) { connectionString = string.Format("Provider= Microsoft.ACE.OLEDB.12.0; Data Source = " + pathToExcelFile + "; Extended Properties = Excel 12.0;HDR=Yes", pathToExcelFile); } var adapter = new OleDbDataAdapter("SELECT * FROM [DepartmentVMs$]", connectionString); // Error on this line var ds = new DataSet(); adapter.Fill(ds, "DepartmentDetails"); DataTable dtable = ds.Tables["DepartmentDetails"]; string sheetName = "DepartmentVMs"; var excelFile = new ExcelQueryFactory(pathToExcelFile); var artistAlbums = from a in excelFile.Worksheet<DepartmentVM>(sheetName) select a; foreach (var a in artistAlbums) { try { if (a.deptCode != "" && a.deptName != "" && a.deptAdditionalInfo!="" && a.deptIsActive!= true) { DepartmentVM dvm = new DepartmentVM(); dvm.deptCode = a.deptCode; dvm.deptName = a.deptName; dvm.deptAdditionalInfo = a.deptAdditionalInfo; dvm.deptIsActive = a.deptIsActive; db.DepartmentVMs.Add(dvm); db.SaveChanges(); } else { data.Add("<ul>"); if (a.deptCode == "" || a.deptCode == null) data.Add("<li> Code is required</li>"); if (a.deptName == "" || a.deptName == null) data.Add("<li> Name is required</li>"); if (a.deptAdditionalInfo == "" || a.deptAdditionalInfo == null) data.Add("<li> Additional Info is required</li>"); if (a.deptIsActive == true || a.deptIsActive == false) data.Add("<li> Is Active status 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); } } 我尝试了什么:What I have tried:[HttpPost] public JsonResult UploadExcel(DepartmentVM deptVM, HttpPostedFileBase FileUpload) { List<string> data = new List<string>(); if(FileUpload != null) { // tdata.ExecuteCommand("truncate table OtherCompanyAssets"); if (FileUpload.ContentType == "application/vnd.ms-excel" || FileUpload.ContentType == "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet") { string filename = FileUpload.FileName; string targetpath = Server.MapPath("~/Docs/"); FileUpload.SaveAs(targetpath + filename); string pathToExcelFile = targetpath + filename; var connectionString = ""; if (filename.EndsWith(".xls")) { connectionString = string.Format(@"C:\src\RedirectApplication\RedirectApplication\301s.xlsx", pathToExcelFile); // public static string path = @"C:\src\RedirectApplication\RedirectApplication\301s.xlsx"; // public static string connStr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties=Excel 12.0;"; } else if (filename.EndsWith(".xlsx")) { connectionString = string.Format("Provider= Microsoft.ACE.OLEDB.12.0; Data Source = " + pathToExcelFile + "; Extended Properties = Excel 12.0;HDR=Yes", pathToExcelFile); } var adapter = new OleDbDataAdapter("SELECT * FROM [DepartmentVMs$]", connectionString); // Error on this line var ds = new DataSet(); adapter.Fill(ds, "DepartmentDetails"); DataTable dtable = ds.Tables["DepartmentDetails"]; string sheetName = "DepartmentVMs"; var excelFile = new ExcelQueryFactory(pathToExcelFile); var artistAlbums = from a in excelFile.Worksheet<DepartmentVM>(sheetName) select a; foreach (var a in artistAlbums) { try { if (a.deptCode != "" && a.deptName != "" && a.deptAdditionalInfo!="" && a.deptIsActive!= true) { DepartmentVM dvm = new DepartmentVM(); dvm.deptCode = a.deptCode; dvm.deptName = a.deptName; dvm.deptAdditionalInfo = a.deptAdditionalInfo; dvm.deptIsActive = a.deptIsActive; db.DepartmentVMs.Add(dvm); db.SaveChanges(); } else { data.Add("<ul>"); if (a.deptCode == "" || a.deptCode == null) data.Add("<li> Code is required</li>"); if (a.deptName == "" || a.deptName == null) data.Add("<li> Name is required</li>"); if (a.deptAdditionalInfo == "" || a.deptAdditionalInfo == null) data.Add("<li> Additional Info is required</li>"); if (a.deptIsActive == true || a.deptIsActive == false) data.Add("<li> Is Active status 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); } }推荐答案,connectionString); // 此行出错 var ds = new DataSet(); adapter.Fill(ds, DepartmentDetails); DataTable dtable = ds.Tables [ DepartmentDetails]; string sheetName = DepartmentVMs; var excelFile = new ExcelQueryFactory(pathToExcelFile ); var artistAlbums = 来自 excelFile.Worksheet< DepartmentVM>(sheetName)选择 a; foreach ( var a in artistAlbums) { try { if (a.deptCode!= && a.deptName!= && a.deptAdditionalInfo!= && a.deptIsActive!= true ) { DepartmentVM dvm = new DepartmentVM(); dvm.deptCode = a.deptCode; dvm.deptName = a.deptName; dvm.deptAdditionalInfo = a.deptAdditionalInfo; dvm.deptIsActive = a.deptIsActive; db.DepartmentVMs.Add(dvm); db.SaveChanges(); } else { data.Add( < ul>); if (a.deptCode == || a.deptCode == null )data.Add( < li>代码是必需的< / li>); if (a.deptName == || a.deptName == null )data.Add( < li>名称是必需的< / li>); if (a.deptAdditionalInfo == || a.deptAdditionalInfo == null )data.Add( < li>需要其他信息< / li>); if (a.deptIsActive == true || a.deptIsActive == false )data.Add( < 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( 属性: + validationError .PropertyName + 错误: + validationError.ErrorMessage); } } } } // 从文件夹中删除excel文件 如果((System.IO.File.Exists(pathToExcelFile))) { System.IO.File.Delete(pathToExcelFile); } return Json( 成功,JsonRequestBehavior.AllowGet); } 其他 { // 无效文件格式的警报消息 data.Add( < UL> 中); data.Add( < li>仅允许使用Excel文件格式< / li>); data.Add( < / ul>); data.ToArray(); return Json(data,JsonRequestBehavior.AllowGet); } } 其他 { data.Add( < ul>); if (FileUpload == null )data.Add( < li>请选择Excel文件< / li>); data.Add( < / ul>); data.ToArray(); return Json(data,JsonRequestBehavior.AllowGet); } }", connectionString); // Error on this line var ds = new DataSet(); adapter.Fill(ds, "DepartmentDetails"); DataTable dtable = ds.Tables["DepartmentDetails"]; string sheetName = "DepartmentVMs"; var excelFile = new ExcelQueryFactory(pathToExcelFile); var artistAlbums = from a in excelFile.Worksheet<DepartmentVM>(sheetName) select a; foreach (var a in artistAlbums) { try { if (a.deptCode != "" && a.deptName != "" && a.deptAdditionalInfo!="" && a.deptIsActive!= true) { DepartmentVM dvm = new DepartmentVM(); dvm.deptCode = a.deptCode; dvm.deptName = a.deptName; dvm.deptAdditionalInfo = a.deptAdditionalInfo; dvm.deptIsActive = a.deptIsActive; db.DepartmentVMs.Add(dvm); db.SaveChanges(); } else { data.Add("<ul>"); if (a.deptCode == "" || a.deptCode == null) data.Add("<li> Code is required</li>"); if (a.deptName == "" || a.deptName == null) data.Add("<li> Name is required</li>"); if (a.deptAdditionalInfo == "" || a.deptAdditionalInfo == null) data.Add("<li> Additional Info is required</li>"); if (a.deptIsActive == true || a.deptIsActive == false) data.Add("<li> Is Active status 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); } } 我尝试了什么:What I have tried:[HttpPost] public JsonResult UploadExcel(DepartmentVM deptVM, HttpPostedFileBase FileUpload) { List<string> data = new List<string>(); if(FileUpload != null) { // tdata.ExecuteCommand("truncate table OtherCompanyAssets"); if (FileUpload.ContentType == "application/vnd.ms-excel" || FileUpload.ContentType == "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet") { string filename = FileUpload.FileName; string targetpath = Server.MapPath("~/Docs/"); FileUpload.SaveAs(targetpath + filename); string pathToExcelFile = targetpath + filename; var connectionString = ""; if (filename.EndsWith(".xls")) { connectionString = string.Format(@"C:\src\RedirectApplication\RedirectApplication\301s.xlsx", pathToExcelFile); // public static string path = @"C:\src\RedirectApplication\RedirectApplication\301s.xlsx"; // public static string connStr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties=Excel 12.0;"; } else if (filename.EndsWith(".xlsx")) { connectionString = string.Format("Provider= Microsoft.ACE.OLEDB.12.0; Data Source = " + pathToExcelFile + "; Extended Properties = Excel 12.0;HDR=Yes", pathToExcelFile); } var adapter = new OleDbDataAdapter("SELECT * FROM [DepartmentVMs,connectionString); //此行出错 var ds = new DataSet(); adapter.Fill(ds,DepartmentDetails); DataTable dtable = ds.Tables [DepartmentDetails]; string sheetName =DepartmentVMs; var excelFile = new ExcelQueryFactory(pathToExcelFile); var artistAlbums =来自excelFile.Worksheet中的< DepartmentVM>(sheetName)选择一个; foreach(艺术家阿尔法中的var a) {尝试 { if(a.deptCode!=&& a.deptName!= && a.deptAdditionalInfo!=&& a.deptIsActive!= true) { DepartmentVM dvm = new DepartmentVM(); dvm.deptCode = a.deptCode; dvm.deptName = a.deptName; dvm.deptAdditionalInfo = a.deptAdditionalInfo; dvm.deptIsActive = a.deptIsActive; db.DepartmentVMs.Add(dvm); db.SaveChanges(); } 其他 { data.Add(< ul>); if(a.deptCode ==|| a.deptCode == null)data.Add(< li> Code is required< / li>); if(a.deptName ==|| a.deptName == null)data.Add(< li> Name is required< / li>); if(a.deptAdditionalInfo ==|| a.deptAdditionalInfo == null)data.Add(< li>附加信息是必需的< / li>); if(a.deptIsActive == true || a.deptIsActive == false)data.Add(< li>是否需要活动状态< / li>); data.Add(< / ul>); data.ToArray(); 返回Json(data,JsonRequestBehavior.AllowGet); } } catch(DbEntityValidationException ex) { foreach(ex.EntityValidationErrors中的var entityValidationErrors) { foreach(var validationError) in entityValidationErrors.ValidationErrors) { Response.Write(Property:+ validationError.PropertyName +Error:+ validationError.ErrorMessage); } } } } //从文件夹中删除excel文件if((System.IO.File.Exists(pathToExcelFile))) { System.IO.File.Delete(pathToExcelFile); } 返回Json(成功,JsonRequestBehavior.AllowGet); } 其他 { //无效文件格式的提醒信息 data.Add(< ul>); data.Add(< li>仅允许Excel文件格式< / li>); data.Add(< / ul>); data.ToArray(); 返回Json(data,JsonRequestBehavior.AllowGet); } } 其他 { data.Add(< ul>); if(FileUpload == null)data.Add(< li>请选择Excel文件< / li>); data.Add(< / ul>); data.ToArray(); 返回Json(data,JsonRequestBehavior.AllowGet); } }", connectionString); // Error on this line var ds = new DataSet(); adapter.Fill(ds, "DepartmentDetails"); DataTable dtable = ds.Tables["DepartmentDetails"]; string sheetName = "DepartmentVMs"; var excelFile = new ExcelQueryFactory(pathToExcelFile); var artistAlbums = from a in excelFile.Worksheet<DepartmentVM>(sheetName) select a; foreach (var a in artistAlbums) { try { if (a.deptCode != "" && a.deptName != "" && a.deptAdditionalInfo!="" && a.deptIsActive!= true) { DepartmentVM dvm = new DepartmentVM(); dvm.deptCode = a.deptCode; dvm.deptName = a.deptName; dvm.deptAdditionalInfo = a.deptAdditionalInfo; dvm.deptIsActive = a.deptIsActive; db.DepartmentVMs.Add(dvm); db.SaveChanges(); } else { data.Add("<ul>"); if (a.deptCode == "" || a.deptCode == null) data.Add("<li> Code is required</li>"); if (a.deptName == "" || a.deptName == null) data.Add("<li> Name is required</li>"); if (a.deptAdditionalInfo == "" || a.deptAdditionalInfo == null) data.Add("<li> Additional Info is required</li>"); if (a.deptIsActive == true || a.deptIsActive == false) data.Add("<li> Is Active status 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); } }首先,请仔细阅读错误消息,您可能会发现您也被告知错误的位置。 通过使用调试器,您还可以看到变量的值。 当你不明白你的代码在做什么或为什么它做它做的事情,答案是调试器。 使用调试器来查看你的代码在做什么。它允许你逐行执行第1行并在执行时检查变量,它是一个令人难以置信的学习工具。 当你不明白你的代码是什么时做或为什么它做它做的,答案是调试器。 使用调试器来查看你的代码在做什么。它允许你逐行执行第1行并在执行时检查变量,它是一个令人难以置信的学习工具。 调试器 - 维基百科,免费的百科全书 [ ^ ] 掌握Visual Studio 2010中的调试 - 初学者指南 [ ^ ] 调试器在这里向您展示您的代码正在做什么,您的任务是与它应该做的比较。 调试器中没有魔法,它没有找到错误,它只是帮助你。当代码没有达到预期的效果时,你就会接近一个错误。First of all, reread the error message carefully and you may discover that you are also told the position of the error.By using the debugger, you will also be able to see the values of the variables.When you don't understand what your code is doing or why it does what it does, the answer is debugger.Use the debugger to see what your code is doing. It allow you to execute lines 1 by 1 and to inspect variables as it execute, it is an incredible learning tool.When you don't understand what your code is doing or why it does what it does, the answer is debugger.Use the debugger to see what your code is doing. It allow you to execute lines 1 by 1 and to inspect variables as it execute, it is an incredible learning tool.Debugger - Wikipedia, the free encyclopedia[^]Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]The debugger is here to show you what your code is doing and your task is to compare with what it should do.There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug. 这篇关于初始化字符串的格式不符合从索引0开始的规范。在MVC 5中,即时得到此错误,请帮助我。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 07-26 05:39