Cómo iterar a través de las hojas de cálculo de Excel solo extrayendo datos de columnas específicas
¿Cómo iterar a través de un libro de Excel con múltiples hojas de trabajo que solo extraen datos de las columnas "C", "E" y "F"?
Aquí está el código que tengo hasta ahora:
public static string ExtractData(string filePath)
{
Excel.Application excelApp = new Excel.Application();
Excel.Workbook workBook = excelApp.Workbooks.Open(filePath);
string data = string.Empty;
int i = 0;
foreach (Excel.Worksheet sheet in workBook.Worksheets)
{
data += "******* Sheet " + i++.ToString() + " ********\n";
//foreach (Excel.Range row in sheet.UsedRange.Rows)
//{
// data += row.Range["C"].Value.ToString();
//}
foreach (Excel.Range row in sheet.UsedRange.Rows)
{
foreach (Excel.Range cell in row.Columns)
{
data += cell.Value + " ";
}
data += "\n";
}
}
excelApp.Quit();
return data;
}
Muchas gracias por su tiempo, cualquier ayuda es apreciada.