C#开发——winform中将Excel数据导入DataGridView
来源:互联网 发布:ubuntu 移除软件 编辑:程序博客网 时间:2024/05/17 22:30
using System;using System.Data;using System.Data.OleDb;using System.Windows.Forms;namespace ExcelToGridview{ public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void btnopenfile_Click(object sender, EventArgs e) { try { //获取Excel文件路径和名称 OpenFileDialog odXls = new OpenFileDialog(); //指定相应的打开文档的目录 AppDomain.CurrentDomain.BaseDirectory定位到Debug目录,再根据实际情况进行目录调整 string folderPath = AppDomain.CurrentDomain.BaseDirectory + @"databackup\"; odXls.InitialDirectory = folderPath; // 设置文件格式 odXls.Filter = "Excel files office2003(*.xls)|*.xls|Excel office2010(*.xlsx)|*.xlsx|All files (*.*)|*.*"; //openFileDialog1.Filter = "图片文件(*.jpg)|*.jpg|(*.JPEG)|*.jpeg|(*.PNG)|*.png"; odXls.FilterIndex = 2; odXls.RestoreDirectory = true; if (odXls.ShowDialog() == DialogResult.OK) { this.txtFilePath.Text = odXls.FileName; this.txtFilePath.ReadOnly = true; string sConnString = string.Format("Provider=Microsoft.ACE.OLEDB.12.0;" + "Data Source={0};" + "Extended Properties='Excel 8.0;HDR=NO;IMEX=1';", odXls.FileName); if ((System.IO.Path.GetExtension(txtFilePath.Text.Trim())).ToLower() == ".xls") { sConnString = "Provider=Microsoft.Jet.OLEDB.4.0;" + "data source=" + odXls.FileName + ";Extended Properties=Excel 5.0;Persist Security Info=False"; //sConnString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + txtFilePath.Text.Trim() + ";Extended Properties=\"Excel 8.0;HDR=" + strHead + ";IMEX=1\""; } using (OleDbConnection oleDbConn = new OleDbConnection(sConnString)) { oleDbConn.Open(); DataTable dt = oleDbConn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { null, null, null, "TABLE" }); //判断是否cmb中已有数据,有则清空 if (cmbtablename.Items.Count > 0) { cmbtablename.DataSource = null; cmbtablename.Items.Clear(); } //遍历dt的rows得到所有的TABLE_NAME,并Add到cmb中 foreach (DataRow dr in dt.Rows) { cmbtablename.Items.Add((String)dr["TABLE_NAME"]); } if (cmbtablename.Items.Count > 0) { cmbtablename.SelectedIndex = 0; } //加载Excel文件数据按钮 this.btnshow.Enabled = true; } } } catch (Exception ex) { MessageBox.Show(ex.Message); } } private void btnshow_Click(object sender, EventArgs e) { #region 读取相应的表名的Excel文件中数据到当前DataGridview中显示 OleDbConnection ole = null; OleDbDataAdapter da = null; DataTable dt = null; string strConn = string.Format("Provider=Microsoft.ACE.OLEDB.12.0;" + "Data Source={0};" + "Extended Properties='Excel 8.0;HDR=NO;IMEX=1';", txtFilePath.Text.Trim()); if ((System.IO.Path.GetExtension(txtFilePath.Text.Trim())).ToLower() == ".xls") { strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" + "data source=" + txtFilePath.Text.Trim() + ";Extended Properties=Excel 5.0;Persist Security Info=False"; //sConnString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + txtFilePath.Text.Trim() + ";Extended Properties=\"Excel 8.0;HDR=" + strHead + ";IMEX=1\""; } string sTableName = cmbtablename.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); this.dgvdata.DataSource = dt; //因为生成Excel的时候第一行是标题,所以要做如下操作: //1.修改DataGridView列头的名字, //2.数据列表中删除第一行 for (int i = 0; i < dt.Columns.Count; i++) { //dgvdata.Columns[i].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells; //dgvdata.Columns[i].Name = dt.Columns[i].ColumnName; dgvdata.Columns[i].HeaderCell.Value = dt.Rows[0][i].ToString();//c# winform 用代码修改DataGridView列头的名字,设置列名,修改列名 } //DataGridView删除行 dgvdata.Rows.Remove(dgvdata.Rows[0]);//删除第一行 //dgvdata.Rows.Remove(dgvdata.CurrentRow);//删除当前光标所在行 //dgvdata.Rows.Remove(dgvdata.Rows[dgvdata.Rows.Count - 1]);//删除最后一行 //dgvdata.Rows.Clear();//删除所有行 ole.Close(); } catch (Exception ex) { MessageBox.Show(ex.Message); } finally { if (ole != null) ole.Close(); } #endregion }
0 0
- C#开发——winform中将Excel数据导入DataGridView
- C# WinForm程序中将DataGridView中的数据导出为EXCEL
- c#将datagridview中的数据导入到Excel中(winForm)
- WinForm中DataGridView数据导入Excel
- winform DataGridView的数据导入Excel
- C#中将DataTable数据集导入 Excel
- C#中将DataGridView中数据导出至Excel
- Winform datagridview Excel 导入导出
- C#Winform 将DataGridView中的数据导入到Excel中
- C# datagridview 导入excel
- C# winform DataGridView导出数据到Excel中,可以导出当前页和全部数据;从Excel导入到DataGridView[转http://yuunagi.blogbus.com/logs/38361242.html]
- datagridview数据导入Excel
- datagridview数据导入Excel
- C# — WinForm DataGridView篇
- C# — WinForm DataGridView篇
- 如何提高C#中将数据导入到Excel的性能
- C#中将dataGridView导到excel
- C#将数据从datagridView中导入到Excel
- hdu-携程复赛-最短路径的代价-抠图+最小割
- 接口和抽象类有什么区别
- Guide Window
- 反射与正则
- android的service本地服务的理解
- C#开发——winform中将Excel数据导入DataGridView
- java 分页技术
- HTML笔记——HTML CSS样式简介
- 火狐和谷歌浏览器屏蔽键盘按键Backspace回退网页
- java集合框架概述
- SQLiteOpenHelper-API
- extjs的CycleButton的changeHandler和textchange事件
- 小节我目前知道的几种堆内存分配模式
- GDB调试fork+exec创建的子进程的方法