Excel到DataTable? 不使用Microsoft.Office.Interop.Excel

我们的机器上没有安装Microsoft Office。 我正在尝试读取一个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 

错误: Retrieving the COM class factory for component with CLSID {00024500-0000-0000-C000-000000000046} failed due to the following error: 80040154.

我知道它最有可能没有安装在计算机上的Microsoft Office。 但有没有其他的select? 我尝试了Epplus,但大多数互联网上关于Epplus的代码都在C#中,我无法使用开发人员融合或telerik将其转换为vb。

有人可以提供一个简单的片段或东西来获取Excel数据到Excel中? 谢谢。

你可以试试NPOI和MYXLS 。

编辑:为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; } 

当你调用没有安装excel的Excel interop时,肯定会出错。

但是为了让你开始: 用VB.NET和ASP.NET创build一个Excel工作簿