一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務(wù)器之家 - 編程語言 - ASP.NET教程 - c#將Excel數(shù)據(jù)導(dǎo)入到數(shù)據(jù)庫的實(shí)現(xiàn)代碼

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

2019-11-24 14:38C#教程網(wǎng) ASP.NET教程

這篇文章主要介紹了c#將Excel數(shù)據(jù)導(dǎo)入到數(shù)據(jù)庫的實(shí)現(xiàn)代碼,有需要的朋友可以參考一下

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

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

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

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

其中Id為自增字段:

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

代碼:

復(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的第一個(gè)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#將Excel數(shù)據(jù)導(dǎo)入到數(shù)據(jù)庫的實(shí)現(xiàn)代碼

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 欧美综合国产精品日韩一 | 女被男啪到哭 | 羞羞视频动漫 | 国产清纯女高中生在线观看 | 精品国产午夜久久久久九九 | 美国69xxxx59 | 国产成人高清精品免费5388密 | 微福利92合集 | 国产午夜视频在线观看网站 | 免费观看韩剧网站在线观看 | 精品国产一区二区三区在线观看 | 男人的j插入女人的p | 国产卡一卡二卡三卡四 | 色悠久久久久综合欧美99 | 日本高清免费看 | 久草在线福利资站免费视频 | caoporn草棚在线视频 | 久久精品国产久精国产果冻传媒 | 男女羞羞的视频 | 我的男友是消防员在线观看 | 无敌在线视频观看免费 | 国产在线精品成人一区二区三区 | 四虎精品永久在线网址 | 美女天天色 | 亚洲成人一区在线 | 校园全黄h全肉细节文 | 午夜久久影院 | 男女真实无遮挡xx00动态图软件 | 国产精品久久久久jk制服 | 奇米狠狠色 | 大学生特黄特色大片免费播放 | 4hu影院在线观看 | av毛片在线看 | 色偷偷亚洲男人 | 波多在线| 性妲己 | 精品一区久久 | 大好硬好深好爽想要视频 | 2020年国产精品午夜福利在线观看 | 国产欧美综合精品一区二区 | 草草视频免费看 |