2013-04-08 61 views
11

從excel 2010讀取數據我無法讀取Excel中的數據。這裏是我使用的代碼:使用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()); 
    } 
} 

我得到一個消息框,指出一些有關System.__ComObject而不是價值。
我該如何解決這個問題?

回答

14

我發現上述方案中,這裏是代碼:

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

Haven't測試,但我認爲它應該讀

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

或可替代

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

投擲以下錯誤「‘對象’不包含「的定義值'並且沒有擴展方法'值'接受類型'對象'的第一個參數可以找到(你是否缺少使用指令或程序集引用?)「 – Coolenough 2013-04-08 07:35:03

0

試試這個代碼:

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

此代碼對我成功運行。

2

嘗試這種情況:

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

謝謝隊友。有用。 – 2017-04-02 09:54:28

0

使用下面的函數來獲取的數據作爲第N個片DATATABLE對象:

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; 

    } 
相關問題