【问题标题】:To read an Excel file with ASP.Net Excel must be installed on the server?用 ASP.Net 读取 Excel 文件必须在服务器上安装 Excel 吗?
【发布时间】: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


【解决方案1】:

只需检查用于读取 excel 的 dll 是否也在服务器 bin 文件夹中。顺便说一句,你用什么来阅读?

【讨论】:

  • 你能推荐另一种使用库或dll读取文件的方法
【解决方案2】:

我用的是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;

}

【讨论】:

  • 有了这个库,我需要在服务器上安装 Excel 吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-08-06
  • 2010-10-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多