.xslx导入错误asp .net中的“Microsoft.ACE.OLEDB.12.0”

时间:2017-02-20 07:39:00

标签: asp.net excel

.xslx导入错误'Microsoft.ACE.OLEDB.12.0' provider is not registered on the local machine. IN SERVER(godaddy webserver)..

有许多博客告诉安装软件.2010和2007 oledb连接软件.. 我的系统是产品32位(x86)架构和64位架构。 我已经尝试了将近2天的问题是在localhost中.xls和.xlsx导入工作正常..但是当我使用服务器时,仅在.xlsx文件上存在问题.. 所以哪个dll需要放在服务器的bin文件夹中,以便我可以解决服务器中的问题..

以下是代码:

    if (flexcel.HasFile)
            {
                name = rnd.Next(111, 9999).ToString() + "_" + System.IO.Path.GetFileName(flexcel.FileName);
                string fileExtension = System.IO.Path.GetExtension(flexcel.FileName);

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

                    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;\"";
                    ////connection String for xls file format.
                    if (fileExtension == ".xls")
                    {
                        excelConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" +
                        fileLocation + ";Extended Properties=\"Excel 8.0;\"";
                    }
                    //connection String for xlsx file format.
                    else 
                    //if (fileExtension == ".xlsx")
                    {
                        excelConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" +
                        Server.MapPath("../Content/MailMarketing/" + name) + ";Extended Properties=Excel 12.0;";
                    }
                    //Create Connection to Excel work book and add oledb namespace
                    OleDbConnection excelConnection = new OleDbConnection(excelConnectionString);
                    excelConnection.Open();
                    DataTable dt = new DataTable();

                    dt = excelConnection.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
                    if (dt == null)
                    {
                        return;
                    }

                    String[] excelSheets = new String[dt.Rows.Count];
                    int t = 0;
                    //excel data saves in temp file here.
                    foreach (DataRow row in dt.Rows)
                    {
                        string x = row["TABLE_NAME"].ToString();
                        if (x != "Sheet1$_" && x != "Sheet2$_" && x != "Sheet3$_" && x != "Sheet4$_" && x != "Sheet5$_")
                        {
                            excelSheets[t] = row["TABLE_NAME"].ToString();
                            t++;
                        }
                    }
                    OleDbConnection excelConnection1 = new OleDbConnection(excelConnectionString);

                    int totalsheet = excelSheets.Length;
                    for (int i = 0; i < totalsheet; i++)
                    {
                        string query = string.Format("Select * from [{0}]", excelSheets[i]);
                        using (OleDbDataAdapter dataAdapter = new OleDbDataAdapter(query, excelConnection1))
                        {
                            dataAdapter.Fill(ds);
                        }
                    }
                }
                if (fileExtension.ToString().ToLower().Equals(".xml"))
                {
                    string fileLocation = Server.MapPath("~/Content/") + Request.Files["FileUpload"].FileName;
                    if (System.IO.File.Exists(fileLocation))
                    {
                        System.IO.File.Delete(fileLocation);
                    }

                    Request.Files["FileUpload"].SaveAs(fileLocation);
                    XmlTextReader xmlreader = new XmlTextReader(fileLocation);
                    // DataSet ds = new DataSet();
                    ds.ReadXml(xmlreader);
                    xmlreader.Close();
                }

有什么建议吗?

如果没有安装软件,任何人都可以建议我将哪种类型的.dll文件放在bin文件夹上?

1 个答案:

答案 0 :(得分:-1)

无需担心只有oledb ..还有很多其他方法用于上传.xlsx文件(导入)。

以下是标题:

using ClosedXML.Excel;

从nuget安装ClosedXML.Excel dll。

以下是代码:

  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++;
                    }
                }
}