Ver Mensaje Individual
  #2 (permalink)  
Antiguo 22/01/2011, 07:14
Avatar de gakutaru
gakutaru
 
Fecha de Ingreso: agosto-2005
Ubicación: frente a mi NtbK
Mensajes: 239
Antigüedad: 18 años, 8 meses
Puntos: 6
Respuesta: Como pueso pubir una panilla excel

Ok, mira, la estructura del excel es la siguiente

idExcel DatoExcel Datos
1 abc 0
2 def #¿NOMBRE?
3 ghi

y nombre de la hoja es "Hoja1"

el codigo ASPX seria:

Código ASP:
Ver original
  1. <form runat="server">
  2. <asp:FileUpload ID="uplExcel" runat="server" /><br />
  3. <asp:Button ID="Button1" runat="server" OnClick="clcikSubirArchivo" Text="Subir Archivo" />
  4. <asp:Button ID="Button2" runat="server" OnClick="leerExcel" Text="Leer Contenido" /><br />
  5. <asp:Label ID="lblResultado" runat="server"></asp:Label><br />
  6. </form>

y en el behind seria asi:

para subir el excel al servdor (y cualquier archivo..
Código C:
Ver original
  1. protected void clcikSubirArchivo(object sender, EventArgs e)
  2.     {
  3.         if (uplExcel.HasFile)//si tiene un archivo
  4.         {
  5.             string strArchivo = uplExcel.FileName;
  6.             string strExtencion = Path.GetExtension(strArchivo);
  7.             if ((strExtencion == ".xls") || (strExtencion == ".XLS"))
  8.             {
  9.                 string strNombreArchivo = "FNI54FR" + strExtencion;
  10.                 string strFileName = Path.Combine(Server.MapPath("~/Recib"), strNombreArchivo);
  11.                 uplExcel.SaveAs(strFileName);
  12.  
  13.             }//fin extencion
  14.         }//fin uplexcel
  15.     }//fin subir archivo
"Recib" es el nombre de la carpeta virtual dentro de la carpeta de tu projecto
"C:\Inetpub\wwwroot\TuProjecto\Recib"

para leer el contenido seria lo siguiente:
Código C:
Ver original
  1. protected void leerExcel(object s, EventArgs e)
  2.     {
  3.         leerExcel2();
  4.     }
  5.  
  6.     protected void leerExcel2()
  7.     {
  8.         //para leer un excel se lee como oledb
  9.         string strConExcel = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Inetpub\wwwroot\Recib\FNI54FR.xls;Extended Properties=""Excel 8.0;HDR=YES;""";
  10.         DbProviderFactory factory = DbProviderFactories.GetFactory("System.Data.OleDb");
  11.  
  12.         using (DbConnection conConExcel = factory.CreateConnection())
  13.         {
  14.             conConExcel.ConnectionString = strConExcel;
  15.             using (DbCommand cmdConExcel = conConExcel.CreateCommand())
  16.             {
  17.                 cmdConExcel.CommandText = "Select idExcel, DatoExcel, Datos From [Hoja1$]";
  18.                 conConExcel.Open();
  19.                 using (DbDataReader dtrConExcel = cmdConExcel.ExecuteReader())
  20.                 {
  21.                     while (dtrConExcel.Read())
  22.                     {
  23.                         lblResultado.Text += dtrConExcel["idExcel"].ToString() + " " + dtrConExcel["DatoExcel"].ToString() + " " + dtrConExcel["Datos"].ToString() + "</br>";
  24.                     }
  25.                 }//fin using dtrConExcel
  26.                 conConExcel.Close();
  27.             }//fin using cmdConExcel
  28.         }//fin using conConExcel
  29.            
  30.     }//fin leer excel

y debe llamar a las librerias
using System.IO;
using System.Reflection;
using System;
using System.Data;

Espero te sirva.