Advertisement
Guest User

Untitled

a guest
Jun 26th, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.76 KB | None | 0 0
  1. public void ExporttoExcel(DataTable table, string filename)
  2. {
  3. HttpContext.Current.Response.Clear();
  4. HttpContext.Current.Response.ClearContent();
  5. HttpContext.Current.Response.ClearHeaders();
  6. HttpContext.Current.Response.Buffer = true;
  7. HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;
  8. HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);
  9. HttpContext.Current.Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
  10. HttpContext.Current.Response.AddHeader("content-disposition", "attachment;filename=Resultados_Finales.xlsx");
  11.  
  12. using (ExcelPackage pack = new ExcelPackage())
  13. {
  14. ExcelWorksheet ws = pack.Workbook.Worksheets.Add(filename);
  15. ws.Cells["A1"].LoadFromDataTable(table, true);
  16. ws.Cells[ws.Dimension.Address].AutoFitColumns();
  17. ws.Column(74).Style.Locked = false;
  18. ws.Column(75).Style.Locked = false;
  19. ws.Column(88).Style.Locked = false;
  20. ws.Column(89).Style.Locked = false;
  21. ws.Column(90).Style.Locked = false;
  22. ws.Column(91).Style.Locked = false;
  23. ws.Column(92).Style.Locked = false;
  24. ws.Column(93).Style.Locked = false;
  25. ws.Column(94).Style.Locked = false;
  26. ws.Cells["BV1:BW1"].Style.Locked = true;
  27. ws.Cells["CJ1:CP1"].Style.Locked = true;
  28. ws.Protection.IsProtected = true;
  29. var ms = new System.IO.MemoryStream();
  30. pack.SaveAs(ms);
  31. ms.WriteTo(HttpContext.Current.Response.OutputStream);
  32. }
  33.  
  34. HttpContext.Current.Response.Flush();
  35. HttpContext.Current.Response.End();
  36.  
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement