Ver Mensaje Individual
  #4 (permalink)  
Antiguo 30/08/2012, 08:26
Alfilxdama
 
Fecha de Ingreso: enero-2002
Mensajes: 136
Antigüedad: 22 años, 3 meses
Puntos: 1
Respuesta: Importar archivo de excel

Te paso un ejemplo sencillo de un trabajo parecido que me tocó hacer con un archivo excel PreciosRef.xlsx y una hoja Precios$

DataTable dt_excel = new DataTable();
string folder = Server.MapPath("~") + ConfigurationManager.AppSettings["file_path"] + "PreciosRef.xlsx";
string ConnStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + folder + ";Extended Properties='Excel 8.0;HDR=NO;IMEX=1';";
using (OleDbConnection connection = new OleDbConnection(ConnStr))
{
connection.Open();
OleDbCommand cmd_1 = new OleDbCommand("SELECT * FROM [Precios$]", connection);
OleDbDataAdapter sda = new OleDbDataAdapter(cmd_1);
DataSet ds_excel = new DataSet();
sda.Fill(ds_excel);
connection.Close();
dt_excel = ds_excel.Tables[0];
}

Saludos