2012-03-11 41 views
0

我有一個包含數據的excel。我想讀從A列的單元格的值F列的單元格值的數據,並繼續閱讀,其中x等於在列F例如讀取單元格的值下一個x列的單元格值:從WinForms Excel中檢索數據

  A B C D E F G H I J 
     a a a a a 1 a a a a 

F公司單元格的值爲1,所以我想讀(GHIJ) 若F的單元格的值是2的我想讀取(GHIJKLMN)

  A B C D E F G H I J K L M N 
     a a a a a 2 a a a a a a a a 

F公司細胞值3:

 A B C D E F G H I J K L M N O P Q R 
     a a a a a 3 a a a a a a a a a a a a 

我該怎麼做?

回答

0

兩個選項:

首先是要容易得多,但第二個作品,當你沒有安裝Excel做。

+0

我已經在使用Interop,但我不知道該怎麼做......問題是:我該如何實現它? – Vlasin 2012-03-11 21:52:05

+0

你還沒有任何代碼,或者你還在計劃中嗎? – 2012-03-11 21:52:46

+0

我有一個代碼,但它不工作...我只是希望看到別人如何解決它.. – Vlasin 2012-03-11 22:01:08

0
// OPEN EXCEL and WORKSHEET 

xlApp = new Microsoft.Office.Interop.Excel.Application(); 
xlWorkBook = xlApp.Workbooks.Open(@"C:\TEST.xlsx", 0, false, 5, "", "", false, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "", true, false, 0, true, false, false); 
xlWorkSheet = (Microsoft.Office.Interop.Excel.Worksheet)xlWorkBook.Sheets[1]; 

// F column index starting, A == 1 
int lastColumnIndex = 6; 

// read each cell including 'F1' and add it to the richTextBox (it is my placeholder in test project) 
for (int i = 1; i <= lastColumnIndex; i++) 
{ 
    richTextBox1.AppendText(Convert.ToString(((Range)xlWorkSheet.Cells[1, i]).Value2)); 
} 

// read and save the value of 'F1' to a variable - 2nd read of the same cell can be avoided, though 
int extraColumnsToRead = Convert.ToInt32(((Range)xlWorkSheet.Cells[1, lastColumnIndex]).Value2) * 4; 

// (value(F1) * 4) cells will be loaded and added to my RichTextBox. 
if (extraColumnsToRead > 0) 
    for (int i = lastColumnIndex + 1; i <= lastColumnIndex + extraColumnsToRead; i++) 
    { 
     richTextBox1.AppendText(Convert.ToString(((Range)xlWorkSheet.Cells[1, i]).Value2)); 
    }