【发布时间】:2012-07-15 05:01:46
【问题描述】:
我从 ASP:NET MVC 到 localhost 读取 Excel 文件没有问题。但是当你发布网站时出错。在服务器上没有安装 office 或任何 Excel 库。会不会是这个问题??或者可能是?? 谢谢你的回答
*不使用任何库,通过连接 OleDbConnection 读取 Excel 文件。我的代码如下:
//the error occurs here, but only published on the website:
strConex = ConfigurationManager.ConnectionStrings["Excel03ConString"].ConnectionString;
/**strConex = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:copia.xlsx;
Extended Properties='Excel 12.0;HDR=YES;'"**/
OleDbConnection connection = new OleDbConnection(strConex);
OleDbCommand command = new OleDbCommand();
OleDbDataAdapter adapter = new OleDbDataAdapter();
command.Connection = connection;
connection.Open();
DataTable dtschema = connection.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
String nameSheet = dtschema.Rows[0]["TABLE_NAME"].ToString();
connection.Close();
connection.Open();
command.CommandText = "SELECT * From [" + nameSheet + "]";
adapter.SelectCommand = command;
adapter.Fill(dt);
connection.Close();
以下异常的结果:
System.NullReferenceException: Object reference not set to an instance of an object. at BusinessServices.RetentionAgentService.CreateRetentionsByAgentExcel(String path, String idcard, String user, DataTable dtError, DateTime initialDate, DateTime endDate, Guid& masterId) in C:\Corp\PublicAriManagua\BusinessServices\RetentionAgent\RetentionAgentService.cs:line 342 at PublicARI.Controllers.AgentRetentionController.SaveRetentions(HttpPostedFileBase file, RetentionAgentDeclaration retAgent) in C:\Corp\PublicAriManagua\PublicARI\Controllers\AgentRetentionController.cs:line 496 at lambda_method(Closure , ControllerBase , Object[] ) 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.ControllerActionInvoker.<>c__DisplayClass15.<InvokeActionMethodWithFilters>b__12() at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func`1 continuation) at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodWithFilters(ControllerContext controllerContext, IList`1 filters, ActionDescriptor actionDescriptor, IDictionary`2 parameters) at System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName)
【问题讨论】:
-
您能否提供更多关于您如何在 ASP.NET 应用程序中读取 Excel 文件的信息?你在使用第三方库吗?一个开源项目?办公自动化?还有什么?
-
如果您使用 COM Interop 读取 excel,您肯定需要在服务器中安装 Excel
-
你能推荐另一种使用库或dll读取文件的方法
-
我相信 [this post][1] 是相关的并且会解决您的问题。 [1]:stackoverflow.com/questions/7143067/…
标签: asp.net-mvc excel