Advertisement
Guest User

Untitled

a guest
Apr 19th, 2014
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.21 KB | None | 0 0
  1. [HttpPost]
  2. public ActionResult CompareExcel(ExcelModel model, string submitValue, string compareOption)
  3. {
  4.  
  5. TempData["firstFile"] = model.Files[0];
  6. TempData["secondFile"] = model.Files[1];
  7.  
  8. if (submitValue == "Compare calculations")
  9. {
  10. var firstFile = TempData["firstFile"];
  11. var secondFile = TempData["secondFile"];
  12. if (firstFile == null || secondFile == null)
  13. return View("CompareExcel");
  14. else
  15. return CompareColumn(compareOption);
  16. }
  17. //Assume submitValue is "Compare calculations"
  18. }
  19.  
  20. [HttpPost]
  21. public ActionResult CompareColumn(string compareOption)
  22. {
  23. List<Calculation> cList = new List<Calculation>();
  24. Calculation calc = new Calculation();
  25. BigModel m = new BigModel();
  26. HttpPostedFileBase firstFile = (HttpPostedFileBase)TempData["firstFile"];
  27. HttpPostedFileBase secondFile = (HttpPostedFileBase)TempData["secondFile"];
  28. DataSet firstSet = ExcelToDataSet.ConvertToDataSet(firstFile);
  29. DataSet secondSet = ExcelToDataSet.ConvertToDataSet(secondFile);
  30. for (var i = 0; i < firstSet.Tables[0].Rows.Count; i++) //Get Object reference not set to an instance of an object
  31. {
  32. DataRow data = firstSet.Tables[0].Rows[i];
  33. calc.PresentValue = Convert.ToDecimal(data.Table.Rows[i]["Capital balance"]);
  34. }
  35.  
  36. return View();
  37. }
  38.  
  39. public partial class GetExcel
  40. {
  41.  
  42. public List<HttpPostedFileBase> Files { get; set; }
  43.  
  44. public GetExcel()
  45. {
  46. Files = new List<HttpPostedFileBase>();
  47. }
  48. }
  49.  
  50. public static DataSet ConvertToDataSet(HttpPostedFileBase excelFile)
  51. {
  52. DataSet result = null;
  53.  
  54. if (excelFile != null && excelFile.ContentLength > 0)
  55. {
  56. // .xlsx
  57. IExcelDataReader reader = ExcelReaderFactory.CreateOpenXmlReader(excelFile.InputStream);
  58.  
  59. // .xls
  60. //IExcelDataReader reader = ExcelReaderFactory.CreateBinaryReader(file.InputStream);
  61.  
  62. reader.IsFirstRowAsColumnNames = true; // if your first row contains column names
  63. result = reader.AsDataSet();
  64. reader.Close();
  65. }
  66. return result;
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement