C# excel数据导入

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 计算员工考勤信息
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            this.openFileDialog1.DefaultExt = "xls";
            this.openFileDialog1.Filter = "Excel文件 (*.xls)|*.xls|(*.csv)|*.csv";
            this.saveFileDialog1.DefaultExt = "xls";
            this.saveFileDialog1.Filter = "Excel文件 (*.xls)|*.xls|(*.csv)|*.csv";
        }
       string url="";
       string fileName="";//excel文件名
        //选择excel表
        private void btnBrowsing_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                this.txtUrl.Text = this.openFileDialog1.FileName;
                url=txtUrl.Text;
                fileName=url.Substring(url.LastIndexOf("//"));
            }
        }
        //导入excel表
        private void btnExport_Click(object sender, EventArgs e)
        {
            if (this.txtUrl.Text.Trim() == "")
            {
                MessageBox.Show("请选择excel表!");
            }
            else
            {
                //导入excel
              this.panel1.Visible = true;
              ExcelToDB(url);
            }
         }

        /// <summary>
        /// excel数据导入
        /// </summary>
        /// <param name="path">excel文件路径</param>
        public void ExcelToDB(string path)
        {
            DataSet ds = new DataSet();
            string connectionString = string.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties=/"Excel 8.0;HDR=Yes;IMEX=1/"", path);
            OleDbConnection connection = new OleDbConnection(connectionString);
            OleDbDataAdapter adapter = new OleDbDataAdapter("select * from [预定表$]", connection);
            adapter.Fill(ds);
            this.dataGridView1.DataSource = ds.Tables[0];
        }

        //导出excel表
        private void btnDerived_Click(object sender, EventArgs e)
        {

        }
        public void Bind(DataGridView dv,DataSet ds)
        {
            dv.DataSource = ds.Tables["ExcelInfo"];
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            DialogResult result = MessageBox.Show("确定要离开吗?", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
            if (result == DialogResult.OK)
            {
                Application.Exit();
            }
            else
            {
                return;
            }
        }
    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

邹琼俊

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值