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] = "http://csharp.net-informations.com";

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

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] = "http://csharp.net-informations.com";

        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.