public JsonResult HistoryData(DateTime begin, DateTime stop) { var data = new SendDataAd(); var temp = _context.SendDataAd.Where(x => x.Time <= stop.AddDays(1) && x.Time >= begin).OrderBy(y => y.Time).ToList(); var result = new ChartData[temp.Count()]; for (int i = 0; i < temp.Count(); i++) { result[i].name = Convert.ToDateTime(temp[i].Time).ToString("yyyy-MM-dd hh:mm:ss"); result[i].value = new string[2]; double voltage; try { var vtemp = Regex.Split(temp[i].Data, "(?<=\\G.{2})"); string[] test = new string[3]; voltage = 0; test[0] = vtemp[12]; test[1] = vtemp[13]; test[2] = vtemp[14]; var testV = test[0] + test[1] + test[2]; voltage = Convert.ToInt32(testV, 16); voltage = Math.Round(voltage / Math.Pow(2, 23) * 5, 7); } catch { voltage = 0; } result[i].value[0] = Convert.ToDateTime(temp[i].Time).ToString("yyyy-MM-dd hh:mm:ss"); result[i].value[1] = voltage.ToString(); } return(Json(result)); }
public IActionResult ExportAd(IFormCollection form) { try { DateTime begin, stop; begin = Convert.ToDateTime(form["begin"]); stop = Convert.ToDateTime(form["stop"]); string sWebRootFolder = _hostingEnvironment.WebRootPath; string sFileName = $"{Guid.NewGuid()}.xlsx"; FileInfo file = new FileInfo(Path.Combine(sWebRootFolder, sFileName)); var data = new SendDataAd(); var temp = _context.SendDataAd.Where(x => x.Time <= stop.AddDays(1) && x.Time >= begin).OrderBy(y => y.Time).ToList(); var result = new ChartData[temp.Count()]; double voltage; string device; string time; using (ExcelPackage package = new ExcelPackage(file)) { // 添加worksheet ExcelWorksheet worksheet = package.Workbook.Worksheets.Add("aspnetcore"); for (int i = 0; i < temp.Count(); i++) { var vtemp = Regex.Split(temp[i].Data, "(?<=\\G.{2})"); string[] test = new string[3]; voltage = 0; test[0] = vtemp[12]; test[1] = vtemp[13]; test[2] = vtemp[14]; var testV = test[0] + test[1] + test[2]; voltage = Convert.ToInt32(testV, 16); voltage = Math.Round(voltage / Math.Pow(2, 23) * 5, 7); device = vtemp[1] + vtemp[2] + vtemp[3]; time = Convert.ToDateTime(temp[i].Time).ToString("yyyy-MM-dd hh:mm:ss"); //添加头 worksheet.Cells[1, 1].Value = "设备号"; worksheet.Cells[1, 2].Value = "电压"; worksheet.Cells[1, 3].Value = "时间"; //添加值 worksheet.Cells["A" + (i + 2)].Value = device; worksheet.Cells["B" + (i + 2)].Value = voltage; worksheet.Cells["C" + (i + 2)].Value = time; } package.Save(); } return(File(sFileName, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")); } catch { return(View("/Shared/Error")); } }
public IActionResult ExportDaAll() { try { string sWebRootFolder = _hostingEnvironment.WebRootPath; string sFileName = $"{Guid.NewGuid()}.xlsx"; FileInfo file = new FileInfo(Path.Combine(sWebRootFolder, sFileName)); var data = new SendDataAd(); var temp = _context.SendDataDa.OrderBy(y => y.Time).ToList(); var result = new ChartData[temp.Count()]; string device; string time; using (ExcelPackage package = new ExcelPackage(file)) { // 添加worksheet ExcelWorksheet worksheet = package.Workbook.Worksheets.Add("aspnetcore"); for (int i = 0; i < temp.Count(); i++) { var vtemp = Regex.Split(temp[i].Data, "(?<=\\G.{2})"); string[] test = new string[3]; test[0] = vtemp[10]; test[1] = vtemp[11]; test[2] = vtemp[12]; device = vtemp[1] + vtemp[2] + vtemp[3]; time = Convert.ToDateTime(temp[i].Time).ToString("yyyy-MM-dd hh:mm:ss"); //添加头 worksheet.Cells[1, 1].Value = "设备号"; worksheet.Cells[1, 2].Value = "DA1"; worksheet.Cells[1, 3].Value = "DA2"; worksheet.Cells[1, 4].Value = "DA3"; worksheet.Cells[1, 5].Value = "时间"; //添加值 worksheet.Cells["A" + (i + 2)].Value = device; worksheet.Cells["B" + (i + 2)].Value = test[0]; worksheet.Cells["C" + (i + 2)].Value = test[1]; worksheet.Cells["D" + (i + 2)].Value = test[2]; worksheet.Cells["E" + (i + 2)].Value = time; } package.Save(); } return(File(sFileName, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")); } catch { return(View("/Shared/Error")); } }