c# - 不使用 OLEDB 从 Excel 导入数据

标签 c# asp.net oledb

大家好,是否可以在不使用OLEDB的情况下加载Excel数据,我已经使用连接字符串为.xls和.xlsx编写了代码,如下所示

>

ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+ pFilePath + ";扩展属性=\"Excel 8.0;HDR=Yes;IMEX=2\"";

ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source="+ pFilePath + ";扩展属性=\"Excel 12.0;HDR=Yes;IMEX=2\"";

在我的系统中,与 12.0 相关的 dll 不存在,我收到错误。所以我想知道是否有什么方法可以实现我的要求而不是一般方法

编写的示例代码

public void Method(string pFilePath)
    {
        DataTable dt = new DataTable();

        using (SpreadsheetDocument spreadSheetDocument = SpreadsheetDocument.Open(pFilePath, false))
        {

            WorkbookPart workbookPart = spreadSheetDocument.WorkbookPart;
            IEnumerable<Sheet> sheets = spreadSheetDocument.WorkbookPart.Workbook.GetFirstChild<Sheets>().Elements<Sheet>();
            string relationshipId = sheets.First().Id.Value;
            WorksheetPart worksheetPart = (WorksheetPart)spreadSheetDocument.WorkbookPart.GetPartById(relationshipId);
            Worksheet workSheet = worksheetPart.Worksheet;
            SheetData sheetData = workSheet.GetFirstChild<SheetData>();
            IEnumerable<Row> rows = sheetData.Descendants<Row>();

            foreach (Cell cell in rows.ElementAt(0))
            {
                dt.Columns.Add(GetCellValue(spreadSheetDocument, cell));
            }

            foreach (Row row in rows)
            {
                DataRow tempRow = dt.NewRow();

                for (int i = 0; i < row.Descendants<Cell>().Count(); i++)
                {
                    tempRow[i] = GetCellValue(spreadSheetDocument, row.Descendants<Cell>().ElementAt(i - 1));
                }

                dt.Rows.Add(tempRow);
            }

        }
        dt.Rows.RemoveAt(0);
    }
    public static string GetCellValue(SpreadsheetDocument document, Cell cell)
    {
        SharedStringTablePart stringTablePart = document.WorkbookPart.SharedStringTablePart;
        string value = cell.CellValue.InnerXml;

        if (cell.DataType != null && cell.DataType.Value == CellValues.SharedString)
        {
            return stringTablePart.SharedStringTable.ChildElements[Int32.Parse(value)].InnerText;
        }
        else
        {
            return value;
        }
    }

但是在 foreach (Row row in rows) 这种情况下,我收到一个异常,因为 指定的参数超出了有效值的范围。 参数名称:索引

最佳答案

我从 excel 读入 mysql 数据库。也许不是最好的方法。 但这是我的代码:

 Excel.Application xlApp = new Excel.Application();

        Excel.Workbook xlWorkbook = xlApp.Workbooks.Open(@"\\xxxx\yyyy.xlsx");
        Excel._Worksheet xlWorksheet = xlWorkbook.Sheets[1];
        Excel.Range xlRange = xlWorksheet.UsedRange;

        int rowCount = xlRange.Rows.Count;
        int colCount = xlRange.Columns.Count;
        string Kund = "";



        for (int i = 1; i <= rowCount; i++)
        {
            for (int j = 1; j <= colCount; j++)
            {


                if ((j == 1) && (i > 1))
                {
                    MySqlConnection conn = new MySqlConnection(databas);

                    Kund = xlRange.Cells[i, j].Value2.ToString();
                }
                ...
            }
                ...
         }
           xlApp.Workbooks.Close();

您必须使用using Excel = Microsoft.Office.Interop.Excel;

关于c# - 不使用 OLEDB 从 Excel 导入数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17041954/

相关文章:

c# - 为什么要使用公共(public)变量?

c# - RazorTemplateEngine 解析器产生不寻常的语法树

c# - 使用 EventArgs/EventArgs<T> 委托(delegate)类型而不是……的事件的好处

asp.net - 使用@@identity检索PK

c# - 合并两个共有 1 列的数据集

c# - 可以更改 NHibernate 的格式来制定绑定(bind)变量吗?

c# - 填充数据表并丢失第一行

c# - Html.BeginForm() 未传递模型

c# - MS Access 的 INSERT INTO 语句中的语法错误

c# - 在 ASP.NET 应用程序上以编程方式配置 Microsoft.IdentityModel 不起作用 - 被动重定向已启用但从未起作用