Advertisement
Guest User

Untitled

a guest
Jun 29th, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.57 KB | None | 0 0
  1. string strFilePath = GL.Encriptacion.DecryptText(Page.Request.QueryString.Get("ifile"), "data");
  2. string sfilename = GL.Encriptacion.DecryptText(Page.Request.QueryString.Get("nombre"), "data");
  3. string se = Page.Request.QueryString.Get("se");
  4.  
  5. EL.SBC_EJECUCION_PROCESO_DETALLE ej = new EL.SBC_EJECUCION_PROCESO_DETALLE();
  6.  
  7. ej.ARC_ID = Convert.ToInt32(GL.Encriptacion.DecryptText(Page.Request.QueryString.Get("arch"), "data"));
  8. ej.EPROD_AVER_ID = Convert.ToInt32(GL.Encriptacion.DecryptText(Page.Request.QueryString.Get("vers"), "data"));
  9. ej.EPROC_ID = Convert.ToInt32(GL.Encriptacion.DecryptText(Page.Request.QueryString.Get("eproc"), "data"));
  10.  
  11. EL.SBC_ARCHIVO archivo = new EL.SBC_ARCHIVO();
  12. archivo.ARC_ID = ej.ARC_ID;
  13.  
  14. ej.INCLUIR = 1;
  15.  
  16. List<EL.SBC_ARCHIVO> archivos = BL.SBC_ARCHIVO.Buscar(archivo);
  17.  
  18. if (archivos.Count > 0)
  19. {
  20. archivo = archivos[0];
  21. }
  22.  
  23. string extension = archivo.ARC_EXT;
  24.  
  25. if (extension == string.Empty)
  26. {
  27. extension = "txt";
  28. }
  29.  
  30. string iUrl = HttpUtility.UrlDecode(strFilePath + sfilename + "." + extension);
  31. DataTable dt = new DataTable();
  32.  
  33.  
  34. if (se == null || se == "")
  35. {
  36. dt = BL.SBC_EJECUCION_PROCESO_DETALLE.generarArchivoFull(ej);
  37. }
  38. else
  39. {
  40. dt = BL.SBC_EJECUCION_PROCESO_DETALLE.generarArchivo(ej);
  41. }
  42.  
  43. if (extension == "xlsx")
  44. {
  45. XLWorkbook wb = new XLWorkbook();
  46. int l = (sfilename.Length <= 31) ? sfilename.Length : 31;
  47. if (se == "1")
  48. {
  49. //sheet.Row(1).Delete();
  50. var sheet = wb.Worksheets.Add(sfilename.Substring(0, l));
  51. sheet.FirstRow().FirstCell().InsertData(dt.Rows);
  52. wb.SaveAs(iUrl);
  53. //sheet.Range("A1", "A1").Delete(XLShiftDeletedCells.ShiftCellsUp);
  54. }
  55. else
  56. {
  57. var sheet = wb.Worksheets.Add(dt, sfilename.Substring(0, l));
  58. wb.SaveAs(iUrl);
  59. }
  60.  
  61. GL.funcionalidades.abrirArchivo(extension, sfilename, strFilePath);
  62.  
  63. System.IO.File.Delete(iUrl);
  64. }
  65. else
  66. {
  67. if (File.Exists(iUrl))
  68. {
  69. System.IO.File.Delete(iUrl);
  70. }
  71. using (StreamWriter sw = new StreamWriter(strFilePath + sfilename + "." + extension, false, Encoding.UTF8))
  72. {
  73. int iColCount = dt.Columns.Count;
  74.  
  75. //Escribiendo las Columnas del DataTable.
  76. if (se != "1")
  77. {
  78. for (int i = 0; i < iColCount; i++)
  79. {
  80. sw.Write(dt.Columns[i]);
  81. if (i < iColCount - 1)
  82. {
  83. sw.Write(",");
  84. }
  85. }
  86.  
  87. sw.Write(sw.NewLine);
  88. }
  89.  
  90. // Escribiendo todas las Filas del DataTable.
  91. foreach (DataRow dr in dt.Rows)
  92. {
  93. for (int i = 0; i < iColCount; i++)
  94. {
  95. if (!Convert.IsDBNull(dr[i]))
  96. {
  97. sw.Write(dr[i].ToString());
  98. }
  99.  
  100. if (i < iColCount - 1)
  101. {
  102. sw.Write(",");
  103. }
  104. }
  105. sw.Write(sw.NewLine);
  106. }
  107. sw.Close();
  108.  
  109. }
  110.  
  111. System.IO.FileInfo toDownload = new System.IO.FileInfo(iUrl);
  112.  
  113. if (toDownload.Exists)
  114. {
  115. HttpContext.Current.Response.Clear();
  116. HttpContext.Current.Response.AddHeader("Content-Disposition",
  117. "attachment; filename=" + toDownload.Name);
  118. HttpContext.Current.Response.AddHeader("Content-Length",
  119. toDownload.Length.ToString());
  120. HttpContext.Current.Response.ContentType = "application/octet-stream";
  121. HttpContext.Current.Response.WriteFile(iUrl);
  122. HttpContext.Current.Response.End();
  123. }
  124. else
  125. {
  126. ScriptManager.RegisterStartupScript(Page, GetType(), "MyScript", "window.close();", true);
  127. }
  128.  
  129. }
  130. }
  131. catch (Exception ex)
  132. {
  133. ScriptManager.RegisterStartupScript(Page, GetType(), "MyScript", "window.close();", true);
  134. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement