1

I want to delete content of an Excel file from my web application but I have some errors, I have used this code:

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();
}

but I'm having this error when I execute the application:

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)).

1 Answer 1

1

Can you try using something like this? Got code from this question, so it is not tested

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;

But if it fails, you could eventually delete all worksheets, and just add new ones?

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.