您如何遍历具有多个工作表的 Excel 工作簿,仅从“C”、“E”和“F”列中提取数据?
这是我到目前为止的代码:
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;
}
非常感谢您的时间,任何帮助表示赞赏。