none
using Excel = Microsoft.Office.Interop.Excel; RRS feed

  • Question

  • I am trying to open an excel document, read the double in a certain cell into my program, do an addition sum on that double then update the cell.

      I can read the excel doc into my program and output all to a list box. I want to get the value of one cell, add but cast is redundant and I only get system object in my list box. How can I capture one cell then update it?
                                            Excel.Application xlApp	        = new Excel.Application();
    					Workbook xlWorkbook		= xlApp.Workbooks.Open(@"C:\Users\cb\Desktop\Project \Book1.xlsx");
    					_Worksheet xlWorksheet		= xlWorkbook.Sheets[1];
    					Range xlRange			= xlWorksheet.UsedRange;
    
    			Sheets excelSheets = xlWorkbook.Worksheets;
    			string currentSheet = "Sheet1";
    			Worksheet excelWorksheet = (Excel.Worksheet)excelSheets.get_Item(currentSheet);
    
    			Range excelCell =
    			(Excel.Range)excelWorksheet.get_Range("C5", "C5").Value2;
    			listBox1.Items.Add(excelCell); 


    Tuesday, July 11, 2017 9:02 AM

All replies

  • Hi CuriousCoder15,

    excelWorksheet.get_Range("C5", "C5").Value2 is the value in Cell C5, it could not be forced as range. You could add value in C5 to listbox like this.

                Excel.Range excelCell =excelWorksheet.get_Range("C5", "C5");

                listBox1.Items.Add(excelCell.Value2);

    Best Regards,

    Terry

    Wednesday, July 12, 2017 1:37 AM