国产探花免费观看_亚洲丰满少妇自慰呻吟_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));

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 馆陶县| 吐鲁番市| 乌兰察布市| 栖霞市| 汝城县| 应城市| 同江市| 米脂县| 梓潼县| 若尔盖县| 沂源县| 石城县| 蒙自县| 沈丘县| 江孜县| 安平县| 阿鲁科尔沁旗| 苍山县| 滦平县| 筠连县| 岳普湖县| 屏东县| 蓬溪县| 铁力市| 焦作市| 洞口县| 应城市| 甘洛县| 阿克苏市| 古交市| 丰台区| 青阳县| 呼和浩特市| 合阳县| 佳木斯市| 奉节县| 蓬莱市| 类乌齐县| 临邑县| 洪泽县| 射洪县|