111,092
社区成员




public static DataTable GetTableFromExcel(string sheetName, string filePath)
{
string connStrTemplate = "";
filePath = HttpContext.Current.Server.MapPath(filePath);
string fileType = System.IO.Path.GetExtension(filePath);
if (string.IsNullOrEmpty(fileType)) return null;
if (fileType == ".xls")
connStrTemplate = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filePath + ";Extended Properties='Excel 8.0;HDR=YES;IMEX=1'";
else
connStrTemplate = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filePath + ";Extended Properties='Excel 12.0;HDR=YES;IMEX=1'";
DataTable dt = null;
if (!System.IO.File.Exists(filePath))
{
// don't find file
return null;
}
OleDbConnection conn = new OleDbConnection(connStrTemplate);
try
{
conn.Open();
if (sheetName == null || sheetName.Trim().Length == 0)
{
DataTable schemaTable = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
sheetName = schemaTable.Rows[0]["TABLE_NAME"].ToString().Trim();
}
else
{
sheetName += "$";
}
string strSQL = "Select * From [" + sheetName + "]";
OleDbDataAdapter da = new OleDbDataAdapter(strSQL, conn);
DataSet ds = new DataSet();
da.Fill(ds);
dt = ds.Tables[0];
}
catch (Exception ex)
{
throw ex;//这里偶尔会抛出异常,但是对同一份Excel文件,大多数时候是正确的
}
finally
{ // 关闭连接
if (conn.State == ConnectionState.Open)
{
conn.Close();
conn.Dispose();
}
}
return dt;
}
用户代码未处理 System.Data.OleDb.OleDbException
HResult=-2147467259
Message=外部表不是预期的格式。
Source=Microsoft Access Database Engine
ErrorCode=-2147467259
StackTrace:
在 RecruitProject.Util.ParseXlsx.GetTableFromExcel(String sheetName, String filePath) 位置 f:\workspace\MVCProject\RecruitProject\RecruitProject\Util\ParseXlsx.cs:行号 55
在 RecruitProject.Util.ParseXlsx.NormalToDb(String filePath) 位置 f:\workspace\MVCProject\RecruitProject\RecruitProject\Util\ParseXlsx.cs:行号 76
在 RecruitProject.Controllers.JobController.AutoAdd(Nullable`1 category) 位置 f:\workspace\MVCProject\RecruitProject\RecruitProject\Controllers\JobController.cs:行号 76
在 lambda_method(Closure , ControllerBase , Object[] )
在 System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters)
在 System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters)
在 System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters)
在 System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass42.<BeginInvokeSynchronousActionMethod>b__41()
在 System.Web.Mvc.Async.AsyncResultWrapper.<>c__DisplayClass8`1.<BeginSynchronous>b__7(IAsyncResult _)
在 System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult`1.End()
在 System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult)
在 System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass37.<>c__DisplayClass39.<BeginInvokeActionMethodWithFilters>b__33()
在 System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass4f.<InvokeActionMethodFilterAsynchronously>b__49()
InnerException: