none
get range from excel sheet of separate columns using c#

    Question

  • 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:

    sheet1.get_Range("A",...)

    sheet1.get_Range("D",...)

    sheet1.get_Range("F",...)

     

    Thanks

    Koby

     

     

    Wednesday, June 21, 2006 8:21 AM

Answers

All replies

  • You may want to ask this of Excel gurus in

    http://msdn.microsoft.com/newsgroups/default.aspx?dg=microsoft.public.excel.programming&lang=en&cr=US

     

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

    ActiveWorkbook.Worksheets(1).Range("C:C,E:E").Select

    Thursday, June 22, 2006 6:37 AM
    Moderator
  • 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);




    Note:
    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!):

    http://msdn2.microsoft.com/en-us/library/microsoft.office.tools.excel.worksheet.get_range(VS.80).aspx

    Friday, November 16, 2007 10:39 AM