Ver Mensaje Individual
  #1 (permalink)  
Antiguo 17/02/2014, 11:58
Avatar de ras_chalo
ras_chalo
 
Fecha de Ingreso: junio-2010
Mensajes: 369
Antigüedad: 14 años, 3 meses
Puntos: 6
Pregunta exportar registros a excel con C#

Junto con saludarlos, recurro a ustedes porque estoy creando pequeño proyecto en asp.net - C#, el cual consiste en exportar datos a excel, de momento solo quiero exportar datos a modo de ejemplo a una planilla y ver los resultados, más adelante pretendo obtener los registros desde una base de datos, pero primero necesito realizar esto...

Bueno, este código lo saque de otro sitio web, la cual pueden ver aqui:
http://forums.asp.net/t/1888756.aspx

Estoy corriendolo en Visual Studio 2012 y el error que me arroja al compilar es el siguiente:

Código error:
Ver original
  1. Compilation Error
  2.  
  3. Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.
  4.  
  5. Compiler Error Message: CS0246: The type or namespace name 'DataTable' could not be found (are you missing a using directive or an assembly reference?)
  6.  
  7. Source Error:
  8.  
  9.  
  10. Line 8:      public partial class ExportToExcel : System.Web.UI.Page
  11. Line 9:      {
  12. Line 10:         public void ExportToExcelFile(DataTable dt)
  13. Line 11:         {
  14. Line 12:             Response.Clear();

estuve averiguando por internet y las posibles soluciones apuntan a que no estoy referenciando correctamente, pero ya hice el ejercicio de referenciar todas las opciones y me sigue arrojando el mismo error...

qué estoy haciendo mal o que me falta??


Código C++:
Ver original
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5.  
  6. namespace Test_Web
  7. {
  8.     public partial class ExportToExcel : System.Web.UI.Page
  9.     {
  10.         public void ExportToExcelFile(DataTable dt)
  11.         {
  12.             Response.Clear();
  13.             Response.ClearContent();
  14.             Response.ClearHeaders();
  15.  
  16.             Response.Buffer = true;
  17.             Response.ContentType = "application/vnd.ms-excel";
  18.             Response.Write("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\">");
  19.             Response.AddHeader("content-disposition", "attachment;filename=PolicyClaimDetail.xls");
  20.             Response.ContentEncoding = Encoding.UTF8;
  21.             Response.Charset = "";
  22.             EnableViewState = false;
  23.  
  24.             //Set Fonts
  25.             Response.Write("<font style='font-size:10.0pt; font-family:Calibri;'>");
  26.             Response.Write("<BR><BR><BR>");
  27.  
  28.             //Sets the table border, cell spacing, border color, font of the text, background,
  29.             //foreground, font height
  30.             Response.Write("<Table border='1' bgColor='#ffffff' borderColor='#000000' cellSpacing='0' cellPadding='0' style='font-size:10.0pt; font-family:Calibri; background:white;'> <TR>");
  31.  
  32.             // Check not to increase number of records more than 65k according to excel,03
  33.             if (dt.Rows.Count <= 65536)
  34.             {
  35.                 // Get DataTable Column's Header
  36.                 foreach (DataColumn column in dt.Columns)
  37.                 {
  38.                     //Write in new column
  39.                     Response.Write("<Td>");
  40.  
  41.                     //Get column headers  and make it as bold in excel columns
  42.                     Response.Write("<B>");
  43.                     Response.Write(column);
  44.                    Response.Write("</B>");
  45.                     Response.Write("</Td>");
  46.                 }
  47.  
  48.                 Response.Write("</TR>");
  49.  
  50.                 // Get DataTable Column's Row
  51.                 foreach (DataRow dtRow in dt.Rows)
  52.                 {
  53.                     //Write in new row
  54.                     Response.Write("<TR>");
  55.  
  56.                     for (int i = 0; i <= dt.Columns.Count - 1; i++)
  57.                     {
  58.                         Response.Write("<Td>");
  59.                         Response.Write(dtRow[i].ToString());
  60.                         Response.Write("</Td>");
  61.                     }
  62.  
  63.                     Response.Write("</TR>");
  64.                 }
  65.             }
  66.  
  67.             Response.Write("</Table>");
  68.             Response.Write("</font>");
  69.  
  70.             Response.Flush();
  71.             Response.End();
  72.         }
  73.  
  74. protected void btnExport_Click(object sender, EventArgs e)
  75.         {
  76.             var dataTable = new DataTable();
  77.             dataTable.Columns.Add("ID");
  78.             dataTable.Columns.Add("Name");
  79.             dataTable.Columns.Add("Country");
  80.             dataTable.Columns.Add("City");
  81.  
  82.             dataTable.Rows.Add("1", "Micheal", "USA", "Washington");
  83.             dataTable.Rows.Add("2", "Smith", "UK", "London");
  84.             dataTable.Rows.Add("3", "Martin", "AUS", "Sydney");
  85.  
  86.             if (dataTable.Rows.Count > 0)
  87.             {
  88.                 this.ExportToExcelFile(dataTable);
  89.             }
  90.         }  
  91.     }
  92. }

Desde ya muchas gracias por el tiempo
Estaré atento a cualquier respuesta!,
saludos!!