1

I read an Excel file from ASP: NET MVC to localhost without problems. But when you publish the site on error. On the server is not installed the office or any Excel library. Could this be the problem?? Or could it be??. Thanks for your answers

*Do not use any library, read the Excel file through a connection OleDbConnection. My code is is the following:

//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();

The result in the following exception:

   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)
tereško
  • 58,060
  • 25
  • 98
  • 150
jorcast
  • 11
  • 3
  • 1
    Can you provide more information on how you're reading the Excel file in your ASP.NET application? Are you using a third-party library? An open-source project? Office automation? Something else? – Scott Mitchell Mar 28 '12 at 22:26
  • If you are using COM Interop to read excel you will definetly need Excel installed in the server – Emmanuel N Mar 28 '12 at 22:40
  • Can you recommend another way to read the file with a library or dll – jorcast Mar 28 '12 at 23:06
  • I believe [this post][1] is related and will resolve your issue. [1]: http://stackoverflow.com/questions/7143067/shall-i-read-an-excel-file-via-oledb-jet4-0-and-save-into-dataset – Mike Mar 29 '12 at 00:20

2 Answers2

0

Just check that the dll for the reading of excel is also going up to the server bin folder. What are you using to read btw?

The Angry Saxon
  • 792
  • 2
  • 7
  • 24
0

I am using EPPlus, it's really good, here is the example of method that exports excel sheet to 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;

}
Antonio Bakula
  • 20,445
  • 6
  • 75
  • 102