我的應用程序正在使用Interop庫讀取excel文件數據。我的代碼如下。如何按名稱訪問XLSheet列數據?
string filepath = "C:\\SAddresses.xls";
XLApp=new Excel.Application();
XLBook = XLApp.Workbooks.Open(filepath, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
XLSheet = (Excel.Worksheet)XLBook.Worksheets.get_Item(1);
cRange = XLSheet.UsedRange;
for (int rowcnt = 2; rowcnt < cRange.Rows.Count; rowcnt++)
{
for (int colidx = 1; colidx < cRange.Columns.Count; colidx++)
{
colvalue+=((cRange.Cells[rowidx,colidx] as Excel.Range).Value2);
}
}
在上面,我能得到利用各行索引和列數據index.But我想用列名,而不是列index.How要做到這一點讀取列數據。
你是什麼意思的專欄名稱? A,B,C?或者第一行的文字? –
...我不明白 - 你將如何循環「A」「B」「C」?你能詳細說明你特別想要什麼嗎? –
如果您只是在讀取數據,則可能需要考慮使用OLEDB而不是互操作服務。你可以按名稱定義參考列 - 讓我知道你是否想要一個例子。 –