婷婷综合国产,91蜜桃婷婷狠狠久久综合9色 ,九九九九九精品,国产综合av

主頁 > 知識庫 > c#將Excel數據導入到數據庫的實現代碼

c#將Excel數據導入到數據庫的實現代碼

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

假如Excel中的數據如下:

數據庫建表如下:

其中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數據導入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代表第一行是標題,不是數據;
            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中的數據
                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導入數據到數據庫
        /// /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;
        }
    }
}

運行結果:

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

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

巨人網絡通訊聲明:本文標題《c#將Excel數據導入到數據庫的實現代碼》,本文關鍵詞  將,Excel,數據,導入,到,數據庫,;如發現本文內容存在版權問題,煩請提供相關信息告之我們,我們將及時溝通與處理。本站內容系統采集于網絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《c#將Excel數據導入到數據庫的實現代碼》相關的同類信息!
  • 本頁收集關于c#將Excel數據導入到數據庫的實現代碼的相關信息資訊供網民參考!
  • 推薦文章
    主站蜘蛛池模板: 南乐县| 迁西县| 扶绥县| 通许县| 宝清县| 稷山县| 绩溪县| 双城市| 临夏市| 新安县| 隆德县| 遵义市| 阜阳市| 同心县| 崇仁县| 柯坪县| 肃宁县| 紫阳县| 龙游县| 洛扎县| 阳西县| 贺州市| 独山县| 武平县| 杭州市| 广宗县| 阜平县| 宜兰县| 若尔盖县| 云南省| 故城县| 石景山区| 密云县| 丹江口市| 环江| 福清市| 广灵县| 天水市| 田阳县| 杭锦旗| 洛阳市|