主頁 > 知識庫 > c#將Excel數(shù)據(jù)導入到數(shù)據(jù)庫的實現(xiàn)代碼

c#將Excel數(shù)據(jù)導入到數(shù)據(jù)庫的實現(xiàn)代碼

熱門標簽:廣州銷售外呼系統(tǒng)定制 電銷機器人 數(shù)據(jù) 怎樣給陜西地圖標注顏色 云狐人工智能電話機器人 400電話辦理信任翰諾科技 宿遷智能外呼系統(tǒng)排名 福州人工智能電銷機器人加盟 ai電銷機器人對貸款有幫助嗎 地圖標注多少錢一張

假如Excel中的數(shù)據(jù)如下:

數(shù)據(jù)庫建表如下:

其中Id為自增字段:

代碼:

復制代碼 代碼如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.OleDb;
using System.Configuration;
using System.Data.SqlClient;

namespace InExcelOutExcel
{
    public partial class ExcelToDB : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            FileSvr fileSvr = new FileSvr();
            System.Data.DataTable dt = fileSvr.GetExcelDatatable("C:\\Users\\NewSpring\\Desktop\\Demo\\InExcelOutExcel\\InExcelOutExcel\\excel\\ExcelToDB.xlsx", "mapTable");
            fileSvr.InsetData(dt);
        }
    }
    class FileSvr
    {
        /// summary>
        /// Excel數(shù)據(jù)導入Datable
        /// /summary>
        /// param name="fileUrl">/param>
        /// param name="table">/param>
        /// returns>/returns>
        public System.Data.DataTable GetExcelDatatable(string fileUrl, string table)
        {
            //office2007之前 僅支持.xls
            //const string cmdText = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties='Excel 8.0;IMEX=1';";
            //支持.xls和.xlsx,即包括office2010等版本的   HDR=Yes代表第一行是標題,不是數(shù)據(jù);
            const string cmdText = "Provider=Microsoft.Ace.OleDb.12.0;Data Source={0};Extended Properties='Excel 12.0; HDR=Yes; IMEX=1'";

            System.Data.DataTable dt = null;
            //建立連接
            OleDbConnection conn = new OleDbConnection(string.Format(cmdText, fileUrl));
            try
            {
                //打開連接
                if (conn.State == ConnectionState.Broken || conn.State == ConnectionState.Closed)
                {
                    conn.Open();
                }


                System.Data.DataTable schemaTable = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);

                //獲取Excel的第一個Sheet名稱
                string sheetName = schemaTable.Rows[0]["TABLE_NAME"].ToString().Trim();

                //查詢sheet中的數(shù)據(jù)
                string strSql = "select * from [" + sheetName + "]";
                OleDbDataAdapter da = new OleDbDataAdapter(strSql, conn);
                DataSet ds = new DataSet();
                da.Fill(ds, table);
                dt = ds.Tables[0];

                return dt;
            }
            catch (Exception exc)
            {
                throw exc;
            }
            finally
            {
                conn.Close();
                conn.Dispose();
            }

        }

        /// summary>
        /// 從System.Data.DataTable導入數(shù)據(jù)到數(shù)據(jù)庫
        /// /summary>
        /// param name="dt">/param>
        /// returns>/returns>
        public int InsetData(System.Data.DataTable dt)
        {
            int i = 0;
            string lng = "";
            string lat = "";
            string offsetLNG = "";
            string offsetLAT = "";

            foreach (DataRow dr in dt.Rows)
            {
                lng = dr["LNG"].ToString().Trim();
                lat = dr["LAT"].ToString().Trim();
                offsetLNG = dr["OFFSET_LNG"].ToString().Trim();
                offsetLAT = dr["OFFSET_LAT"].ToString().Trim();

                //sw = string.IsNullOrEmpty(sw) ? "null" : sw;
                //kr = string.IsNullOrEmpty(kr) ? "null" : kr;

                string strSql = string.Format("Insert into DBToExcel (LNG,LAT,OFFSET_LNG,OFFSET_LAT) Values ('{0}','{1}',{2},{3})", lng, lat, offsetLNG, offsetLAT);

                string strConnection = ConfigurationManager.ConnectionStrings["ConnectionStr"].ToString();
                SqlConnection sqlConnection = new SqlConnection(strConnection);
                try
                {
                    // SqlConnection sqlConnection = new SqlConnection(strConnection);
                    sqlConnection.Open();
                    SqlCommand sqlCmd = new SqlCommand();
                    sqlCmd.CommandText = strSql;
                    sqlCmd.Connection = sqlConnection;
                    SqlDataReader sqlDataReader = sqlCmd.ExecuteReader();
                    i++;
                    sqlDataReader.Close();
                }
                catch (Exception ex)
                {
                    throw ex;
                }
                finally
                {
                    sqlConnection.Close();

                }
                //if (opdb.ExcSQL(strSql))
                //    i++;
            }
            return i;
        }
    }
}

運行結(jié)果:

您可能感興趣的文章:
  • C#導入導出Excel數(shù)據(jù)的兩種方法
  • 讓C# Excel導入導出 支持不同版本Office
  • C#的Excel導入、導出
  • C#實現(xiàn)Excel導入sqlite的方法
  • C#導入導出EXCEL文件的代碼實例
  • c#使用EPPlus封裝excel表格導入功能的問題

標簽:曲靖 延安 黃南 宜春 綿陽 大興安嶺 新疆 焦作

巨人網(wǎng)絡通訊聲明:本文標題《c#將Excel數(shù)據(jù)導入到數(shù)據(jù)庫的實現(xiàn)代碼》,本文關鍵詞  將,Excel,數(shù)據(jù),導入,到,數(shù)據(jù)庫,;如發(fā)現(xiàn)本文內(nèi)容存在版權問題,煩請?zhí)峁┫嚓P信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《c#將Excel數(shù)據(jù)導入到數(shù)據(jù)庫的實現(xiàn)代碼》相關的同類信息!
  • 本頁收集關于c#將Excel數(shù)據(jù)導入到數(shù)據(jù)庫的實現(xiàn)代碼的相關信息資訊供網(wǎng)民參考!
  • 推薦文章