Editing your method, here's something should do what you're looking for:
public static string ExtractData(string filePath)
{
Excel.Application excelApp = new Excel.Application();
Excel.Workbook workBook = excelApp.Workbooks.Open(filePath);
int[] Cols = { 3, 5, 6 }; //Columns to loop
//C, E, F
string data = string.Empty;
int i = 0;
foreach (Excel.Worksheet sheet in workBook.Worksheets)
{
data += "******* Sheet " + i++.ToString() + " ********
";
foreach (Excel.Range row in sheet.UsedRange.Rows)
{
foreach (int c in Cols) //changed here to loop through columns
{
data += sheet.Cells[row.Row, c].Value2.ToString() + " ";
}
data += "
";
}
}
excelApp.Quit();
return data;
}
I've created a int array to indicate which columns you'd like to read from, and then on each row we just loop through the array.
HTH,
Z
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…