/ / Lectura de datos de clientes de Excel en una matriz - c #, matrices, excel, vsto

Lectura de datos de clientes de Excel en una matriz: c #, matrices, excel, vsto

Soy nuevo en la codificación y estoy tratando de obtener información (número de identificación, nombre, fecha de nacimiento) de cada cliente en su propia matriz. Hasta ahora soy capaz de leer los datos de Excel, pero no sé cómo almacenarlo en una matriz y no estoy seguro de cómo convertir "valueArray" de un objeto a una matriz de cadena

static void Main(string[] args)
{
// Reference to Excel Application.
Excel.Application xlApp = new Excel.Application();

Excel.Workbook xlWorkbook = xlApp.Workbooks.Open(Path.GetFullPath("excelpractice1.xlsx"));

// Get the first worksheet.
Excel.Worksheet xlWorksheet = (Excel.Worksheet)xlWorkbook.Sheets.get_Item(1);

// Get the range of cells which has data.
Excel.Range xlRange = xlWorksheet.UsedRange;

// Get an object array of all of the cells in the worksheet with their values.
object[,] valueArray = (object[,])xlRange.get_Value(Excel.XlRangeValueDataType.xlRangeValueDefault);

// iterate through each cell and display the contents.
for (int row = 1; row <= xlWorksheet.UsedRange.Rows.Count; ++row)
{
Console.WriteLine(valueArray[row, row].ToString());
}

// Close the Workbook.
xlWorkbook.Close(false);

// Relase COM Object by decrementing the reference count.
Marshal.ReleaseComObject(xlWorkbook);

// Close Excel application.
xlApp.Quit();

// Release COM object.
Marshal.FinalReleaseComObject(xlApp);

Console.ReadLine();
}

Respuestas

0 para la respuesta № 1

Algo como esto:

String[,] arr = new String[xlWorksheet.UsedRange.Rows.Count,xlWorksheet.UsedRange.Columns.Count];
for (int row = 1; row <= xlWorksheet.UsedRange.Rows.Count; ++row)
{
for (int col = 1; col <= xlWorksheet.UsedRange.Columns.Count; ++col)
{
arr[row,col] = valueArray[row, col].ToString();
}
}

También puedes usar Array.Copy me gusta aquí.

Por cierto No estoy seguro, pero creo que debería tener que liberar también xlRange