2012-06-14 183 views
-1

我寫了一些從excel中讀取數據並在消息框中顯示數據的代碼。我從網站上獲取了該代碼。當我運行一樣,我在兩個特定行從Excel中讀取數據時出錯

在那裏我遇到的都是錯誤如下線得到了一些錯誤

for (int i = 1; i <= rowCount; i++) 
      { 
       for (int j = 1; j <= colCount; j++) 
       { 
        MessageBox.Show(xlRange.Cells[i, j].Value2.ToString()); 
       } 
      } 

完整的代碼如下

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 

using Excel = Microsoft.Office.Interop.Excel; 

namespace ExcelReader 
{ 
    public partial class Form1 : Form 
    { 
     public Form1() 
     { 
      InitializeComponent(); 
     } 

     private void Form1_Load(object sender, EventArgs e) 
     { 
      Excel.Application xlApp = new Excel.Application(); 
      Excel.Workbook xlWorkbook = xlApp.Workbooks.Open(@"D:/C.xlsx"); 
      Excel._Worksheet xlWorksheet = xlWorkbook.Sheets[1]; 
      Excel.Range xlRange = xlWorksheet.UsedRange; 

      int rowCount = xlRange.Rows.Count; 
      int colCount = xlRange.Columns.Count; 

      for (int i = 1; i <= rowCount; i++) 
      { 
       for (int j = 1; j <= colCount; j++) 
       { 
        MessageBox.Show(xlRange.Cells[i, j].Value2.ToString()); 
       } 
      } 

     } 

    } 
} 

燦有人很好地幫助我瞭解這段代碼的正確解決方案嗎?

+3

你得到哪些錯誤?你爲什麼要在網頁瀏覽器中設置「<";」來代替「<」? – Marco

+0

「&lt; value is」<「 如果可能,你應該點擊網頁上的複製源代碼按鈕,不要複製在html中的原生內容。 – Thinhbk

+0

@Thinhbk:是的,但是這應該是C#! – Marco

回答

0

更換

for (int i = 1; i &lt;= rowCount; i++) 
{ 
    for (int j = 1; j &lt;= colCount; j++) 
    { 

for (int i = 1; i <= rowCount; i++) 
{ 
    for (int j = 1; j <= colCount; j++) 
    {