none
Excel with C# -- how to specify a range ? RRS feed

  • Question

  •  

    VB6 syntax:
    Worksheets("Sheet1").Range(Cells(1, 1), Cells(5, 3)).Font.Italic = True

     

    This line is good (C# syntax):
    ((Excel.Range)ws.Cells[j,5]).Value2 = "found status here";

     

    this one has bad C# syntax:
    ((Excel.Range)ws.Cells[j,1], ws.Cells[j,5]).Font.Italic = True;

     

    another bad try:
    Excel.Range range1 = ((ws.Cells[j,1])),((ws.Cells[j,5]);


    How to fix it?

    Thursday, March 13, 2008 11:41 PM

Answers

  • Hi,

     

    You can try the following codes:

    Code Snippet

                //New Application

                Excel.Application excelApp = new Microsoft.Office.Interop.Excel.Application();

                excelApp.Visible = true;

     

                //create a new blank workbook

                excelApp.Workbooks.Add(Type.Missing);

                //create a new blank worksheet in current workbook

                excelApp.Worksheets.Add(Type.Missing, Type.Missing, 1, Type.Missing);

     

                Excel.Worksheet ws = excelApp.ActiveSheet as Excel.Worksheet;

                //Get the range using number index

                Excel.Range newRng = excelApp.get_Range(ws.Cells[1,1], ws.Cells[4, 5]);

                newRng.Value2 = "Test String put in Cells";

     

     

     

    Thanks,

    Ji

     

    Tuesday, March 18, 2008 5:37 AM
    Moderator

All replies

  • oSheet.get_Range(rangeA, rangeA).

     

    Adam

    Thursday, March 13, 2008 11:56 PM
  • Hi,

     

    You can try the following codes:

    Code Snippet

                //New Application

                Excel.Application excelApp = new Microsoft.Office.Interop.Excel.Application();

                excelApp.Visible = true;

     

                //create a new blank workbook

                excelApp.Workbooks.Add(Type.Missing);

                //create a new blank worksheet in current workbook

                excelApp.Worksheets.Add(Type.Missing, Type.Missing, 1, Type.Missing);

     

                Excel.Worksheet ws = excelApp.ActiveSheet as Excel.Worksheet;

                //Get the range using number index

                Excel.Range newRng = excelApp.get_Range(ws.Cells[1,1], ws.Cells[4, 5]);

                newRng.Value2 = "Test String put in Cells";

     

     

     

    Thanks,

    Ji

     

    Tuesday, March 18, 2008 5:37 AM
    Moderator
  • If you want to select a specific cell in Excel sheet, you can code like this.

    Excel.Worksheet excelSheet = workbook.ActiveSheet;

    Excel.Range rng = (Excel.Range)excelSheet.Cells[10, 10];

    If you want to select multiple cell value from Excel sheet, you can code like this.

    Excel.Worksheet excelSheet = workbook.ActiveSheet;
    Excel.Range rng = (Excel.Range) excelSheet.get_Range(excelSheet.Cells[1, 1], excelSheet.Cells[3,3]);

     Source from ... : Format Excel sheet from C#

    VB6 syntax:
    Worksheets("Sheet1").Range(Cells(1, 1), Cells(5, 3)).Font.Italic = True

    This line is good (C# syntax):
    ((Excel.Range)ws.Cells[j,5]).Value2 = "found status here";

    this one has bad C# syntax:
    ((Excel.Range)ws.Cells[j,1], ws.Cells[j,5]).Font.Italic = True;

    another bad try:
    Excel.Range range1 = ((ws.Cells[j,1])),((ws.Cells[j,5]);


    How to fix it?




    • Edited by bonave2008 Friday, March 21, 2014 5:34 AM edit
    Friday, March 21, 2014 5:31 AM