get range from excel sheet of separate columns using c#


  • hi,

    I want to get range of separate columns from sheet in excel.

    for example: I want the entire columns: "A", "D", "F"


    Can I do it in one line statement? or whether I need to do it in three steps:









    Wednesday, June 21, 2006 8:21 AM


All replies

  • You may want to ask this of Excel gurus in


    FWIW I think you should be able to do it in one step. This works in Excel VBA:


    Thursday, June 22, 2006 6:37 AM
  • You will need the following imports:

    Code Block

    using Excel;
    using System.Reflection;                  // Needed for Missing.Value

    And here's the code

    Code Block

    // Code to get to the point of selecting the range
    Excel.Application excelApp = new Excel.ApplicationClass();
    Excel.Workbook workbook = excelApp.Workbooks.Open("c:\spreadsheet.xls", 0, false, 5, "", "", false, Excel.XlPlatform.xlWindows, "", true, false, 0, true, false, false);
    Excel.Worksheet worksheet = (Excel.Worksheet) workbook.Worksheets.get_Item("sheet1");

    // Select a multi column range
    Excel.Range dataRange =worksheet.get_Range("A:A,D:D,F:F", Missing.Value);

    Unfortunately the get_Range Method is "not intended to be used directly in your code" but as I haven't found a supported way yet I use it in the way I've described above.  The MSDN page for the method is as follows (for now!):

    Friday, November 16, 2007 10:39 AM