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;using Excel = Microsoft.Office.Interop.Excel; namespace leer_excel{ public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void btLeerExcel_Click(object sender, EventArgs e) { try { OleDbConnection MyConnection; DataSet DtSet; OleDbDataAdapter MyCommand; MyConnection = new OleDbConnection("provider=Microsoft.Jet.OLEDB.4.0;Extended Properties=Excel 8.0;"); MyCommand = new OleDbDataAdapter("select * from [Hoja1$]", MyConnection); MyCommand.TableMappings.Add("Table", "TestTable"); DtSet = new DataSet(); MyCommand.Fill(DtSet); dataGridView1.DataSource = DtSet.Tables[0]; MyConnection.Close(); } catch (Exception ex) { MessageBox.Show(ex.ToString()); } } }}
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;using System.Data.Odbc;using Excel = Microsoft.Office.Interop.Excel; namespace leer_excel{ public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void btLeerExcel_Click(object sender, EventArgs e) { try { /* OleDbConnection MyConnection; DataSet DtSet; OleDbDataAdapter MyCommand; MyConnection = new OleDbConnection("provider=Microsoft.Jet.OLEDB.4.0; "+ "Data Source=LEER.xlsx;Extended Properties=Excel 8.0;"); //MessageBox.Show("entras"); MyCommand = new OleDbDataAdapter("select * from [Hoja1$]", MyConnection); //MyCommand.TableMappings.Add("Table", "TestTable"); DtSet = new DataSet(); MyCommand.Fill(DtSet); dataGridView1.DataSource = DtSet.Tables[0]; MyConnection.Close(); */ string ruta = "E:/Proyectos_carpeta_seguridad/Visual C#/Enciclopedia de MVC#/"+ "capitulo 15/excel/leer excel/leer excel/bin/Debug/LEER.xlsx"; System.Data.Odbc.OdbcConnection conexionExcel = new System.Data.Odbc.OdbcConnection("Driver={Microsoft Excel Driver (*.xls)};Dbq=" + ruta + ";ReadOnly=0;"); System.Data.Odbc.OdbcDataAdapter adaptadorExcel = new System.Data.Odbc.OdbcDataAdapter("SELECT * FROM [Hoja1$] ", conexionExcel); System.Data.DataTable dtExcel = new System.Data.DataTable("Hoja1"); //Aquí lleno mi datatable, luego sólo le asignas al DataSource //del gridview ese datatable. adaptadorExcel.Fill(dtExcel); dataGridView1.DataSource = dtExcel; } catch (Exception ex) { MessageBox.Show(ex.ToString()); } } }}
MyConnection = new OleDbConnection("provider=Microsoft.Jet.OLEDB.4.0; Data Source=LEER.xls;Extended Properties=Excel 8.0;");
#define CadenaExcel2007#define CadenaExcel2010using 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;using System.Data.Odbc;using Excel = Microsoft.Office.Interop.Excel; namespace leer_excel{ public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void btLeerExcel_Click(object sender, EventArgs e) { try { OleDbConnection MyConnection; DataSet DtSet; OleDbDataAdapter MyCommand; String sConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;"+ "Data Source=E:/Proyectos_carpeta_seguridad/Visual C#/Enciclopedia de MVC#/capitulo 15/"+ "excel/leer excel/leer excel/bin/Debug/LEER.xlsx;"+ "Extended Properties=Excel 8.0;"; #if CadenaExcel2007 MyConnection = new OleDbConnection("provider=Microsoft.Jet.OLEDB.4.0; "+ "Data Source=LEER.xls;Extended Properties=Excel 8.0;"); #endif #if CadenaExcel2010 MyConnection = new OleDbConnection(sConnectionString); #endif MyCommand = new OleDbDataAdapter("select * from [Hoja1$]", MyConnection); MyCommand.TableMappings.Add("Table", "TestTable"); DtSet = new DataSet(); MyCommand.Fill(DtSet); dataGridView1.DataSource = DtSet.Tables[0]; MyConnection.Close(); } catch (Exception ex) { MessageBox.Show(ex.ToString()); } } }}
private void btAbrirModificarExcel_Click(object sender, EventArgs e) {// openFileDialog1.FileName = ""; openFileDialog1.Filter="Archivos Excel(*.xls)|*.xls|Archivos Excel(*.xlsx)|*.xlsx"; if (openFileDialog1.ShowDialog() == DialogResult.OK) { string ruta = openFileDialog1.FileName; try { OleDbConnection MyConnection; DataSet DtSet; OleDbDataAdapter MyCommand; String sConnectionString2010 = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + ruta + ";Extended Properties=Excel 8.0;"; String sConnectionString2007 = "provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + ruta + ";Extended Properties=Excel 8.0;"; #if CadenaExcel2007 MyConnection = new OleDbConnection(sConnectionString2007); #endif #if CadenaExcel2010 MyConnection = new OleDbConnection(sConnectionString2010); #endif MyCommand = new OleDbDataAdapter("select * from [Hoja1$]", MyConnection); // MyCommand.TableMappings.Add("Table", "TestTable"); DtSet = new DataSet(); MyCommand.Fill(DtSet); dataGridView1.DataSource = DtSet.Tables[0]; /* OleDbCommand cmd = new OleDbCommand(); cmd.Connection = new OleDbConnection(sConnectionString2010); cmd.CommandText = "UPDATE [Hoja1$B2:B2] SET F1=" + data[0]; cmd.ExecuteNonQuery(); */ MyConnection.Close(); } catch (Exception ex) { MessageBox.Show(ex.ToString()); } }
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;using Excel = Microsoft.Office.Interop.Excel; namespace AbreModificaExcel{ public partial class Form1 : Form { string sRutaArchivo = ""; public Form1() { InitializeComponent(); } private string RutaArchivo(object sender, EventArgs e) { // mediante OpenFileDialog se crea la ruta que el usuario da, guardandola en string ruta. // es una funcion con devolución, para que pueda ser usada de froma global. string ruta=""; if (openFileDialog1.ShowDialog() == DialogResult.OK) // si pulsamos Ok después de seleccionar ruta { ruta = openFileDialog1.FileName; sRutaArchivo = ruta; } return ruta; } private void AbrirConexion(object sender, EventArgs e, string ruta) { try { OleDbConnection MyConnection; DataSet DtSet; OleDbDataAdapter MyCommand; String sConnectionString2010 = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + ruta + ";Extended Properties=Excel 8.0;"; MyConnection = new OleDbConnection(sConnectionString2010); MyCommand = new OleDbDataAdapter("select * from [Hoja1$]", MyConnection); //MyCommand.TableMappings.Add("Table", "TestTable"); DtSet = new DataSet(); MyCommand.Fill(DtSet); dataGridView1.DataSource = DtSet.Tables[0]; //MyConnection.Close(); } catch (Exception ex) { MessageBox.Show(ex.ToString()); } } private void CerrarConexion(object sender, EventArgs e) { } /// abrirHojaDeDatosToolStripMenuItem_Click /// //// /// Acción dada al evento de pulsar el boton del menú "abrir...".Se le pasa la ruta de archivo desde la funcion RutaArchivo /// <param name="sender"></param> /// <param name="e"></param> private void abrirHojaDeDatosToolStripMenuItem_Click(object sender, EventArgs e) { string ruta = ""; openFileDialog1.Filter = "Archivos Excel(*.xls)|*.xls|Archivos Excel(*.xlsx)|*.xlsx"; ruta=RutaArchivo(sender,e); AbrirConexion(sender, e,ruta); } private void guardarHojaActualToolStripMenuItem_Click(object sender, EventArgs e) { OleDbConnection MyConnection; DataSet DtSet; DtSet = new DataSet(); //OleDbDataAdapter MyCommand; String sConnectionString2010 = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + sRutaArchivo + ";Extended Properties=Excel 8.0;"; MyConnection = new OleDbConnection(sConnectionString2010); OleDbCommand cmd = new OleDbCommand(); cmd.CommandText = "UPDATE [Hoja1$] "; dataGridView1.DataSource = DtSet; //cmd.ExecuteNonQuery(); MyConnection.Close(); } }}