濮阳杆衣贸易有限公司

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

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

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

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

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

其中Id為自增字段:

代碼:

復(fù)制代碼 代碼如下:

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ù)導(dǎo)入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代表第一行是標(biāo)題,不是數(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導(dǎo)入數(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;
        }
    }
}

運(yùn)行結(jié)果:

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

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

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《c#將Excel數(shù)據(jù)導(dǎo)入到數(shù)據(jù)庫的實現(xiàn)代碼》,本文關(guān)鍵詞  將,Excel,數(shù)據(jù),導(dǎo)入,到,數(shù)據(jù)庫,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《c#將Excel數(shù)據(jù)導(dǎo)入到數(shù)據(jù)庫的實現(xiàn)代碼》相關(guān)的同類信息!
  • 本頁收集關(guān)于c#將Excel數(shù)據(jù)導(dǎo)入到數(shù)據(jù)庫的實現(xiàn)代碼的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    永兴县| 图木舒克市| 洱源县| 连南| 米泉市| 博野县| 尼勒克县| 崇文区| 玛纳斯县| 台山市| 武安市| 中江县| 乐陵市| 孟津县| 八宿县| 康平县| 修武县| 集安市| 高安市| 奉节县| 福贡县| 肃北| 哈密市| 新巴尔虎左旗| 宜州市| 施甸县| 贵定县| 镇坪县| 天全县| 循化| 赤壁市| 开原市| 平泉县| 会宁县| 深水埗区| 台中县| 修文县| 九江市| 唐河县| 大关县| 米泉市|