我想寫一個應用程序,它將打開一個excel電子表格找到正確名稱的工作表並遍歷行,直到找到包含文本的第0列單元格爲止「Cont Date 「然後閱讀,直到找到第一個空白單元格(第0列)。我越來越掛在如何遍歷行。從Excel中查找和提取數據
這是我到目前爲止有:
public static void LoadFromFile(FileInfo fi)
{
Application ExcelObj = new Application();
if (ExcelObj != null)
{
Workbook wb = ExcelObj.Workbooks.Open(fi.FullName,
Type.Missing, true, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing);
Sheets sheets = wb.Worksheets;
foreach (Worksheet ws in sheets)
{
if (ws.Name == "Raw Data")
LoadFromWorkSheet(ws);
}
wb.Close(false, Type.Missing, Type.Missing);
}
}
public static void LoadFromWorkSheet(Worksheet ws)
{
int start = 0;
int end = 0;
// Iterate through all rows at column 0 and find the cell with "Cont Date"
}
顯然,你不能
foreach(Row row in worksheet.Rows)
{
}
編輯::
我所做的就是這是什麼:
for (int r = 0; r < 65536; r++)
{
string value = ws.Cells[r, 0].Value;
}
這給了我以下的exce ption當試圖讀取單元格的值:
Exception from HRESULT: 0x800A03EC
不,它必須是ws.Cells [r,1]。列以1開頭 – 2010-10-14 20:24:17