2017-07-19 12 views
0

I Inhalt einer Excel-Datei von meiner Web-Anwendung löschen wollen, aber ich habe einige Fehler habe ich diesen Code verwendet:löschen Excel Inhalt C#

var excel = new Application(); 
var workbook = excel.Workbooks.Open(ExcelFileName, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing); 
try 
{ 
    foreach (dynamic worksheet in workbook.Worksheets) 
    { 
     worksheet.Cells.ClearContents(); 
    } 
    workbook.Save(); 
} 
finally 
{ 
    workbook.Close(); 
    excel.Quit(); 
} 

aber ich habe diesen Fehler, wenn ich die execute Anwendung:

Unable to cast COM object of type 'Microsoft.Office.Interop.Excel.ApplicationClass' to interface type 'Microsoft.Office.Interop.Excel._Application'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{000208D5-0000-0000-C000-000000000046}' failed due to the following error: Error loading type library/DLL. (Exception from HRESULT: 0x80029C4A (TYPE_E_CANTLOADLIBRARY)).

Antwort

0

Können Sie versuchen, etwas wie das zu verwenden? Got Code aus diesem question, so ist es nicht

excel.DisplayAlerts = false; 
for (int i = excel.ActiveWorkbook.Worksheets.Count; i > 0 ; i--) 
{ 
    Worksheet worksheet = (Worksheet)excel.ActiveWorkbook.Worksheets[i]; 
    worksheet.Cells.ClearContents();   
} 
excel.DisplayAlerts = true; 

getestet, aber wenn es scheitert, könnte man schließlich alle Arbeitsblätter löschen, und nur neue hinzufügen?