var excelApp = new Excel.Application();
var excelWorkbook = excelApp.Workbooks.Open(Program.FileName);
var excelSheets1 = excelWorkbook.Worksheets;
var excelWorksheet1 = (Excel.Worksheet)excelSheets1.Item["Sheet1"];
for (var i = 2; i <= 62; i++)
{
for (var j = 2; j <= 5; j++)
{
var iRow = _dtCode1.NewRow();
var cellValue = Convert.ToString(((Excel.Range)excelWorksheet1.Cells[i, j]).Value);
var codeValue = Convert.ToString(((Excel.Range)excelWorksheet1.Cells[i + 1, j]).Value);
iRow[0] = cellValue; iRow[1] = codeValue;
_dtCode1.Rows.Add(iRow);
}
i = i + 1;
}
this code above in
var excelWorksheet1 = (Excel.Worksheet)excelSheets1.Item["Sheet1"];
i wanna change Sheet1 by ID like the number of sheet in the woorkbook the second thing is there any way to close the excel file after open it
var excelWorkbook = excelApp.Workbooks.Open(Program.FileName);
To select a sheet using its index use:
Excel.Worksheet xlWorkSheetFocus = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
xlWorkSheetFocus.Activate();
To close the workbook use
excelWorkbook.Close();
To quit Excel app:
excelApp.Quit();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.