如何通过asp.net和vb.net将excel表(.xlxs)导入sql server 2012 db

时间:2017-03-31 11:50:52

标签: asp.net sql-server vb.net sql-server-2012

这是我第一次在这里发帖提问。

我在网上有很多关于如何通过asp.net和vb.net将Excel工作表(.xlxs)上传到sql server 2012数据库的研究,但不幸的是,所有这些研究都毫无用处。

有人可以帮我解决一下源代码。

感谢。

1 个答案:

答案 0 :(得分:0)

SqlBulkCopy类顾名思义是从一个源批量插入到另一个源,因此可以使用SqlBulkCopy类轻松读取和插入Excel表格中的所有行。

<add name = "constr" connectionString = "Data 
 Source=.\SQL2008R2;Database=dbname;User Id=abc;password=xxxxx"/>

我已在Web.Config文件中为2003和2007或更高格式的Excel文件指定了连接字符串。

<add name = "Excel03ConString" connectionString = 
"Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties='Excel 
 8.0;HDR=YES'"/>
 <add name = "Excel07+ConString" connectionString = 
 "Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended 
  Properties='Excel 8.0;HDR=YES'"/>

HTML标记包含ASP.Net FileUpload控件和用于触发文件上载过程的按钮。

<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:Button Text="Upload" OnClick = "Upload" runat="server" />

您需要导入以下命名空间。

using System.IO;
using System.Data;
using System.Data.OleDb;
using System.Data.SqlClient;
using System.Configuration;

protected void Upload(object sender, EventArgs e)
{
 //Upload and save the file
  string excelPath = Server.MapPath("~/Files/") + 
  Path.GetFileName(FileUpload1.PostedFile.FileName);
  FileUpload1.SaveAs(excelPath);

string conString = string.Empty;
string extension = Path.GetExtension(FileUpload1.PostedFile.FileName);
switch (extension)
{
    case ".xls": //Excel 97-03
        conString = ConfigurationManager.ConnectionStrings["Excel03ConString"].ConnectionString;
        break;
    case ".xlsx": //Excel 07 or higher
        conString = 


ConfigurationManager.ConnectionStrings["Excel07+ConString"].ConnectionString;
        break;

}
conString = string.Format(conString, excelPath);
using (OleDbConnection excel_con = new OleDbConnection(conString))
{
    excel_con.Open();
    string sheet1 = excel_con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, 
    null).Rows[0]["TABLE_NAME"].ToString();
    DataTable dtExcelData = new DataTable();

    //[OPTIONAL]: It is recommended as otherwise the data will be considered 
    as String by default.
    dtExcelData.Columns.AddRange(new DataColumn[3] { new DataColumn("Id", 
    typeof(int)),
            new DataColumn("Name", typeof(string)),
            new DataColumn("Salary",typeof(decimal)) });

    using (OleDbDataAdapter oda = new OleDbDataAdapter("SELECT * FROM [" + 
   sheet1 + "]", excel_con))
    {
        oda.Fill(dtExcelData);
    }
    excel_con.Close();

    string consString = 
    ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
    using (SqlConnection con = new SqlConnection(consString))
    {
        using (SqlBulkCopy sqlBulkCopy = new SqlBulkCopy(con))
        {
            //Set the database table name
            sqlBulkCopy.DestinationTableName = "dbo.tblPersons";

            //[OPTIONAL]: Map the Excel columns with that of the database table
            sqlBulkCopy.ColumnMappings.Add("Id", "PersonId");
            sqlBulkCopy.ColumnMappings.Add("Name", "Name");
            sqlBulkCopy.ColumnMappings.Add("Salary", "Salary");
            con.Open();
            sqlBulkCopy.WriteToServer(dtExcelData);
            con.Close();
        }
    }
}
 }

来源是 https://www.aspsnippets.com/Articles/Using-SqlBulkCopy-to-import-Excel-SpreadSheet-data-into-SQL-Server-in-ASPNet-using-C-and-VBNet.aspx 如需进一步帮助,请访问此 希望它对你有所帮助