使用asp.net将Excel文件数据导入Sql server

Posted

技术标签:

【中文标题】使用asp.net将Excel文件数据导入Sql server【英文标题】:Import Excel file data into Sql server using asp.net 【发布时间】:2015-05-01 09:24:55 【问题描述】:

我想将 excel 文件数据导入 SQL Server,但出现如下错误:

外部表不是预期的 xls 格式

我正在使用 Windows 8.1 OS 和 excel 2013。我正在使用以下代码。

try

    if (FlUploadcsv.HasFile)
    
        string FileName = FlUploadcsv.FileName;
        string filePath = "C:\\Users\\admin\\Desktop\\Sheet1.xlsx";
        string path = filePath;// string.Concat(Server.MapPath("~/Document/" + FlUploadcsv.FileName));

        FlUploadcsv.PostedFile.SaveAs(path);

        OleDbConnection OleDbcon = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties=Excel 12.0;");

        OleDbCommand cmd = new OleDbCommand("SELECT * FROM [Sheet1$]", OleDbcon);
        OleDbDataAdapter objAdapter1 = new OleDbDataAdapter(cmd);
        ds = new DataSet();
        objAdapter1.Fill(ds);
        Dt = ds.Tables[0];
    

catch (Exception ex)



【问题讨论】:

Error External table is not in the expected format的可能重复 你能发布你的错误吗? 【参考方案1】:

首先你需要替换连接字符串:

OleDbConnection OleDbcon = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties=Excel 12.0;");

有了这个:

如果您对.xls 使用导入,请使用以下一个:

OleDbConnection OleDbcon = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + path + "; Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=2\"");

如果您对.xlsx 使用导入,请使用以下一个:

OleDbConnection OleDbcon = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + "; Extended Properties='Excel 12.0;HDR=YES;IMEX=1;';");

试试这一击:

try

    if (FlUploadcsv.HasFile)
    
        OleDbConnection OleDbcon;
        OleDbCommand cmd = new OleDbCommand(); ;
        OleDbDataAdapter objAdapter1 = new OleDbDataAdapter();
        DataSet ds = new DataSet();
        DataTable dtExcelData = new DataTable();

        string FileName = FlUploadcsv.FileName;
        string filePath = "C:\\Users\\admin\\Desktop\\Sheet1.xlsx";
        string path = filePath;// string.Concat(Server.MapPath("~/Document/" + FlUploadcsv.FileName));

        FlUploadcsv.PostedFile.SaveAs(path);

        if (Path.GetExtension(path) == ".xls")
        
            OleDbcon = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + path + "; Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=2\"");
        
        else if (Path.GetExtension(path) == ".xlsx")
        
            OleDbcon = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + "; Extended Properties='Excel 12.0;HDR=YES;IMEX=1;';");
        

        OleDbcon.Open();
        cmd.Connection = OleDbcon;
        cmd.CommandType = CommandType.Text;
        cmd.CommandText = "SELECT * FROM [Sheet1$]";
        objAdapter1 = new OleDbDataAdapter(cmd);
        objAdapter1.Fill(ds);
        dtExcelData = ds.Tables[0];

        string consString = "Your Sql Connection string";

        /* You want insert into your sql database table using SqlBulkCopy. */
        using (SqlConnection con = new SqlConnection(consString))
        
            using (SqlBulkCopy sqlBulkCopy = new SqlBulkCopy(con))
            
                //Set the database table name
                sqlBulkCopy.DestinationTableName = "SqlDatabase Table name where you want insert data";

                //[OPTIONAL]: Map the Excel columns with that of the database table
                sqlBulkCopy.ColumnMappings.Add(".xls/.xlsx Header column name(Id)", "Your database table column(IndexId)");
                .
                .
                .
                con.Open();
                sqlBulkCopy.WriteToServer(dtExcelData);
                con.Close();
            
        
        /* You want insert into your sql database table using SqlBulkCopy. */
    

catch (Exception ex)
 

【讨论】:

【参考方案2】:

这是一个类似的方法:

string filename = System.IO.Path.GetFileName(FileUpload1.FileName);
if (FileUpload1.HasFile == true) 
string fp = System.IO.Path.GetDirectoryName(FileUpload1.FileName);
string full = "C:\\Users\\user\\Documents\\" + filename;
TextBox1.Text = full;
//FileUpload1.SaveAs(Server.MapPath("Files/" + filename))
try 
    string connStr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=full;Extended Properties=Excel 8.0;HDR=YES;";
    string cmdStr = "Select * from [Sheet1$]";
    using (OleDbConnection oledbconn = new OleDbConnection(connStr)) 
        using (OleDbCommand oledbcmd = new OleDbCommand(cmdStr, oledbconn)) 
            oledbconn.Open();
            OleDbDataAdapter oledbda = new OleDbDataAdapter(oledbcmd);
            DataSet ds = new DataSet();
            oledbda.Fill(ds);
            //save to an SQL Database
            oledbconn.Close();
        
    

 catch (Exception ex) 
    TextBox2.Text = ex.ToString();


【讨论】:

以上是关于使用asp.net将Excel文件数据导入Sql server的主要内容,如果未能解决你的问题,请参考以下文章

将 Excel 导入 SQL 将日期存储为 ASP.NET 中的数值

asp.NET 中把Excel文件导入到 Oracle 数据库中怎么做,请大家帮助

我是不是需要在服务器中安装 office 2007,以便我可以使用 ASP.NET 将 excel 2007 导入 SQL?

在asp.net中如何对oracle数据库执行批量插入数据?

使用 Asp.net 将 Excel 导入 DataTable

使用 C# 和 ASP.NET 将数据从 Excel 文件导出到 SQL Server 表的最佳方法? [复制]