Home>

After specifying sheet 1 and outputting the value of the cell range specified by range, I want to specify sheet 10 and output the value of the cell range specified by range. Currently, I am writing the following source code, and when I execute it, the processing of sheet 1 works well, but when I set it to sheet 10, even though the sheet is specified in range.value2 for some reason, the one of sheet 1 It will be. How can I specify a sheet and output the value in range?

// Excel application generation
Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application ();
// Open an existing Book
Microsoft.Office.Interop.Excel.Workbooks xlBooks = xlApp.Workbooks;
Microsoft.Office.Interop.Excel.Workbook xlBook = xlBooks.Open (System.IO.Path.GetFullPath (@ "Excel name"));
// Select a sheet
Microsoft.Office.Interop.Excel.Sheets xlSheets = xlBook.Worksheets;
// Get Excel file name
string ExcelBookFileName = xlBook.FullName;
// Open Excel file
Workbook wb = xlApp.Workbooks.Open (ExcelBookFileName,
              Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
              Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
              Type.Missing);

// Sheet specification
ExcellData.ws1 = ExcellData.wb.Sheets [1];
range = .xlApp.get_Range ("A1", "D100);
for (int a = 1;a<100: a ++)
{
     textbox1.Text = range.value2 [a, a];
}

// Sheet specification
ExcellData.ws1 = ExcellData.wb.Sheets [10];
range = .xlApp.get_Range ("A1", "D100);
for (int a = 1;a<100: a ++)
{
     textbox1.Text = range.value2 [a, a];
}


I am developing using Microsoft.Office.Interop.Excel in C # and Form Application.

  • Answer # 1

    // Sheet specification
    ExcellData.range = ExcellData.xlApp.get_Range ("C" + test, "D" + test);

    To

    // Sheet specification
    ExcellData.range = ExcellData.ws1.get_Range ("C" + test, "D" + test);


    It was solved by specifying ws and substituting. Radian who gave me a hint. Thank you very much.