码迷,mamicode.com
首页 > Windows程序 > 详细

winform下读取excel文件并绑定datagridview例子

时间:2014-09-05 00:54:10      阅读:303      评论:0      收藏:0      [点我收藏+]

标签:winform   datagridview   blog   http   os   io   ar   for   文件   

首先我要读取这个excel文件然后生成Datable 

bubuko.com,布布扣

用winform编程的方式

前台界面:

bubuko.com,布布扣

 后台的代码

bubuko.com,布布扣
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];
        }


    }
}
bubuko.com,布布扣

 

实现的效果:(说明:在excel中读取的datable中列头都是F几,如F1,F2等,要自己转换)

bubuko.com,布布扣

winform下读取excel文件并绑定datagridview例子

标签:winform   datagridview   blog   http   os   io   ar   for   文件   

原文地址:http://www.cnblogs.com/kennyliu/p/3957109.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!