如何将相同的Excel文件导入SQL Server

时间:2014-11-29 20:49:13

标签: c# asp.net-mvc visual-studio-2010

我创建了一个asp.net mvc4应用程序,并将其部署到网上。

在这个应用程序中,我有一个控制器将一些Excel文件导入数据库。

在我的本地主机上它工作正常,但是当我尝试导入一个与现有文件同名的文件时,在服务器上它会给我一个错误

(我看不到异常,我看到一些不错的消息(抓住......)

这是代码的一部分:

if (Request.Files["file"].ContentLength > 0)
{
    string fileExtension = System.IO.Path.GetExtension(Request.Files["file"].FileName);

    if (fileExtension == ".xls" || fileExtension == ".xlsx")
    {
        string fileLocation = Server.MapPath("~/Content/") + Request.Files["file"].FileName;

        if (System.IO.File.Exists(fileLocation))
        {
            System.IO.File.Delete(fileLocation);
        }

        Request.Files["file"].SaveAs(fileLocation);

        string excelConnectionString = string.Empty;

        excelConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" +
        fileLocation + ";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=" +
            fileLocation + ";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();

        DataTable dt = new DataTable();

        dt = excelConnection.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);

        if (dt == null)
        {
            return null;
        }

        String[] excelSheets = new String[dt.Rows.Count];
        int t = 0;

        //excel data saves in temp file here.
        foreach (DataRow row in dt.Rows)
        {
            excelSheets[t] = row["TABLE_NAME"].ToString();
            t++;
        }

        OleDbConnection excelConnection1 = new OleDbConnection(excelConnectionString);

        string query = string.Format("Select * from [{0}]", excelSheets[0]);

        using (OleDbDataAdapter dataAdapter = new OleDbDataAdapter(query, excelConnection1))
        {
            dataAdapter.Fill(ds);
        }

        excelConnection.Close();
    }

    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);

        ds.ReadXml(xmlreader);
        xmlreader.Close();
    }
}

1 个答案:

答案 0 :(得分:0)

我的回答是在不知道具体异常的情况下(正如您所提到的,您自己无法看到异常)。在我的类似经历中,我遇到了同样的问题,并且通过在服务器上安装Microsoft ACE OLEDB 12.0驱动程序解决了这个问题。我已经安装了以下组件来解决问题:

2007 Office System驱动程序 - 数据连接组件: http://www.microsoft.com/en-us/download/details.aspx?id=23734

如果这没有帮助,有关您所看到的例外情况的更多信息将有助于解决问题。