【问题标题】:Import data from excel without using OLEDB不使用 OLEDB 从 excel 导入数据
【发布时间】:2013-06-11 10:30:14
【问题描述】:

大家好,不使用OLEDB可以加载excel数据吗,我已经为.xls and .xlsx编写了如下的连接字符串代码

ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + pFilePath + ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=2\"";

ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + pFilePath + ";Extended Properties=\"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) 这种情况下,我得到了Specified argument was out of the range of valid values. Parameter name: index 的异常

【问题讨论】:

  • 您可以尝试使用OpenXML方法http://msdn.microsoft.com/en-us/library/office/gg575571.aspx
  • pratik 你能告诉我如何加载 2003 excel 文件,它也需要 2007 年或 2010 年
  • 这是使用 OpenXml 方法的限制。 :( 唯一的替代方法是答案中建议的其他第三方工具。
  • 在上面的代码中,日期列读取为数字,例如2014 年 1 月读作 41640。您对此有何建议?

标签: c# asp.net oledb


【解决方案1】:

我从 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;

【讨论】:

    【解决方案2】:

    一个月前我正在从 excel 表中读取一些相当大量的数据,我使用了 codeplex 的 ExcellReader,这是一个用于从 excel 中读取的优秀开源项目。我希望它也能帮助你。看看就好。。

    【讨论】:

      【解决方案3】:

      您可以为此使用许多第三方组件。请在 NuGet 上找到相当大的列表:

      https://nuget.org/packages?q=excel

      其中一些不需要安装 office(注意关键字“standalone”)。

      并非所有软件都是免费的,因此您应该执行适当的软件选择过程,看看哪些适合您的要求。

      【讨论】:

      • 嗨,如果我需要同时导入excel文件和打开office电子表格数据,我应该常用什么
      【解决方案4】:

      很多关于阅读 XLS 文件的建议都在堆栈上讨论过

      Read Excel 2003 & 2007 in C#

      检查这个希望它有帮助。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-02-14
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多