在我的MVC 5应用程序中,我使用LinqToExcel将数据从Excel文件导入SQL Server数据库表。以下是获取数据并保存到数据库的代码。
var dataDirectory = new DirectoryInfo(Server.MapPath("~/App_Data/Data/"));
DirectoryInfo dir = new DirectoryInfo(dataDirectory.ToString());//reading the director at a specific path
FileInfo[] info = dir.GetFiles("*.*");//getting the list of files in the director
foreach(FileInfo f in info)
{
dataFileName = f.FullName;//getting full file name
fileExt = f.Extension;//getting the file extension
}
if(fileExt == ".xlsx")
{
var excel = new ExcelQueryFactory();
excel.FileName = dataFileName;
//mapping the database table columns to EXCEL columns
excel.AddMapping<WASA_Bill_Detail>(s => s.AccountNo, "ACCOUNTNO");
excel.AddMapping<WASA_Bill_Detail>(s => s.BillNo, "BILLNO");
excel.AddMapping<WASA_Bill_Detail>(s => s.Category, "CATEGORY");
excel.AddMapping<WASA_Bill_Detail>(s => s.Billing_Period, "BILLING_PERIOD");
excel.AddMapping<WASA_Bill_Detail>(s => s.Name, "NAME");
excel.AddMapping<WASA_Bill_Detail>(s => s.Address, "ADDRESS");
excel.AddMapping<WASA_Bill_Detail>(s => s.Issue_Date, "ISSUE_DATE");
excel.AddMapping<WASA_Bill_Detail>(s => s.Due_Date, "DUE_DATE");
excel.AddMapping<WASA_Bill_Detail>(s => s.Water_Bill, "WATER_BILL");
excel.AddMapping<WASA_Bill_Detail>(s => s.Sewerage_Bill, "SEWERAGE_BILL");
excel.AddMapping<WASA_Bill_Detail>(s => s.Aquifer_Charges, "AQUIFER");
excel.AddMapping<WASA_Bill_Detail>(s => s.Current_Amount, "CURRENT AMOUNT");
excel.AddMapping<WASA_Bill_Detail>(s => s.Arrears, "ARREARS");
excel.AddMapping<WASA_Bill_Detail>(s => s.Service_Charges, "SERVICE CHARGES");
excel.AddMapping<WASA_Bill_Detail>(s => s.Payable_within_DueDate, "PAYABLE WITHIN DUEDATE");
excel.AddMapping<WASA_Bill_Detail>(s => s.Surcharge, "SURCHARGE");
excel.AddMapping<WASA_Bill_Detail>(s => s.Payable_after_DueDate, "AFTER DUE DATE");
excel.AddMapping<WASA_Bill_Detail>(s => s.Payment_History_1, "PAY HISTORY 1");
excel.AddMapping<WASA_Bill_Detail>(s => s.Paid_1, "PAID 1");
excel.AddMapping<WASA_Bill_Detail>(s => s.Payment_History_2, "PAY HISTORY 2");
excel.AddMapping<WASA_Bill_Detail>(s => s.Paid_2, "PAID 2");
excel.AddMapping<WASA_Bill_Detail>(s => s.Payment_History_3, "PAY HISTORY 3");
excel.AddMapping<WASA_Bill_Detail>(s => s.Paid_3, "PAID 3");
excel.AddMapping<WASA_Bill_Detail>(s => s.Area, "AREA");
excel.AddMapping<WASA_Bill_Detail>(s => s.Water_Rate, "WATER RATE");
excel.AddMapping<WASA_Bill_Detail>(s => s.Sewerage_Rate, "SEWER RATE");
excel.AddMapping<WASA_Bill_Detail>(s => s.Discharge_Basis, "DISCHAGE");
excel.AddMapping<WASA_Bill_Detail>(s => s.Pump_Size, "PUMP SIZE");
excel.AddMapping<WASA_Bill_Detail>(s => s.Ferrule_Size, "FERRULE SIZE");
excel.AddMapping<WASA_Bill_Detail>(s => s.Meter_Type, "METER TYPE");
excel.AddMapping<WASA_Bill_Detail>(s => s.Meter_Status, "METER STATUS");
excel.AddMapping<WASA_Bill_Detail>(s => s.Current_Reading, "CURRENT READING");
excel.AddMapping<WASA_Bill_Detail>(s => s.Last_Readin, "LAST READING");
excel.AddMapping<WASA_Bill_Detail>(s => s.Water_Aquiffer_Charges, "WATER AQUIFER CHARGES");
var excelData = from x in excel.Worksheet<WASA_Bill_Detail>() select x;
foreach(var data in excelData)
{
if (ModelState.IsValid)
{
db.WASA_Bill_Detail.Add(wASA_Bill_Detail);
db.SaveChanges();
}
}
return RedirectToAction("Index");
}
在上面的代码中我做了属性映射到excel列。
现在问题是当我试图将数据保存到数据库表中时,发生了“System.FormatException”。例外细节如下
System.FormatException was unhandled by user code
HResult=-2146233033
Message=Input string was not in a correct format.
Source=mscorlib
StackTrace:
at System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal)
at System.Number.ParseDecimal(String value, NumberStyles options, NumberFormatInfo numfmt)
at System.Convert.ToDecimal(String value, IFormatProvider provider)
at System.String.System.IConvertible.ToDecimal(IFormatProvider provider)
at System.Convert.ChangeType(Object value, Type conversionType, IFormatProvider provider)
at System.Convert.ChangeType(Object value, Type conversionType)
at LinqToExcel.Extensions.CommonExtensions.Cast(Object object, Type castType)
at LinqToExcel.Query.ExcelQueryExecutor.GetTypeResults(IDataReader data, IEnumerable`1 columns, QueryModel queryModel)
at LinqToExcel.Query.ExcelQueryExecutor.GetDataResults(SqlParts sql, QueryModel queryModel)
at LinqToExcel.Query.ExcelQueryExecutor.ExecuteCollection[T](QueryModel queryModel)
at Remotion.Data.Linq.Clauses.StreamedData.StreamedSequenceInfo.ExecuteCollectionQueryModel[T](QueryModel queryModel, IQueryExecutor executor)
at Remotion.Data.Linq.Clauses.StreamedData.StreamedSequenceInfo.ExecuteQueryModel(QueryModel queryModel, IQueryExecutor executor)
at Remotion.Data.Linq.QueryModel.Execute(IQueryExecutor executor)
at Remotion.Data.Linq.QueryProviderBase.Execute[TResult](Expression expression)
at Remotion.Data.Linq.QueryableBase`1.GetEnumerator()
at LINQToSQLTest.Controllers.WASA_Bill_DetailController.Create(WASA_Bill_Detail wASA_Bill_Detail, HttpPostedFileBase[] Files) in e:\Projects\DotNet\MVC5\LINQToSQLTest\LINQToSQLTest\Controllers\WASA_Bill_DetailController.cs:line 133
at lambda_method(Closure , ControllerBase , Object[] )
at System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters)
at System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters)
at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters)
at System.Web.Mvc.Async.AsyncControllerActionInvoker.ActionInvocation.InvokeSynchronousActionMethod()
at System.Web.Mvc.Async.AsyncControllerActionInvoker.<BeginInvokeSynchronousActionMethod>b__39(IAsyncResult asyncResult, ActionInvocation innerInvokeState)
at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult`2.CallEndDelegate(IAsyncResult asyncResult)
at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResultBase`1.End()
at System.Web.Mvc.Async.AsyncResultWrapper.End[TResult](IAsyncResult asyncResult, Object tag)
at System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult)
at System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3f()
at System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.<>c__DisplayClass48.<InvokeActionMethodFilterAsynchronouslyRecursive>b__41()
InnerException:
有关上述异常的任何帮助