winform下讀取excel文件並綁定datagridview例子。


首先我要讀取這個excel文件然后生成Datable 

用winform編程的方式

前台界面:

 后台的代碼

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

namespace 讀Excel文件
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 選擇文件按鈕
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            if (this.openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                this.textBox1.Text = this.openFileDialog1.FileName;
            }
        }
        /// <summary>
        /// 點擊導出excel按鈕
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            string File = this.openFileDialog1.FileName;
            DataTable dt = ExcelUp(File);
            dataGridView1.AutoGenerateColumns = false;
            dataGridView1.DataSource = dt;

        }
        /// <summary>
        /// 讀取指定excel表中的內容返回datatable
        /// </summary>
        /// <param name="fileName">文件地址</param>
        /// <returns>表中內容</returns>
        public DataTable ExcelUp(string fileName)
        {
            string filePath = fileName;//讀取excel文件路徑;

            DataTable dt = GetDataTable("Sheet1", filePath);

            return dt;

        }

        /// <summary>
        /// 讀取excel指定頁中的內容
        /// </summary>
        /// <param name="strSheetName">頁名</param>
        /// <param name="strExcelFileName">excel路徑</param>
        /// <returns></returns>
        protected DataTable GetDataTable(string strSheetName, string strExcelFileName)
        {
            //源的定義
            string strConn = string.Format("Provider=Microsoft.ACE.OLEDB.12.0;" + "Data Source={0};" + "Extended Properties='Excel 8.0;HDR=NO;IMEX=1';", strExcelFileName);

            //Sql語句
            string strExcel = string.Format("select * from [{0}$]", strSheetName);

            //定義存放的數據表
            DataSet ds = new DataSet();

            //連接數據源
            OleDbConnection conn = new OleDbConnection(strConn);

            try
            {
                conn.Open();
                //適配到數據源
                OleDbDataAdapter adapter = new OleDbDataAdapter(strExcel, strConn);
                adapter.Fill(ds, strSheetName);
            }
            catch (Exception e)
            {
                throw e;
            }
            finally
            {
                conn.Close();
            }
            return ds.Tables[strSheetName];
        }


    }
}

 

實現的效果:(說明:在excel中讀取的datable中列頭都是F幾,如F1,F2等,要自己轉換)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM