|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using TestSample.Models; |
| 5 | +using System.Threading.Tasks; |
| 6 | +using Microsoft.AspNetCore.Mvc; |
| 7 | +using Syncfusion.XlsIO; |
| 8 | +using Syncfusion.EJ2.Base; |
| 9 | +using System.Data; |
| 10 | +using System.IO; |
| 11 | +using Microsoft.AspNetCore.Http; |
| 12 | +using Newtonsoft.Json; |
| 13 | + |
| 14 | +namespace TestSample.Controllers |
| 15 | +{ |
| 16 | + |
| 17 | + public class HomeController : Controller |
| 18 | + { |
| 19 | + |
| 20 | + public IActionResult Index() |
| 21 | + { |
| 22 | + ViewBag.datasource = OrdersDetails.GetAllRecords().ToList(); |
| 23 | + return View(); |
| 24 | + } |
| 25 | + public async Task<IActionResult> Save() |
| 26 | + { |
| 27 | + string filePath = "App_Data/TempData/"; |
| 28 | + string directoryPath = Path.Combine(new FileInfo(filePath).Directory.FullName); |
| 29 | + |
| 30 | + if (!Directory.Exists(directoryPath)) |
| 31 | + Directory.CreateDirectory(directoryPath); |
| 32 | + |
| 33 | + try |
| 34 | + { |
| 35 | + if (HttpContext.Request.Form.Files.Count > 0) |
| 36 | + { |
| 37 | + for (int i = 0; i < HttpContext.Request.Form.Files.Count; ++i) |
| 38 | + { |
| 39 | + IFormFile httpPostedFile = HttpContext.Request.Form.Files[i]; |
| 40 | + |
| 41 | + if (httpPostedFile != null) |
| 42 | + { |
| 43 | + filePath = Path.Combine(directoryPath, httpPostedFile.FileName); |
| 44 | + |
| 45 | + if (!System.IO.File.Exists(filePath)) |
| 46 | + { |
| 47 | + using (var fileStream = new FileStream(filePath, FileMode.Create)) |
| 48 | + { |
| 49 | + await httpPostedFile.CopyToAsync(fileStream); |
| 50 | + ExcelEngine excelEngine = new ExcelEngine(); |
| 51 | + |
| 52 | + //Loads or open an existing workbook through Open method of IWorkbooks |
| 53 | + fileStream.Position = 0; |
| 54 | + IWorkbook workbook = excelEngine.Excel.Workbooks.Open(httpPostedFile.OpenReadStream()); |
| 55 | + IWorksheet worksheet = workbook.Worksheets[0]; |
| 56 | + |
| 57 | + // Read data from the worksheet and Export to the DataTable. |
| 58 | + |
| 59 | + DataTable table = worksheet.ExportDataTable(worksheet.UsedRange.Row, worksheet.UsedRange.Column, worksheet.UsedRange.LastRow, worksheet.UsedRange.LastColumn, ExcelExportDataTableOptions.ColumnNames | ExcelExportDataTableOptions.ComputedFormulaValues); |
| 60 | + string JSONString = string.Empty; |
| 61 | + JSONString = JsonConvert.SerializeObject(table); |
| 62 | + ViewBag.data = JsonConvert.SerializeObject(table, Formatting.Indented, new JsonSerializerSettings { Converters = new[] { new Newtonsoft.Json.Converters.DataTableConverter() } }); |
| 63 | + |
| 64 | + //return View(); |
| 65 | + } |
| 66 | + |
| 67 | + return Ok(ViewBag.data); |
| 68 | + } |
| 69 | + else |
| 70 | + { |
| 71 | + return BadRequest("File already exists"); |
| 72 | + } |
| 73 | + } |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + return BadRequest("No file in request"); ; |
| 78 | + } |
| 79 | + catch (Exception e) |
| 80 | + { |
| 81 | + return BadRequest(e.Message); |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + public IActionResult gridState([FromBody]DataManagerRequest dm) |
| 86 | + { |
| 87 | + var query = dm; |
| 88 | + return Json(query); |
| 89 | + } |
| 90 | + |
| 91 | + public IActionResult GridDatasource([FromBody]DataManagerRequest dm) |
| 92 | + { |
| 93 | + var DataSource = OrdersDetails.GetAllRecords().ToList(); |
| 94 | + |
| 95 | + DataOperations operation = new DataOperations(); |
| 96 | + int count = DataSource.Count(); |
| 97 | + return dm.RequiresCounts ? Json(new { result = DataSource.Skip(dm.Skip).Take(dm.Take), count = count }) : Json(DataSource); |
| 98 | + } |
| 99 | + public ActionResult Update([FromBody]CRUDModel<OrdersDetails> value) |
| 100 | + { |
| 101 | + var ord = value.Value; |
| 102 | + OrdersDetails val = OrdersDetails.GetAllRecords().Where(or => or.OrderID == ord.OrderID).FirstOrDefault(); |
| 103 | + val.OrderID = ord.OrderID; |
| 104 | + val.ShipName = ord.ShipName; |
| 105 | + val.CustomerID = ord.CustomerID; |
| 106 | + val.ShipCountry = ord.ShipCountry; |
| 107 | + |
| 108 | + return Json(value.Value); |
| 109 | + } |
| 110 | + //insert the record |
| 111 | + public ActionResult Insert([FromBody]CRUDModel<OrdersDetails> value) |
| 112 | + { |
| 113 | + |
| 114 | + OrdersDetails.GetAllRecords().Insert(0, value.Value); |
| 115 | + return Json(value.Value); |
| 116 | + } |
| 117 | + //Delete the record |
| 118 | + public ActionResult Delete([FromBody]CRUDModel<OrdersDetails> value) |
| 119 | + { |
| 120 | + OrdersDetails.GetAllRecords().Remove(OrdersDetails.GetAllRecords().Where(or => or.OrderID == int.Parse(value.Key.ToString())).FirstOrDefault()); |
| 121 | + return Json(value); |
| 122 | + } |
| 123 | + |
| 124 | + public class Data |
| 125 | + { |
| 126 | + |
| 127 | + public bool requiresCounts { get; set; } |
| 128 | + public int skip { get; set; } |
| 129 | + public int take { get; set; } |
| 130 | + } |
| 131 | + public class CRUDModel<T> where T : class |
| 132 | + { |
| 133 | + public string Action { get; set; } |
| 134 | + |
| 135 | + public string Table { get; set; } |
| 136 | + |
| 137 | + public string KeyColumn { get; set; } |
| 138 | + |
| 139 | + public object Key { get; set; } |
| 140 | + |
| 141 | + public T Value { get; set; } |
| 142 | + |
| 143 | + public List<T> Added { get; set; } |
| 144 | + |
| 145 | + public List<T> Changed { get; set; } |
| 146 | + |
| 147 | + public List<T> Deleted { get; set; } |
| 148 | + |
| 149 | + public IDictionary<string, object> @params { get; set; } |
| 150 | + } |
| 151 | + } |
| 152 | +} |
0 commit comments