Hi

I used below coding for create a excel sheet.I got from on web site.
In below coding there is no problem.Its create the excel file.If i run second time,its ask to overwrite the current excel file.If I said "Yes" then it ok ,but If I select cancel button,its generate the Error message.The exception is
exception for HRESULT:.x800A03EC

using System;
using System.Windows.Forms;
using Excel = Microsoft.Office.Interop.Excel; 

namespace WindowsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Excel.Application xlApp ;
            Excel.Workbook xlWorkBook ;
            Excel.Worksheet xlWorkSheet ;
            object misValue = System.Reflection.Missing.Value;

            xlApp = new Excel.ApplicationClass();
            xlWorkBook = xlApp.Workbooks.Add(misValue);

            xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
            xlWorkSheet.Cells[1, 1] = "";

            xlWorkBook.SaveAs("csharp-Excel.xls", Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
            xlWorkBook.Close(true, misValue, misValue);
            xlApp.Quit();

            releaseObject(xlWorkSheet);
            releaseObject(xlWorkBook);
            releaseObject(xlApp);

            MessageBox.Show("Excel file created , you can find the file c:\\csharp-Excel.xls");
        }

        private void releaseObject(object obj)
        {
            try
            {
                System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
                obj = null;
            }
            catch (Exception ex)
            {
                obj = null;
                MessageBox.Show("Exception Occured while releasing object " + ex.ToString());
            }
            finally
            {
                GC.Collect();
            }
        }
    }
}

Dani AI

Generated

A few targeted notes that build on and and that also answer 's later error.

Excel SaveAs and the 0x800A03EC COMException

  • The SaveAs call will trigger Excel UI; if the user cancels the confirmation Excel surfaces a COMException. Suppress the prompt with Application.DisplayAlerts = false, or use Workbook.SaveCopyAs when a simple copy/backup (no format change) is acceptable. Catch and handle the COMException (check ex.ErrorCode == 0x800A03EC) so code can continue cleanly. See the DisplayAlerts docs and the SaveCopyAs docs. (learn.microsoft.com)

Example (safe pattern — do not force GC.Collect):

// disable prompts, try to save, always clean up with FinalReleaseComObject
excelApp.DisplayAlerts = false;
try {
  workbook.SaveAs(path);
}
catch (System.Runtime.InteropServices.COMException ex) when ((uint)ex.ErrorCode == 0x800A03ECu) {
  // Save canceled or Excel signaled failure — handle gracefully
}
finally {
  workbook.Close(false);
  excelApp.Quit();
  System.Runtime.InteropServices.Marshal.FinalReleaseComObject(worksheet);
  System.Runtime.InteropServices.Marshal.FinalReleaseComObject(workbook);
  System.Runtime.InteropServices.Marshal.FinalReleaseComObject(excelApp);
}

TYPE_E_INVDATAREAD (0x80028018) on Workbooks.Add

  • That specific “Old format or invalid type library” error often means Excel expects a different LCID than the process thread (regional/language mismatch). Microsoft documents workarounds: install the Office language pack for the OS locale, run the Excel call under a matching CultureInfo, or invoke the method with reflection specifying culture. (support.microsoft.com)

Best practices and alternatives

  • Do not call GC.Collect() to “fix” COM cleanup; prefer Marshal.FinalReleaseComObject and nulling references. Microsoft warns against automating Office in unattended server processes — prefer server-safe libraries for web/server scenarios such as the Open XML SDK or ClosedXML when possible. (support.microsoft.com)

References:

(These points elaborate on 's comment about SaveAs behavior and address 's 0x80028018 symptom with the Microsoft-recommended fixes.)

Recommended Answers

All 4 Replies

You should never call the garbage collector from code: GC.Collect(); .

Also that is the expected behavior of the Excel assembly. Think about it -- you're calling SaveAs() which is a void method meaning you can't return a value. If you call SaveAs() then you expect the file to be saved. However since Excel flashes a prompt and they can override the SaveAs it throws an exception to signal the caller that the file was not saved, and they should do something about it.

Take a look at these threads regarding excel and SaveAs():
http://www.daniweb.com/forums/thread208167.html
http://www.daniweb.com/forums/thread196171.html

private void button4_Click(object sender, EventArgs e)
    {
      Excel.Application xlApp = default(Excel.Application);
      Excel.Workbook xlWorkBook = default(Excel.Workbook);
      Excel.Worksheet xlWorkSheet = default(Excel.Worksheet);

      const string fName = @"C:\csharp-Excel.xls";

      try
      {
        object misValue = System.Reflection.Missing.Value;

        xlApp = new Excel.ApplicationClass();
        xlWorkBook = xlApp.Workbooks.Add(misValue);

        xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
        xlWorkSheet.Cells[1, 1] = "";

        xlWorkBook.SaveAs(fName, Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
        xlWorkBook.Close(true, misValue, misValue);
        xlApp.Quit();

        releaseObject(xlWorkSheet);
        releaseObject(xlWorkBook);
        releaseObject(xlApp);
      }
      finally
      {
        if (xlApp != null)
          releaseObject(xlApp);
        if (xlWorkBook != null)
          releaseObject(xlWorkBook);
        if (xlWorkSheet != null)
          releaseObject(xlWorkSheet);
      }

      if (System.IO.File.Exists(fName))
      {
        if (MessageBox.Show("Would you like to open the excel file?", this.Text, MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
        {
          try
          {
            System.Diagnostics.Process.Start(fName);
          }
          catch (Exception ex)
          {
            MessageBox.Show("Error opening the excel file." + Environment.NewLine +
              ex.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
          }
        }
      }
    }

    private void releaseObject(object obj)
    {
      if (obj == null)
        throw new ArgumentNullException("obj");
      try
      {
        System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
      }
      catch { }
    }

IM having a problem with

xlWorkBook = xlApp.Workbooks.Add(misValue);

error ==> Old format or invalid type library. (Exception from HRESULT: 0x80028018 (TYPE_E_INVDATAREAD))

how can i fix it?

By posing your question in a new thread and not resurecting and old solved one.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.