public static void ROC_ExcelToDatagridView (DataGridView dgv)
{
path = "";
try
{
try
{
OpenFileDialog file = new OpenFileDialog ( );
file.Filter = "Excel Files |*.xlsx";
file.Title = "Seleccione el Archivo de Excel";
if (file.ShowDialog ( ) == DialogResult.OK)
{
if (file.FileName.Equals ( "" ) == false)
{
path = file.FileName;
}
}
hoja = Microsoft.VisualBasic.Interaction.InputBox
("Escriba el nombre de la hoja!", "Excel a Datagridview" );
string conexion = @"Provider=Microsoft.ACE.OLEDB.12.0; Data
[![introducir la descripción de la imagen aquí][1]][1]Source:" + path + ";Extended Properties='Excel 12.0 Xml;HDR=Yes'";
oCon = new OleDbConnection ( conexion );
oCon.Close ( );
oCon.Open ( );
oda = new OleDbDataAdapter ( "Select * from [" + hoja + "$] where Fecha is not null and Importado is null", oCon );
dt = new DataTable ( );
oda.Fill ( dt );
dgv.DataSource = dt;
oCon.Close ( );
}
catch (OleDbException e)
{
MessageBox.Show ( "ERROR: " + hoja + " "+ path +" "+ e.Message);
}
}
catch (Exception ex)
{
MessageBox.Show ( "Error al momento de cargar los datos del Excel de la hoja " + hoja + " "+ path +" a la tabla del formulario. " + ex.Message + " " + ex.ToString ( ), "ERROR DE CARGA", MessageBoxButtons.OK, MessageBoxIcon.Error );
}
}
}
}