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

Recommended Answers

All 3 Replies

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.

You can try this
http://www.e-iceblue.com/Introduce/excel-for-net-introduce.html
It supports both for the old Excel 97-2003 format (.xls) and for the new Excel 2007,Excel 2010 and Excel 2013 (.xlsx, .xlsb, .xlsm), along with Open Office(.ods) format.
Download free edition add reference and add:

Workbook workbook = new Workbook();
workbook.LoadFromFile(@"....\XLS1.xlsx");

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)

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.