使用ASP.Net读取Excel文件必须在服务器上安装Excel吗?

我从ASP:NET MVC到本地主机读取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) 

只要检查Excel的阅读DLL也上升到服务器bin文件夹。 你用什么来阅读btw?

我正在使用EPPlus ,这是非常好的,这里是将Excel表导出到DataTable的方法的示例:

 /// <summary> /// exports XLSX sheet to DataTable /// </summary> /// <param name="excelFile">Excel file</param> /// <param name="sheetName">Sheet for export</param> /// <param name="firstRowIsHeader">Excel first row is header</param> /// <returns>DataTable with selected sheet data</returns> private DataTable XlsxToDataTable(FileInfo excelFile, string sheetName, bool firstRowIsHeader) { ExcelPackage ePack = new ExcelPackage(excelFile); ExcelWorksheet ws = ePack.Workbook.Worksheets[sheetName]; DataTable result = new DataTable(); /// header for (int xx = 1; xx <= ws.Dimension.End.Column; xx++) { string data = ""; if (ws.Cells[1, xx].Value != null) data = ws.Cells[1, xx].Value.ToString(); string columnName = firstRowIsHeader ? data : "Column" + xx.ToString(); result.Columns.Add(columnName); } /// data int first = firstRowIsHeader ? 2 : 1; for (int excelRow = first; excelRow <= ws.Dimension.End.Row; excelRow++) { DataRow rw = result.NewRow(); result.Rows.Add(rw); for (int excelCol = 1; excelCol <= ws.Dimension.End.Column; excelCol++) { string data = ""; if (ws.Cells[excelRow, excelCol].Value != null) data = ws.Cells[excelRow, excelCol].Value.ToString(); rw[excelCol-1] = data; } } return result; }