Advertisement
Guest User

Untitled

a guest
Jan 19th, 2017
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.08 KB | None | 0 0
  1. public ActionResult ExportToExcel()
  2. {
  3. DataTable tbl = CopyGenericToDataTable(res);
  4. tbl.TableName = "InvalidInvoices";
  5.  
  6.  
  7. using (XLWorkbook wb = new XLWorkbook())
  8. {
  9. wb.Worksheets.Add(tbl);
  10. wb.Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
  11. wb.Style.Font.Bold = true;
  12.  
  13. Response.Clear();
  14. Response.Buffer = true;
  15. Response.Charset = "";
  16. Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
  17. Response.AddHeader("content-disposition", "attachment;filename= "+fileName + ".xls");
  18.  
  19. using (MemoryStream MyMemoryStream = new MemoryStream())
  20. {
  21. wb.SaveAs(MyMemoryStream);
  22. MyMemoryStream.WriteTo(Response.OutputStream);
  23. Response.Flush();
  24. Response.End();
  25. }
  26. }
  27. }
  28.  
  29. if (files != null)
  30. {
  31. HttpPostedFileBase upload = files.FirstOrDefault();
  32. Stream stream = upload.InputStream;
  33. DataSet result = new DataSet();
  34. if (upload != null && upload.ContentLength > 0)
  35. {
  36. if (upload.FileName.EndsWith(".xls") || upload.FileName.EndsWith(".xlsx"))
  37. {
  38. // ExcelDataReader works with the binary Excel file, so it needs a FileStream
  39. // to get started. This is how we avoid dependencies on ACE or Interop:
  40. // We return the interface, so that
  41.  
  42. IExcelDataReader reader = null;
  43.  
  44. if (upload.FileName.EndsWith(".xls"))
  45. {
  46. reader = ExcelReaderFactory.CreateBinaryReader(stream);
  47. }
  48. else if (upload.FileName.EndsWith(".xlsx"))
  49. {
  50. reader = ExcelReaderFactory.CreateOpenXmlReader(stream);
  51. }
  52.  
  53. reader.IsFirstRowAsColumnNames = false;
  54. result = reader.AsDataSet();
  55. reader.Close();
  56. }
  57. }
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement