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

Asp.net mvc 要使用ASP.Net读取Excel文件,必须在服务器上安装Excel吗?,asp.net-mvc,excel,Asp.net Mvc,Excel,我将一个Excel文件从ASP:NETMVC读取到localhost,没有任何问题。但是当你发布错误的网站时。服务器上未安装office或任何Excel库。这可能是问题所在吗??或者可能是??。 谢谢你的回答 *不要使用任何库,通过OLEDB连接读取Excel文件。我的代码如下: //the error occurs here, but only published on the website: strConex = ConfigurationManager.ConnectionStrings

我将一个Excel文件从ASP:NETMVC读取到localhost,没有任何问题。但是当你发布错误的网站时。服务器上未安装office或任何Excel库。这可能是问题所在吗??或者可能是??。 谢谢你的回答

*不要使用任何库,通过OLEDB连接读取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)
System.NullReferenceException:对象引用未设置为对象的实例。在C:\Corp\PublicAriManagua\BusinessServices\RetentionAgent\RetentionAgentService.cs中的BusinessServices.RetentionsByAgentExcel(字符串路径、字符串ID卡、字符串用户、数据表dtError、日期时间初始日期、日期时间结束日期、Guid和主ID):PublicARI.Controllers.AgentRetentionAgent\RetentionAgentService.cs的第342行C:\Corp\PublicAriManagua\PublicARI\Controllers\AgentRetentionController.cs:System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext ControllerContext,IDictionary`2参数)中lambda_方法(Closure,ControllerBase,Object[])的第496行位于System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext ControllerContext,ActionDescriptor ActionDescriptor,IDictionary`2参数)的System.Web.Mvc.ControllerActionInvoker.c_uuDisplayClass15.b_uuu12()位于System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter在System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodWithFilters(ControllerContext ControllerContext,IList`1 filters,ActionDescriptor ActionDescriptor,IDictionary`2参数)的System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodWithFilters(ControllerContext ControllerContext,IList`1 filters,ActionDescriptor ActionDescriptor,IDictionary`2参数)(ControllerContext ControllerContext,字符串actionName)

只需检查用于读取excel的dll是否也会进入服务器bin文件夹。顺便说一句,您使用什么来读取?

我正在使用,它非常好,下面是将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;

}
//
///将XLSX工作表导出到DataTable
/// 
///Excel文件
///出口单
///Excel的第一行是标题
///具有选定图纸数据的DataTable
私有数据表XlsxToDataTable(FileInfo Excel文件、字符串sheetName、bool FirstRowisheder)
{
ExcelPackage ePack=新的ExcelPackage(excelFile);
Excel工作表ws=ePack.Workbook.Worksheets[sheetName];
DataTable结果=新DataTable();
///标题

对于(int xx=1;xx您能否提供有关如何在ASP.NET应用程序中读取Excel文件的更多信息?您是否使用第三方库?开源项目?办公自动化?其他内容?如果您使用COM Interop读取Excel,您肯定需要在服务器中安装Excel?您能否推荐另一种读取Excel文件的方法ile与库或dllI相信[this post][1]是相关的,可以解决您的问题。[1]:您能推荐另一种使用库或DLL读取文件的方法吗?使用此库,我需要在服务器上安装Excel?