ASP.NET - Exportar a excel ASP.net

 
Vista:

Exportar a excel ASP.net

Publicado por Freddy (1 intervención) el 26/12/2012 22:08:30
Estimados, tengo un div de la siguiente forma en la pagina .aspx

<div id="testing" runat="server" style="width:100%"></div>

Luego por codebehind crean una tabla en ese div como InnerHtml (testing.InnerHtml = ...)

Mi pregunta es como puedo exportar esa tabla dinamica que se genera por codebehind. No lo puedo hacer con el nombre de testing porque no es el nombre de la tabla.

Gracias de antemano
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder
Imágen de perfil de Edgar Zambrano

Exportar a excel ASP.net

Publicado por Edgar Zambrano (1 intervención) el 06/02/2013 22:21:18
yo lo hago de esta forma

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
public class GenerarExcel
    {
        private StreamWriter w;
 
        ~GenerarExcel() { w = null; }
 
        public void DoExcel(string ruta)
        {
            // creo el archivo
            FileStream fs = new FileStream(ruta,FileMode.Create,FileAccess.ReadWrite);
            // abro en modo escritura el archivo
            w = new StreamWriter(fs);
            // que cabecera se escribira?
                // escribir cabecera de numeros malos
                EscribirCabecera();
                // rellenar lineas
                EscribeLinea();
            // escribir pie de pagina
            EscribePiePagina();
            // libero archivo
            w.Close();
            w.Dispose();
            fs.Close();
            fs.Dispose();
        }
 
        public void EscribirCabecera()
        {
            StringBuilder html = new StringBuilder();
 
            html.Append("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\">");
            html.Append("<html>");
            html.Append("<head>");
            html.Append("<title>Archivo de Marcacion</title>");
            html.Append("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />");
            html.Append("</head>");
            html.Append("<body>");
            html.Append("<table border='1'><tr>");
            html.Append("<td><b>IDENTIFICACION</b></td>");
            html.Append("<td><b>REF. PAGO</b></td>");
            html.Append("<td><b>NOMBRE</b></td>");
            html.Append("<td><b>CIUDAD</b></td>");
            html.Append("<td><b>SALDO</b></td>");
            html.Append("<td><b>TELEFONOS</b></td>");
            html.Append("</tr>");
 
            w.Write(html.ToString());
        }
 
        public void EscribeLinea()
        {
            // instancia datatable
            DataTable dtResult;
            // obtengo datos
            dtResult = new Data.ConsultasGenerales().trae_log_asignaciones(Convert.ToDateTime("2100-01-01"), Convert.ToDateTime("2100-01-01"));
            // recorrer datos
            //for (int i = 0; i < dtResult.Rows.Count; i++)
            foreach (DataRow drow in dtResult.Rows)
            {
                w.Write("<tr>\n");
 
                w.Write(@"<td>{0}</td><td>{1}</td><td>{2}</td><td>{3}</td><td>{4}</td><td>{5}</td>",
                    Convert.ToString(drow[0]), Convert.ToString(drow[1]),
                    Convert.ToString(drow[2]), Convert.ToString(drow[3]),
                    Convert.ToString(drow[4]), Convert.ToString(drow[5]));
 
                w.Write("</tr>");
            }
        }
 
        public void EscribePiePagina()
        {
            StringBuilder html = new StringBuilder();
            html.Append("</table>");
            //html.Append("</p>");
            html.Append("</body>");
            html.Append("</html>");
            w.Write(html.ToString());
        }
 
        public void RetornarArchivos(string filename)
        {
            // envio archivo al cliente
            HttpContext.Current.Response.Clear();
            HttpContext.Current.Response.ContentType = "application/octet-stream";
            HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=" + Path.GetFileName(filename));
            HttpContext.Current.Response.Flush();
            HttpContext.Current.Response.WriteFile(filename);
            HttpContext.Current.Response.End();
        }
 
        public void RetornarArchivos(string ruta_archivos, string ruta_zip)
        {
            // obtengo archivos en el directorio
            string[] filenames = Directory.GetFiles(ruta_archivos);
            // verifico si el archivo zip existe
            if (File.Exists(ruta_zip + "/archivos_planos.zip") == true)
            {
                // elimino archivo zip
                File.Delete(ruta_zip + "/archivos_planos.zip");
            }
            // genero el archivo zip
            using (ZipOutputStream s = new ZipOutputStream(File.Create(ruta_zip+"/archivos_planos.zip")))
            {
                // nivel de comprension
                s.SetLevel(9);
                // buffer
                byte[] buffer = new byte[4096];
                // recorrer archivos en el directorio
                foreach (string file in filenames)
                {
                    // agrego archivo al zip
                    ZipEntry entry = new ZipEntry(Path.GetFileName(file));
                    // obtener fecha actual
                    entry.DateTime = DateTime.Now;
                    s.PutNextEntry(entry);
 
                    using (FileStream fs = File.OpenRead(file))
                    {
                        int sourceBytes;
                        do
                        {
                            sourceBytes = fs.Read(buffer, 0, buffer.Length);
                            s.Write(buffer, 0, sourceBytes);
                        } while (sourceBytes > 0);
                    }
                    // elimino archivo
                    File.Delete(file);
                }
                // termino instancia
                s.Finish();
                // cierro y libero
                s.Close();
                // envio archivo al cliente
                HttpContext.Current.Response.Clear();
                HttpContext.Current.Response.ContentType = "application/octet-stream";
                HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=" + Path.GetFileName(ruta_zip+"/archivos_planos.zip"));
                HttpContext.Current.Response.Flush();
                HttpContext.Current.Response.WriteFile(ruta_zip+"/archivos_planos.zip");
                HttpContext.Current.Response.End();
            }
        }
    }
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar

Exportar a excel ASP.net

Publicado por eduardo (1 intervención) el 21/02/2013 05:33:10
Edgar

Como haces para mostrar un código suponte 000123 y en el excel te aparezca el código 000123 y no 123 como un numero.
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar

Exportar a excel ASP.net

Publicado por asdf (1 intervención) el 18/08/2014 17:05:00
Envia los datos como texto no como numero y después los conviertes :) saludos
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar