class Importar
{
OleDbConnection conexion;
OleDbDataAdapter MyDataAdapter;
DataTable dt;
public string celda { get; set; }
public void ImportarExcel (DataGridView dgv, string nombreHoja)
{
string ruta = "";
try
{
OpenFileDialog openfile = new OpenFileDialog();
openfile.Filter = "Excel Files |*.xls;*.xlsx;*.xlsm";
openfile.Title = "";
if (openfile.ShowDialog () == System.Windows.Forms.DialogResult.OK)
{
if (openfile.FileName.Equals("") == false)
{
ruta = openfile.FileName;
}
}
conexion = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;"+"Data Source=" + ruta + ";Extended Properties='Excel 12.0 Xml;HDR=YES;'");
MyDataAdapter = new OleDbDataAdapter("Select * From [" + nombreHoja + "$]", conexion);
dt = new DataTable();
MyDataAdapter.Fill(dt);
//celda = dt.Rows[6][2].ToString();
dgv.DataSource = dt;
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
}
class Importar
{
OleDbConnection conexion;
OleDbDataAdapter MyDataAdapter;
DataTable dt;
public string celda { get; set; }
public void ImportarExcel (DataGridView dgv, string nombreHoja)
{
string ruta = "";
try
{
OpenFileDialog openfile = new OpenFileDialog();
openfile.Filter = "Excel Files |*.xls;*.xlsx;*.xlsm";
openfile.Title = "";
if (openfile.ShowDialog () == System.Windows.Forms.DialogResult.OK)
{
if (openfile.FileName.Equals("") == false)
{
ruta = openfile.FileName;
}
}
conexion = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;"+"Data Source=" + ruta + ";Extended Properties='Excel 12.0 Xml;HDR=YES;'");
MyDataAdapter = new OleDbDataAdapter("Select * From [" + nombreHoja + "$]", conexion);
dt = new DataTable();
MyDataAdapter.Fill(dt);
//celda = dt.Rows[6][2].ToString();
dgv.DataSource = dt;
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
}