C# winfrom Excel导入sqlserver数据库

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.OleDb;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;


namespace 读取excel到datagridview
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
        }

        /// 
        /// 选择文件,并且读取excel中sheet
        /// 
        /// 
        /// 
        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                //获取Excel文件路径和名称
                OpenFileDialog odXls = new OpenFileDialog();
                // 指定相应的打开文档的目录
                odXls.InitialDirectory = "C://";
                // 设置文件格式
                odXls.Filter = "Excel files (*.xls)|*.xls|Excel files (*.xlsx)|*.xlsx";
                odXls.FilterIndex = 2;
                odXls.RestoreDirectory = true;
                if (odXls.ShowDialog() == DialogResult.OK)
                {
                    txtFilePath.Text = odXls.FileName;
                    OleDbConnection oledbConn = null;
                    string sConnString = "provider=Microsoft.ACE.OLEDB.12.0;data source=" + odXls.FileName + ";Extended Properties=Excel 12.0;Persist Security Info=False";
                    oledbConn = new OleDbConnection(sConnString);
                    oledbConn.Open();
                    DataTable dt = oledbConn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { null, null, null, "TABLE" });
                    combox1.Items.Clear();
                    foreach (DataRow dr in dt.Rows)
                    {
                       
                        combox1.Items.Add((String)dr["TABLE_NAME"]);
                    }
                    if (combox1.Items.Count > 0)
                        combox1.SelectedIndex = 0;
                }
            }
            catch (Exception Ex)
            {
                MessageBox.Show(Ex.Message);
                richTextBox1.Text = Ex.Message;
            }
        }

        /// 
        /// 读取文件具体内容
        /// 
        /// 
        /// 
        private void button2_Click(object sender, EventArgs e)
        {
            string connectionString = @"Data Source=ZHANGP;Initial Catalog=Test_Z;Integrated Security=True";
            OleDbConnection ole = null;
            OleDbDataAdapter da = null;
            DataTable dt = null;


            string strConn = "Provider=Microsoft.ACE.OLEDB.12.0;"
                            + "Data Source=" + txtFilePath.Text.Trim() + ";"
                            + "Extended Properties=Excel 12.0";
            string sTableName = combox1.Text.Trim();
            string strExcel = "select * from [" + sTableName + "]";
            try
            {
                ole = new OleDbConnection(strConn);
                ole.Open();
                da = new OleDbDataAdapter(strExcel, ole);
                dt = new DataTable();
                da.Fill(dt);
                using (System.Data.SqlClient.SqlBulkCopy bcp = new System.Data.SqlClient.SqlBulkCopy(connectionString))
                {
                
                    bcp.BatchSize = 1000;//每次传输的行数    
                    bcp.DestinationTableName = "SA_CusTotal";//目标表
                    bcp.WriteToServer(dt);
                    MessageBox.Show("导入完成!");
                }
                //为datagridview设置数据源
                this.xlsExpData.DataSource = dt;
                ole.Close();
            }
            catch (Exception Ex)
            {
                MessageBox.Show(Ex.Message);
            }
            finally
            {
                if (ole != null)
                    ole.Close();
            }
        }
        //进度显示  
        void bcp_SqlRowsCopied(object sender, System.Data.SqlClient.SqlRowsCopiedEventArgs e)
        {
            this.Text = e.RowsCopied.ToString();
            this.Update();
        }
    }
}

 

你可能感兴趣的:(C#)