2013-04-08 29 views
11

Tôi không thể đọc dữ liệu trong Excel. Đây là mã tôi đang sử dụng:Đọc dữ liệu từ excel 2010 bằng Microsoft.Office.Interop.Excel

using Excel = Microsoft.Office.Interop.Excel; 

Excel.Application xlApp = new Excel.Application(); 
Excel.Workbook xlWorkbook = xlApp.Workbooks.Open(@"Book1.xlsx", 0, true, 5, "", "", true, Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0); 
Excel._Worksheet xlWorksheet = (Excel._Worksheet)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(xlWorksheet.Cells[i,j].ToString()); 
    } 
} 

Tôi nhận được hộp thông báo có nội dung gì đó về số System.__ComObject thay vì giá trị.
Làm cách nào để khắc phục sự cố này?

Trả lời

14

Tôi tìm thấy giải pháp cho ở trên, đây là mã:

string temp = (string)(xlRange.Cells[i, j] as Excel.Range).Value2; 
MessageBox.Show(temp); 
5

Haven't thử nghiệm nó, nhưng tôi nghĩ rằng nó nên đọc

MessageBox.Show(xlRange.Cells[i,j].ToString()); 

hoặc cách khác

MessageBox.Show(xlRange.Cells[i,j].Value.ToString()); 
+0

Throwing dưới đây lỗi " 'đối tượng' không chứa một định nghĩa cho ' Giá trị 'và không có phương pháp mở rộng' Giá trị 'chấp nhận đối số đầu tiên của loại' đối tượng 'có thể được tìm thấy (bạn thiếu một chỉ thị sử dụng hoặc tham chiếu assembly?) " – Coolenough

0

Thử mã này:

MessageBox.Show(((Excel.Range)xlRange.Cells[i,j]).Value2.ToString()); 

Mã này hoạt động thành công cho tôi.

2

Hãy thử điều này:

MessageBox.Show(xlRange.Cells[i][j].Value); 
+1

cảm ơn bạn đời. nó hoạt động. –

0

sử dụng chức năng sau đây để có được dữ liệu như đối tượng DataTable cho thứ N tờ:

public DataTable GetWorkSheet(int workSheetID) 
    { 
     string pathOfExcelFile = fileFullName; 
     DataTable dt = new DataTable(); 

     try 
     { 
      excel.Application excelApp = new excel.Application(); 

      excelApp.DisplayAlerts = false; //Don't want Excel to display error messageboxes 

      excel.Workbook workbook = excelApp.Workbooks.Open(pathOfExcelFile, 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); //This opens the file 

      excel.Worksheet sheet = (excel.Worksheet)workbook.Sheets.get_Item(workSheetID); //Get the first sheet in the file 

      int lastRow = sheet.Cells.SpecialCells(excel.XlCellType.xlCellTypeLastCell, Type.Missing).Row; 
      int lastColumn = sheet.Cells.SpecialCells(excel.XlCellType.xlCellTypeLastCell, Type.Missing).Column; 

      excel.Range oRange = sheet.get_Range(sheet.Cells[1, 1], sheet.Cells[lastRow, lastColumn]);//("A1",lastColumnIndex + lastRow.ToString()); 

      oRange.EntireColumn.AutoFit(); 


      for (int i = 0; i < oRange.Columns.Count; i++) 
      { 
       dt.Columns.Add("a" + i.ToString()); 
      } 

      object[,] cellValues = (object[,])oRange.Value2; 
      object[] values = new object[lastColumn]; 

      for (int i = 1; i <= lastRow; i++) 
      { 

       for (int j = 0; j < dt.Columns.Count; j++) 
       { 
        values[j] = cellValues[i, j + 1]; 
       } 
       dt.Rows.Add(values); 
      } 

      workbook.Close(false, Type.Missing, Type.Missing); 
      excelApp.Quit(); 
     } 
     catch (Exception ex) 
     { 
      System.Windows.Forms.MessageBox.Show(ex.Message, "Error", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error); 

     } 
     return dt; 

    }