【问题标题】:Excel to DataTable? Without using Microsoft.Office.Interop.ExcelExcel 到数据表?不使用 Microsoft.Office.Interop.Excel
【发布时间】:2013-12-13 11:06:05
【问题描述】:

我们的机器上没有安装 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。但有替代方案吗?我尝试了 Epplus,但互联网上关于 Epplus 的大部分代码都在 C# 中,我无法使用 developer fusion 或 telerik 将其转换为 vb。

有人可以提供一个简单的 sn-p 或其他东西来将 excel 数据放入 DataSet 吗?谢谢。

【问题讨论】:

  • Epplus 是 Excel 2007+ 的不错选择。我建议您不要因为转换到 VB.NET 的问题而放弃。尝试转换并在此处发布有关您遇到的任何问题的问题,您很快就会明白。

标签: .net vb.net winforms excel epplus


【解决方案1】:

你可以试试NPOIMYXLS

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

【讨论】:

  • 能否也提供一些代码?我不知道如何在 VB 中使用它
【解决方案2】:

在没有安装 excel 的情况下调用 Excel 互操作时肯定会出错!

但为了让您入门:Creating an Excel Workbook with VB.NET and ASP.NET

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-11-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多