2013-12-13 80 views
1

我們的機器上沒有安裝Microsoft Office。 我想讀一個excel文件,並將其存儲在一個數據集,但我在Excel到DataTable?不使用Microsoft.Office.Interop.Excel

Dim ws As Microsoft.Office.Interop.Excel.Worksheet 
         Dim Obj As Object 

         ws = DirectCast(workbook.Worksheets(1), Microsoft.Office.Interop.Excel.Worksheet) <---error here 

ERROR收到錯誤:Retrieving the COM class factory for component with CLSID {00024500-0000-0000-C000-000000000046} failed due to the following error: 80040154.

我知道它最有可能是安裝在機器上沒有微軟Office。但是有沒有其他選擇?我嘗試過Epplus,但大部分關於Epplus的代碼都在C#中,我無法使用開發人員融合或telerik將其轉換爲vb。

有人可以提供一個簡單的片段或東西來獲取Excel數據到Excel中嗎?謝謝。

+0

Epplus是爲Excel 2007+一個不錯的選擇。我建議你不要放棄,因爲你的問題轉換爲VB.NET。嘗試在這裏轉換併發布有關您遇到的任何問題的問題,您很快就會拿起它。 – Joe

回答

1

你可以有一個嘗試NPOIMYXLS

Edit:Add some sample code for NPOI.

public static DataTable ExcelInput(string FilePath) 
    {    
     DataTable table = new DataTable(); 
     //Get the excel from filepath 
     HSSFWorkbook workbook = new HSSFWorkbook(File.Open(FilePath, FileMode.Open)); 
     HSSFSheet sheet = (HSSFSheet)workbook.GetSheetAt(0); 

     //get Excel rows 
     int rowsCount = sheet.PhysicalNumberOfRows;   

     int colsCount = sheet.GetRow(0).PhysicalNumberOfCells; 

     for (int i = 0; i < colsCount; i++) 
     { 
      table.Columns.Add(i.ToString()); 
     } 

     for (int x = 0; x < rowsCount; x++) 
     { 
      DataRow dr = table.NewRow(); 
      for (int y = 0; y < colsCount; y++) 
      { 
       dr[y] = sheet.GetRow(x).GetCell(y).ToString(); 
      } 
      table.Rows.Add(dr); 
     } 

     sheet = null; 
     workbook = null; 
     return table; 
    } 
+0

可以請提供一些代碼嗎?我不知道如何使用VB – Arbaaz

+0

我使用C#.Blow是一些示例代碼。 – Justin

相關問題