2013-01-07 93 views
1

我試圖從我的web應用程序中將數據從數據表插入Excel文件。在按下按鈕時,數據表被填充並被插入到Excel文件中。問題是,當數據插入正確,我保存該文件,然後關閉它,數據消失。當Excel文檔關閉時會發生這種情況。無法使用asp.net保存Excel文件

這是我的代碼:

public bool ExportDataTableToExcel(DataTable dt, string filepath) 
{ 

    Excel.Application oXL; 
    Excel.Workbook oWB; 
    Excel.Worksheet oSheet; 
    Excel.Range oRange; 

    try 
    { 
     // Start Excel and get Application object. 
     oXL = new Excel.Application(); 

     // Set some properties 
     oXL.Visible = true; 
     oXL.DisplayAlerts = false; 

     // Get a new workbook. 
     oWB = oXL.Workbooks.Add(Missing.Value); 

     // Get the Active sheet 
     oSheet = (Excel.Worksheet)oWB.ActiveSheet; 
     oSheet.Name = "Data"; 

     int rowCount = 1; 
     foreach (DataRow dr in dt.Rows) 
     { 
      rowCount += 1; 
      for (int i = 1; i < dt.Columns.Count + 1; i++) 
      { 
       // Add the header the first time through 
       if (rowCount == 2) 
       { 
        oSheet.Cells[1, 1] = "CustID"; 
        oSheet.Cells[1, 2] = "Alias"; 
        oSheet.Cells[1, 3] = "AdrID"; 
        oSheet.Cells[1, 4] = "Truck Delivery Days"; 
        oSheet.Cells[1, 5] = "Truck Delivery Hours"; 
        oSheet.Cells[1, 6] = "Truck Delivery Type"; 
        oSheet.Cells[1, 7] = "Plane Delivery Days"; 
        oSheet.Cells[1, 8] = "Plane Delivery Hours"; 
        oSheet.Cells[1, 9] = "Plane Delivery Type"; 
        oSheet.Cells[1, 10] = "Other Delivery Days"; 
        oSheet.Cells[1, 11] = "Other Delivery Hours"; 
        oSheet.Cells[1, 12] = "Other Delivery Type"; 
        oSheet.Cells[1, 13] = "Distance to Dealer"; 
       } 
       oSheet.Cells[rowCount, i] = dr[i - 1].ToString(); 
      } 
     } 
     oRange = oSheet.Range[oSheet.Cells[1, 1],oSheet.Cells[rowCount, dt.Columns.Count]]; 
     oRange.EntireColumn.AutoFit(); 

     // Save the sheet and close 
     oSheet = null; 
     oRange = null; 
     oWB.SaveAs(filepath, Excel.XlFileFormat.xlWorkbookNormal, 
      Missing.Value, Missing.Value, Missing.Value, Missing.Value, 
      Excel.XlSaveAsAccessMode.xlExclusive, 
      Missing.Value, Missing.Value, Missing.Value, 
      Missing.Value, Missing.Value); 
     oWB.Close(Missing.Value, Missing.Value, Missing.Value); 
     oWB = null; 
     oXL.Quit(); 
    } 
    catch 
    { 
     throw; 
    } 
    finally 
    { 
     // Clean up 
     // NOTE: When in release mode, this does the trick 
     GC.WaitForPendingFinalizers(); 
     GC.Collect(); 
     GC.WaitForPendingFinalizers(); 
     GC.Collect(); 
    } 

    return true; 
} 

,執行這條線後的數據消失:

oWB.Close(Missing.Value, Missing.Value, Missing.Value); 

任何人可以幫助我嗎?

感謝

+1

請你幫個忙,並使用NPOI。 http://npoi.codeplex.com/releases/view/49524 –

回答

6

Interop is NOT supported in sever-scenarios (like ASP...) by MS

有很多選擇閱讀/編輯/製作的Excel文件,而不互操作/在服務器上安裝的Excel:

MS提供免費的OpenXML SDK 2.0版 - 看到http://msdn.microsoft.com/en-us/library/bb448854%28office.14%29.aspx(XLSX只)

這可以讀取+寫入MS Office文件(包括Excel)。

另一種自由選擇看http://www.codeproject.com/KB/office/OpenXML.aspx(XLSX只)

如果您需要更多像處理舊的Excel版本(如XLS,不僅XLSX),渲染,創建PDF,公式等,則有不同的免費和商業圖書館像ClosedXML(免費,僅XLSX),EPPlus(免費,XLSX只),Aspose.CellsSpreadsheetGearLibXLFlexcel