Friday, April 19, 2024 :: Login  

Excel: import worksheet into Array

 

using Excel;

Excel.Application oExcel;
Excel.Workbook oWorkBook;
Excel.Worksheet oWorkSheet;

String[,] arrreturn = null;
String[,] Arrtmp = null;

oExcel = new Excel.Application();

oWorkBook = oExcel.Workbooks.Open(varExcelFileName, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);

oWorkSheet = (Excel.Worksheet)oWorkBook.Worksheets[1];

int r;
int c;
int intRows;
int intCols;

Excel.Range excelCell = oWorkSheet.UsedRange;

Object[,] values = (Object[,])excelCell.Value2;

intRows = values.GetLength(0);

if (intRows != 0)
{
intCols = values.GetLength(1);

if (intCols != 0)
{
Arrtmp = new String[values.GetLength(0),values.GetLength(1)];
for (r = 1; r <= intRows; r++)
{
for (c = 1; c <= intCols; c++)
{
Arrtmp[r - 1, c - 1] = (string)((Excel.Range)values[r, c]).Text;
}
}
}
}

arrreturn = Arrtmp;
Arrtmp = null;
oWorkSheet = null;
oWorkBook.Close(Missing.Value, Missing.Value, Missing.Value);
oWorkBook = null;
oExcel.Quit();
oExcel = null;

 

 

Privacy Policy www.made4dotnet.com 2020

Map IP Address
Powered byIP2Location.com

   Terms Of Use   ::   Privacy Statement   ::   Copyright (c) 2024 www.made4dotnet.com - .NET Development