2012-03-28 4 views
1

ASP:NET MVCのExcelファイルを問題なくlocalhostに読み込みました。しかし、あなたが間違ってサイトを公開するとき。サーバー上にオフィスまたは任意のExcelライブラリがインストールされていません。これが問題だろうか?それとも? あなたのお返事ありがとうASP.NetでExcelファイルを読むにはExcelがサーバーにインストールされている必要がありますか?

*ライブラリを使用しないで、Excelファイルを接続OleDbConnectionで読み取ってください。私のコードは、次のされている次の例外で

//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) 
+1

は、あなたがあなたのASPでExcelファイルを読んでいる方法に関する詳細な情報を提供することができます。 NETアプリケーション?サードパーティのライブラリを使用していますか?オープンソースプロジェクトですか?オフィスオートメーション?他に何か? –

+0

Excelの読み込みにCOM Interopを使用している場合は、Excelがサーバーにインストールされている必要があります。 –

+0

ライブラリまたはDLLを使用してファイルを読む別の方法をお勧めしますか? – jorcast

答えて

0

エクセルの読書のためのDLLは、サーバのbinフォルダに上がっていることを確認してください。あなたはbtwを読むために何を使用していますか?

+0

ライブラリやDLLでファイルを読む別の方法をお勧めします – jorcast

0

私はEPPlusを使用しています、それは本当に良いですが、ここでは輸出は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; 

} 
+0

このライブラリでは、私はサーバーにExcelをインストールする必要がありますか? – jorcast

+0

いいえ、サーバー上でExcelする必要はありません –

関連する問題