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();
}
}
}