2013-01-07 91 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