Abraham Olatubosun

Abraham Olatubosun

  • NA
  • 471
  • 112.7k

HOW TO DOWNLOAD EPPLUS GENERATED FILE TO MY PC

Feb 21 2018 8:19 AM
Dear Code Masters,
       Good afternoon and hope this mail meet you in good health.
Please, i am generating  an excel file with data's from sql server using Epplus. The data's are read from the sql server into the EPPLUS generated excel file into the memory it didn't download so that my client can see the data's in the excel.
below is my code for the downloading.
 
  1. ExcelPackage pck = new ExcelPackage();  
  2.             pck.Workbook.Worksheets.Add("RADET_SHEET");  
  3.             ExcelWorksheet ws = pck.Workbook.Worksheets[1];  
  4.   
  5.             object msValue = System.Reflection.Missing.Value;  
  6.   
  7.             ExcelRange ChartRange = ws.Cells["A3:R3"];  
  8.   
  9.             //======== Create XLS Folder for storage =============  
  10.             string directoryPath = Server.MapPath(string.Format("~/{0}/""XLS"));  
  11.             if (!Directory.Exists(directoryPath))  
  12.             {  
  13.                 Directory.CreateDirectory(directoryPath);  
  14.             }  
  15.   
  16.             //ws.TabColor = ConsoleColor.Blue;  
  17.             ws.Cells[1, 1].Value = "State : " + DrpState.SelectedItem.Text.Trim();  
  18.             ws.Cells[2, 1].Value = "Facility Name :" + DrpFacName.SelectedItem.Text.Trim();  
  19.             ws.Cells[3, 1].Value = "Period From :" + TextBox1.Text.Trim() + "      " +  
  20.                                    "TO :" + TextBox2.Text.Trim();  
  21.   
  22.             using (var Range = ws.Cells[5, 2, 5, 19])  
  23.             {  
  24.                 Range.Style.Font.Bold = true;  
  25.                 Range.Style.Fill.PatternType = OfficeOpenXml.Style.ExcelFillStyle.Solid;  
  26.                 //Range.Style.Fill.BackgroundColor.SetColor(System.Drawing.Color.LightGray);  
  27.                 Range.Style.Font.Color.SetColor(System.Drawing.Color.WhiteSmoke);  
  28.                 Range.Style.ShrinkToFit = false;  
  29.                 Range.Style.WrapText = true;  
  30.                 //Range.AutoFitColumns();  
  31.             }  
  32.   
  33.             for(int ii = 2;ii <= 18; ii++)  
  34.             {  
  35.                 ws.Cells[5,ii].Style.Border.Top.Style = ExcelBorderStyle.Thin;  
  36.                 ws.Cells[5, ii].Style.Border.Bottom.Style = ExcelBorderStyle.Thin;  
  37.                 ws.Cells[5, ii].Style.Border.Left.Style = ExcelBorderStyle.Thin;  
  38.                 ws.Cells[5, ii].Style.Border.Right.Style = ExcelBorderStyle.Thin;  
  39.             }  
  40.   
  41.             ws.Cells[5, 2].Value = "S/N";  
  42.             ws.Cells[5, 3].Value = "Patient ID";  
  43.             ws.Cells[5, 4].Value = "Patient Hospital No";  
  44.             ws.Cells[5, 5].Value = "Sex";  
  45.             ws.Cells[5, 6].Value = "Age at Start of ART (Year)";  
  46.             ws.Cells[5, 7].Value = "Age at Start of ART (Month)";  
  47.             ws.Cells[5, 8].Value = "ART Start Date";  
  48.             ws.Cells[5, 9].Value = "Last Pickup Date";  
  49.             ws.Cells[5, 10].Value = "Month of ARV Refill";  
  50.             ws.Cells[5, 11].Value = "Regimen Line at ART Start";  
  51.             ws.Cells[5, 12].Value = "Regimen at ART Start";  
  52.             ws.Cells[5, 13].Value = "Current Regimen Line";  
  53.             ws.Cells[5, 14].Value = "Current ART Regimen";  
  54.             ws.Cells[5, 15].Value = "Pregnancy Status";  
  55.             ws.Cells[5, 16].Value = "Current Viral Load (c/ml)";  
  56.             ws.Cells[5, 17].Value = "Date of Current Viral Load";  
  57.             ws.Cells[5, 18].Value = "Viral Load Indication";  
  58.             ws.Column(19).AutoFit();  
  59.             ws.Cells[5, 19].Value = "Current ART Status";  
  60.   
  61.             int rownum = 5;  
  62.             int i = 1;  
  63.             int j = 1;  
  64.   
  65.             var DAT = new DataTable();  
  66.             string SQLD = "SELECT * FROM new_RADET";  
  67.             using (SqlConnection con = new SqlConnection(ConnectAll()))  
  68.             {  
  69.                 con.Open();  
  70.                 SqlDataAdapter DA = new SqlDataAdapter(SQLD, con);  
  71.                 DA.Fill(DAT);  
  72.                 foreach(DataRow r1 in DAT.Rows)  
  73.                 {  
  74.                     ws.Cells[rownum + 1, 2].Value = i;  
  75.                     ws.Cells[rownum + 1, 3].Value = r1["Enrol_id"].ToString();  
  76.                     ws.Cells[rownum + 1, 4].Value = r1["Hopital_id"].ToString();  
  77.                     ws.Cells[rownum + 1, 5].Value = r1["Sex"].ToString();  
  78.                     ws.Cells[rownum + 1, 6].Value = r1["Age_Art"].ToString();  
  79.                     ws.Cells[rownum + 1, 7].Value = r1["AgeLW5_Art"].ToString();  
  80.                     ws.Cells[rownum + 1, 8].Value = r1["ArtStart_Date"].ToString();  
  81.                     ws.Cells[rownum + 1, 9].Value = r1["LPickup_Date"].ToString();  
  82.                     ws.Cells[rownum + 1, 10].Value = r1["Refill_Mnth"].ToString();  
  83.                     ws.Cells[rownum + 1, 11].Value = r1["Reg_Line1"].ToString();  
  84.                     ws.Cells[rownum + 1, 12].Value = r1["Reg_ART1"].ToString();  
  85.                     ws.Cells[rownum + 1, 13].Value = r1["Reg_Line2"].ToString();  
  86.                     ws.Cells[rownum + 1, 14].Value = r1["Reg_ART2"].ToString();  
  87.                     ws.Cells[rownum + 1, 15].Value = r1["Preg_Status"].ToString();  
  88.                     ws.Cells[rownum + 1, 16].Value = r1["cur_vl"].ToString();  
  89.                     ws.Cells[rownum + 1, 17].Value = Convert.ToDateTime(r1["vlDate"].ToString().Substring(0,10));  
  90.                     ws.Cells[rownum + 1, 18].Value = r1["VL_Ind"].ToString();  
  91.                     ws.Cells[rownum + 1, 19].Value = r1["Cur_ART_Status"].ToString();  
  92.                     i++;  
  93.                     rownum++;  
  94.                 }  
  95.                // con.Close();  
  96.             }  
  97.             ws.Column(1).AutoFit();  
  98.             ws.Column(2).AutoFit();  
  99.             ws.Column(3).AutoFit();  
  100.             ws.Column(4).AutoFit();  
  101.             ws.Column(5).AutoFit();  
  102.             ws.Column(6).AutoFit();  
  103.             ws.Column(7).AutoFit();  
  104.             ws.Column(8).AutoFit();  
  105.             ws.Column(9).AutoFit();  
  106.             ws.Column(10).AutoFit();  
  107.             ws.Column(11).AutoFit();  
  108.             ws.Column(12).AutoFit();  
  109.             ws.Column(13).AutoFit();  
  110.             ws.Column(14).AutoFit();  
  111.             ws.Column(15).AutoFit();  
  112.             ws.Column(16).AutoFit();  
  113.             ws.Column(17).AutoFit();  
  114.             ws.Column(18).AutoFit();  
  115.             ws.Column(19).AutoFit();  
  116.   
  117.             string excelName = "\\CIHP_RADET";  
  118.             string PathsTo = directoryPath + excelName + ".xlsx";  
  119.             if (PathsTo != null) { File.Delete(PathsTo); }  
  120.             pck.Workbook.Properties.Title = "RADET";  
  121.             this.Response.ClearContent();  
  122.             this.Response.AddHeader("content-disposition"string.Format("attachment;  filename={0}", PathsTo));  
  123.             Response.AddHeader("Content-Type""application/vnd.ms-excel");  
  124.             this.Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";  
  125.             this.Response.BinaryWrite(pck.GetAsByteArray());  
  126.             Response.Flush();  
  127.             Response.End();  
Kindly help me out. Thank you. 

Answers (2)