簡體   English   中英

從excel導入數據而不使用OLEDB

[英]Import data from excel without using OLEDB

大家好,無需使用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因此我遇到了異常Specified argument was out of the range of valid values. Parameter name: index Specified argument was out of the range of valid values. Parameter name: index

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

我一個月前正在從excel表格中讀取一些非常繁重的數據,並且我使用了Codeplex的ExcellReader,這是一個用於從excel讀取的優秀開源項目。 希望對您有幫助。 只是檢查一下..

您可以使用許多第三方組件。 請在NuGet上找到很多清單:

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

其中一些不需要安裝Office(監視關鍵字“ standalone”)。

並非所有人都是免費的,因此您應該執行適當的軟件選擇過程,以查看適合您要求的軟件。

堆棧上討論了許多閱讀XLS文件的建議

在C#中閱讀Excel 2003和2007

檢查此希望對您有幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM