简体   繁体   中英

The 'Microsoft.ACE.OLEDB.12.0' provider in import code asp .net

The 'Microsoft.ACE.OLEDB.12.0' provider is not registered on the local machine. on import code:

  if (flexcel.HasFile)
            {
                string fileExtension = System.IO.Path.GetExtension(flexcel.FileName);

                if (fileExtension == ".xls" || fileExtension == ".xlsx")
                {
                    string fileLocation = Server.MapPath("../Content/MailMarketing/") + flexcel.FileName + month;

                    if (System.IO.File.Exists(fileLocation))
                    {

                        // System.IO.File.Delete(fileLocation);
                    }
                    flexcel.SaveAs(fileLocation);
                    string excelConnectionString = string.Empty;
                    excelConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" +
                    Server.MapPath("~/Content/MailMarketing/" ) + flexcel.FileName + month + ";Extended Properties=\"Excel 12.0;HDR=Yes;IMEX=2\"";
                    //connection String for xls file format.
                    if (fileExtension == ".xls")
                    {
                        excelConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" +
                        fileLocation + ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=2\"";
                    }
                    //connection String for xlsx file format.
                    else if (fileExtension == ".xlsx")
                    {
                        excelConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" +
                        Server.MapPath("~/Content/MailMarketing/") + flexcel.FileName + month + ";Extended Properties=\"Excel 12.0;HDR=Yes;IMEX=2\"";
                    }
                    //Create Connection to Excel work book and add oledb namespace
                    OleDbConnection excelConnection = new OleDbConnection(excelConnectionString);
                    excelConnection.Open();

How to solve??

I have already download this software but still not working..any suggestion??

https://www.microsoft.com/en-us/download/details.aspx?id=23734

I have tried a lot code but the same issue..so any suggestion???

No need to worry about only oledb..there is a lot of other method are used to upload .xlsx files(import).

Here is the header:

using ClosedXML.Excel;

Install ClosedXML.Excel dll from nuget.

Here is the code:

  string filePath = Server.MapPath("~/Content/MailMarketing/") + Path.GetFileName(flexcel.PostedFile.FileName);
        flexcel.SaveAs(filePath);

        //Open the Excel file using ClosedXML.
        using (XLWorkbook workBook = new XLWorkbook(filePath))
        {
            //Read the first Sheet from Excel file.
            IXLWorksheet workSheet = workBook.Worksheet(1);

            //Create a new DataTable.
            DataTable dt = new DataTable();

            //Loop through the Worksheet rows.
            bool firstRow = true;
            foreach (IXLRow row in workSheet.Rows())
            {
                //Use the first row to add columns to DataTable.
                if (firstRow)
                {
                    foreach (IXLCell cell in row.Cells())
                    {
                        dt.Columns.Add(cell.Value.ToString());
                    }
                    firstRow = false;
                }
                else
                {
                    //Add rows to DataTable.
                    dt.Rows.Add();
                    int i = 0;
                    foreach (IXLCell cell in row.Cells())
                    {
                        dt.Rows[dt.Rows.Count - 1][i] = cell.Value.ToString();
                        i++;
                    }
                }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM