hy when i run this command to open excel and i get error (on 2007 excel it was fine)

                //Excel.ApplicationClass xlApp;
                Excel.Application  xlApp;

                Excel.Workbook xlWorkBook;

                Excel.Worksheet xlWorkSheet;


                object misValue = System.Reflection.Missing.Value;

                SaveFileDialog oDialog = new SaveFileDialog();

                xlApp = new Excel.ApplicationClass();

               ** xlWorkBook = xlApp.Workbooks.Add(misValue);**

                xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);

                int i = 0;

                int j = 0;

                int z = 1;
                //button1.Visible = true;
                this.Cursor = Cursors.WaitCursor;

                ///////////////////////////////////////////////////////////////////////
                foreach (DataGridViewColumn c in dvRMS.Columns)
                {
                    c.SortMode = DataGridViewColumnSortMode.NotSortable;
                    c.Selected = false;
                }
                dvRMS.SelectionMode = DataGridViewSelectionMode.FullColumnSelect;

                this is the error line::::
                 xlWorkBook = xlApp.Workbooks.Add(misValue);

Hi

What is the actual error that you get?

Also, you might want to consider using ADO.NET to do this which would remove the dependency on using the Excel Interop libraries. I can provide an example if that is of interest to you.

yeah you don't want to be adding to the workbook. Once you have a worksheet (which by default excel creates one), you can write to it, like

xlWorksheet.Cells[y,x] = value;

(of course x and y are the grid coordinates, and do note they are backwards to navigating a DataGridView, and value is teh value you wish to assign).

If you are indeed using Interop the hierarchy is (also I kind of explain their relevence)

  • Application - Simply used to communicate with Excel
  • Workbook - The actual Excel file
  • Worksheet - A sheet within the file (you know those tabs on the bottom)

(also, don't forget you will ahve to save when you are done).

I wish I could help more, but you'll need to provide us more detail. (I actually have begun to write a class to make reading/writing to Excel easier but it's not complete ... one major flaw, getting it to release the resources properly)

This article has been dead for over six months. Start a new discussion instead.