国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C# > 正文

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

2024-09-07 17:05:29
字體:
來源:轉載
供稿:網友

假如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));

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 云南省| 纳雍县| 乌兰浩特市| 茶陵县| 达日县| 多伦县| 巨野县| 青州市| 万全县| 樟树市| 扶绥县| 周至县| 锡林郭勒盟| 密山市| 扎赉特旗| 新巴尔虎左旗| 安图县| 攀枝花市| 三门峡市| 中阳县| 西林县| 太原市| 枣阳市| 凤山市| 石台县| 台南市| 平邑县| 儋州市| 开封县| 安泽县| 德化县| 布尔津县| 成安县| 个旧市| 泸定县| 上虞市| 紫金县| 崇信县| 康平县| 丹阳市| 抚远县|