diff --git a/Frontend/_historyData.html b/Frontend/_historyData.html
index ab20694..b62599c 100644
--- a/Frontend/_historyData.html
+++ b/Frontend/_historyData.html
@@ -280,18 +280,32 @@
$('#devPointsList').html(strHtml);
- if (!hisFirst)
- getData();
+ if (!hisFirst) {
+ let start = new Date($('#his_startdate').val());
+ let end = new Date(new Date().setDate(new Date($('#his_enddate input').val()).getDate() + 1));
+
+ if (pageAct.dateType == "today" || pageAct.dateType == "day" || pageAct.dateType == "ytd")
+ end = new Date(new Date(start.valueOf()).setDate(start.getDate() + 1));
+ else if (pageAct.dateType == "month") {
+ start = new Date($('#getmonth').val());
+ end = new Date(new Date(start.valueOf()).setDate(start.getDate() + 30));
+ }
+ else if (pageAct.dateType != "range")
+ return;
+
+ getData(formatDate(start, "date", true), formatDate(end, "date", true));
+ }
}
ytAjax = new YourTeam.Ajax(url, objSendData, success, null, "POST").send();
}
function getData(start = null, end = null) {
+ $(loadEle).Loading("start");
var sdt = new Date();
var edt = new Date(new Date().setDate(sdt.getDate() + 1));
start = start ?? sdt.toLocaleDateString();
end = end ?? edt.toLocaleDateString();
- callBackFromHistory();
+ // callBackFromHistory();
getHistoryDataByBaja(pageAct.deviceNumber + "_" + pageAct.deviceItem,
new Date(start).getTime(),
new Date(end).getTime(),
@@ -329,13 +343,13 @@
let end = new Date(new Date().setDate(new Date($('#his_enddate input').val()).getDate() + 1));
if (pageAct.dateType == "today" || pageAct.dateType == "day" || pageAct.dateType == "ytd")
- end = new Date(new Date().setDate(start.getDate() + 1));
+ end = new Date(new Date(start.valueOf()).setDate(start.getDate() + 1));
else if (pageAct.dateType == "month") {
start = new Date($('#getmonth').val());
- end = new Date(new Date().setDate(start.getDate() + 30));
+ end = new Date(new Date(start.valueOf()).setDate(start.getDate() + 30));
}
else if (pageAct.dateType != "range")
- getData();
+ return;
getData(formatDate(start, "date", true), formatDate(end, "date", true));
}
@@ -399,10 +413,10 @@
let end = new Date(new Date().setDate(new Date($('#his_enddate input').val()).getDate() + 1));
if (pageAct.dateType == "today" || pageAct.dateType == "day" || pageAct.dateType == "ytd")
- end = new Date(new Date().setDate(start.getDate() + 1));
+ end = new Date(new Date(start.valueOf()).setDate(start.getDate() + 1));
else if (pageAct.dateType == "month") {
start = new Date($('#getmonth').val());
- end = new Date(new Date().setDate(start.getDate() + 30));
+ end = new Date(new Date(start.valueOf()).setDate(start.getDate() + 30));
}
else if (pageAct.dateType != "range")
return;
@@ -471,21 +485,48 @@
function exportExcel() {
$(loadEle).Loading("start");
- let url = baseApiUrl + "/History/OpeExportExcel";
+ let token = cookies.get("JWT-Authorization");
+ let url = baseApiUrl + "/api/ExportHistory";
let v = {};
v.starttime = (pageAct.dateType == "month" ? new Date($('#getmonth').val()) : new Date($('#his_startdate').val()));
v.endtime = $('#his_enddate input').val() === "" ? null : new Date($('#his_enddate input').val());
v.dateType = pageAct.dateType;
objSendData.Data = v;
- ytAjax = new YourTeam.Ajax(url, objSendData, function (rel) {
- if (rel.code == "0000")
- location.href = baseApiUrl + "/api/df?path=" + rel.data.split('/')[0] + "&fileName=" + rel.data.split('/')[1] + "&token=" + cookies.get("JWT-Authorization");
+ $.ajax({
+ type: "POST",
+ url: url,
+ data: JSON.stringify(objSendData.Data),
+ aysnc: true,
+ headers: {
+ Authorization: "Bearer " + token,
+ },
+ contentType: "application/json; charset=utf-8",
+ xhrFields: {
+ responseType: "blob", // to avoid binary data being mangled on charset conversion
+ },
+ success: function (rel, text, xhr) {
+ if (rel) {
+ downloadByBlob(xhr, rel);
+ }
+ $(loadEle).Loading("close");
+ },
+ error: function () {
+ $(loadEle).Loading("close");
+ },
+ complete: (xhr) => {
+ setLoading(false);
+ },
+ });
+ // ytAjax = new YourTeam.Ajax(url, objSendData, function (rel) {
+ // $(loadEle).Loading("close");
+ // }, function (rel) {
+ // if (rel) {
+ // downloadByBlob(xhr, rel);
+ // }
- $(loadEle).Loading("close");
- }, () => {
- $(loadEle).Loading("close");
- }, "POST", true).send();
+ // $(loadEle).Loading("close");
+ // }, "POST",true).send();
}
//選擇棟別
diff --git a/FrontendWebApi/ApiControllers/HistoryController.cs b/FrontendWebApi/ApiControllers/HistoryController.cs
index 792507e..22b6c40 100644
--- a/FrontendWebApi/ApiControllers/HistoryController.cs
+++ b/FrontendWebApi/ApiControllers/HistoryController.cs
@@ -1,9 +1,11 @@
using FrontendWebApi.Models;
using Microsoft.AspNetCore.Mvc;
+using Microsoft.AspNetCore.Routing;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
+using NPOI.HPSF;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using Repository.BackendRepository.Interface;
@@ -45,35 +47,40 @@ namespace FrontendWebApi.ApiControllers
///
///
[HttpPost]
- public async Task>> OpeExportExcel([FromBody] HistoryExport lhe)
+ [Route("api/ExportHistory")]
+ public FileResult OpeExportExcel([FromBody] HistoryExport lhe)
{
ApiResult apiResult = new ApiResult();
- if (lhe == null)
- {
- apiResult.Code = "0001";
- apiResult.Msg = "沒有資料匯入";
- return apiResult;
- }
-
+ //if (lhe == null)
+ //{
+ // apiResult.Code = "0001";
+ // apiResult.Msg = "沒有資料匯入";
+ // return apiResult;
+ //}
+ var fileDateName = lhe.dateType == "today" ? lhe.starttime.ToString("yyyy-MM-dd")
+ : lhe.dateType == "month" ? lhe.starttime.ToString("yyyy-MM")
+ : lhe.starttime.ToString("yyyy-MM-dd") + "_" + ((DateTime)lhe.endtime).ToString("yyyy-MM-dd");
+ var fileName = "歷史資料_" + fileDateName + ".xlsx";
+ XSSFWorkbook workbook = new XSSFWorkbook();
try
{
#region 取得obix 設定
EDFunction ed = new EDFunction();
var obixApiConfig = new ObixApiConfig();
var sqlObix = $@"SELECT system_value as Value, system_key as Name FROM variable WHERE deleted = 0 AND system_type = 'obixConfig'";
- var variableObix = await backendRepository.GetAllAsync(sqlObix);
+ var variableObix = backendRepository.GetAllAsync(sqlObix).Result;
obixApiConfig.ApiBase = variableObix.Where(x => x.Name == "ApiBase").Select(x => x.Value).FirstOrDefault();
obixApiConfig.UserName = ed.AESDecrypt(variableObix.Where(x => x.Name == "UserName").Select(x => x.Value).FirstOrDefault());
obixApiConfig.Password = ed.AESDecrypt(variableObix.Where(x => x.Name == "Password").Select(x => x.Value).FirstOrDefault());
String encoded = System.Convert.ToBase64String(System.Text.Encoding.GetEncoding("ISO-8859-1").GetBytes(obixApiConfig.UserName + ":" + obixApiConfig.Password));
#endregion 取得obix 設定
- var building = await backendRepository.GetAllAsync("select * from building where deleted = 0");
- var buildStation = await backendRepository.GetAllAsync("select SUBSTRING_INDEX(system_value, '/', 1) system_value, system_key from variable where system_type = 'dashboard_total_elec' and deleted = 0 and (SUBSTRING_INDEX(system_value, '/', 1) != '' and SUBSTRING_INDEX(system_value, '/', 1) is not null)");
+ var building = backendRepository.GetAllAsync("select * from building where deleted = 0").Result;
+ var buildStation = backendRepository.GetAllAsync("select SUBSTRING_INDEX(system_value, '/', 1) system_value, system_key from variable where system_type = 'dashboard_total_elec' and deleted = 0 and (SUBSTRING_INDEX(system_value, '/', 1) != '' and SUBSTRING_INDEX(system_value, '/', 1) is not null)").Result;
#region get device and device_item(point)
- var device = await backendRepository.GetAllAsync($"select * from device where deleted = 0 and is_link = 1 and device_building_tag in ('{string.Join("','", building.Select(x => x.building_tag))}')");
- var devicePoint = await backendRepository.GetAllAsync($"select * from device_item where deleted = 0 and is_link = 1 and is_show_history = 1 and device_building_tag in ('{string.Join("','", building.Select(x => x.building_tag))}')");
+ var device = backendRepository.GetAllAsync($"select * from device where deleted = 0 and is_link = 1 and device_building_tag in ('{string.Join("','", building.Select(x => x.building_tag))}')").Result;
+ var devicePoint = backendRepository.GetAllAsync($"select * from device_item where deleted = 0 and is_link = 1 and is_show_history = 1 and device_building_tag in ('{string.Join("','", building.Select(x => x.building_tag))}')").Result;
#endregion
List listDevicePoint = new List();
@@ -166,130 +173,140 @@ namespace FrontendWebApi.ApiControllers
#endregion
#region export file progress
- var fileDateName = lhe.dateType == "today" ? lhe.starttime.ToString("yyyy-MM-dd")
- : lhe.dateType == "month" ? lhe.starttime.ToString("yyyy-MM")
- : lhe.starttime.ToString("yyyy-MM-dd") + "_" + ((DateTime)lhe.endtime).ToString("yyyy-MM-dd");
- var fileName = "歷史資料_" + fileDateName + ".xlsx";
- var filePath = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "excel", "history");
+ //var filePath = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "excel", "history");
- if (!System.IO.Directory.Exists(filePath))
- System.IO.Directory.CreateDirectory(filePath);
+ //if (!System.IO.Directory.Exists(filePath))
+ // System.IO.Directory.CreateDirectory(filePath);
- using (var fs = new FileStream(Path.Combine(filePath, fileName), FileMode.Create, FileAccess.Write))
+
+ #region excel設定
+ IFont font12 = workbook.CreateFont();
+ font12.FontName = "新細明體";
+ font12.FontHeightInPoints = 12;
+ ICellStyle style12 = workbook.CreateCellStyle();
+ style12.SetFont(font12);
+ style12.Alignment = HorizontalAlignment.Center;
+ style12.VerticalAlignment = VerticalAlignment.Center;
+ IFont font12Times = workbook.CreateFont();
+ font12Times.FontName = "Times New Roman";
+ font12Times.FontHeightInPoints = 12;
+ IFont font18 = workbook.CreateFont();
+ font18.FontName = "新細明體";
+ font18.FontHeightInPoints = 18;
+ font18.IsBold = true;
+ ICellStyle styleTitle18 = workbook.CreateCellStyle();
+ styleTitle18.SetFont(font18);
+ styleTitle18.Alignment = HorizontalAlignment.Center;
+ styleTitle18.VerticalAlignment = VerticalAlignment.Center;
+ ICellStyle styleLeft12 = workbook.CreateCellStyle();
+ styleLeft12.SetFont(font12);
+ styleLeft12.Alignment = HorizontalAlignment.Left;
+ styleLeft12.VerticalAlignment = VerticalAlignment.Center;
+ ICellStyle styleLine12 = workbook.CreateCellStyle();
+ styleLine12.SetFont(font12);
+ styleLine12.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Center;
+ styleLine12.VerticalAlignment = VerticalAlignment.Center;
+ styleLine12.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
+ styleLine12.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
+ styleLine12.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
+ styleLine12.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
+ ICellStyle stylein12 = workbook.CreateCellStyle();
+ stylein12.SetFont(font12Times);
+ stylein12.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Left;
+ stylein12.VerticalAlignment = VerticalAlignment.Center;
+ stylein12.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
+ stylein12.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
+ stylein12.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
+ stylein12.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
+ stylein12.WrapText = true;
+ #endregion
+
+ foreach (var b in building)
{
- IWorkbook workbook = new XSSFWorkbook();
- #region excel設定
- IFont font12 = workbook.CreateFont();
- font12.FontName = "新細明體";
- font12.FontHeightInPoints = 12;
- ICellStyle style12 = workbook.CreateCellStyle();
- style12.SetFont(font12);
- style12.Alignment = HorizontalAlignment.Center;
- style12.VerticalAlignment = VerticalAlignment.Center;
- IFont font12Times = workbook.CreateFont();
- font12Times.FontName = "Times New Roman";
- font12Times.FontHeightInPoints = 12;
- IFont font18 = workbook.CreateFont();
- font18.FontName = "新細明體";
- font18.FontHeightInPoints = 18;
- font18.IsBold = true;
- ICellStyle styleTitle18 = workbook.CreateCellStyle();
- styleTitle18.SetFont(font18);
- styleTitle18.Alignment = HorizontalAlignment.Center;
- styleTitle18.VerticalAlignment = VerticalAlignment.Center;
- ICellStyle styleLeft12 = workbook.CreateCellStyle();
- styleLeft12.SetFont(font12);
- styleLeft12.Alignment = HorizontalAlignment.Left;
- styleLeft12.VerticalAlignment = VerticalAlignment.Center;
- ICellStyle styleLine12 = workbook.CreateCellStyle();
- styleLine12.SetFont(font12);
- styleLine12.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Center;
- styleLine12.VerticalAlignment = VerticalAlignment.Center;
- styleLine12.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
- styleLine12.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
- styleLine12.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
- styleLine12.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
- ICellStyle stylein12 = workbook.CreateCellStyle();
- stylein12.SetFont(font12Times);
- stylein12.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Left;
- stylein12.VerticalAlignment = VerticalAlignment.Center;
- stylein12.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
- stylein12.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
- stylein12.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
- stylein12.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
- stylein12.WrapText = true;
+ var data = he.Where(x => x.building_tag == b.building_tag).ToList();
+ ISheet sheet = workbook.CreateSheet($"{b.full_name}歷史資料");
+ int RowPosition = 0;
+ #region set cell
+ IRow row = sheet.CreateRow(RowPosition);
+ sheet.SetColumnWidth(0, 4 * 160 * 12);
+ sheet.SetColumnWidth(1, 4 * 160 * 12);
+ sheet.SetColumnWidth(2, 4 * 160 * 12);
+ sheet.SetColumnWidth(3, 4 * 160 * 12);
+ ICell cell = row.CreateCell(0);
+ cell.SetCellValue("類型");
+ cell.CellStyle = styleLine12;
+ cell = row.CreateCell(1);
+ cell.SetCellValue("設備名稱");
+ cell.CellStyle = styleLine12;
+ cell = row.CreateCell(2);
+ cell.SetCellValue("數值");
+ cell = row.CreateCell(3);
+ cell.SetCellValue("記錄時間");
+ cell.CellStyle = styleLine12;
#endregion
- foreach (var b in building)
+ if (data.Count > 0)
{
- var data = he.Where(x => x.building_tag == b.building_tag).ToList();
- ISheet sheet = workbook.CreateSheet($"{b.full_name}歷史資料");
- int RowPosition = 0;
- #region set cell
- IRow row = sheet.CreateRow(RowPosition);
- sheet.SetColumnWidth(0, 4 * 160 * 12);
- sheet.SetColumnWidth(1, 4 * 160 * 12);
- sheet.SetColumnWidth(2, 4 * 160 * 12);
- sheet.SetColumnWidth(3, 4 * 160 * 12);
- ICell cell = row.CreateCell(0);
- cell.SetCellValue("類型");
- cell.CellStyle = styleLine12;
- cell = row.CreateCell(1);
- cell.SetCellValue("設備名稱");
- cell.CellStyle = styleLine12;
- cell = row.CreateCell(2);
- cell.SetCellValue("數值");
- cell = row.CreateCell(3);
- cell.SetCellValue("記錄時間");
- cell.CellStyle = styleLine12;
- #endregion
-
- if (data.Count > 0)
+ foreach (var d in data)
{
- foreach (var d in data)
+ RowPosition += 1;
+ row = sheet.CreateRow(RowPosition);
+ for (var i = 0; i < 4; i++)
{
- RowPosition += 1;
- row = sheet.CreateRow(RowPosition);
- for (var i = 0; i < 4; i++)
+ cell = row.CreateCell(i);
+ if (i == 0)
{
- cell = row.CreateCell(i);
- if (i == 0)
- {
- cell.SetCellValue(d.type);
- }
- if (i == 1)
- {
- cell.SetCellValue(d.deviceName);
- }
- if (i == 2)
- {
- cell.SetCellValue(d.value);
- }
- if (i == 3)
- {
- cell.SetCellValue(d.timestamp.ToString("yyyy-MM-dd HH:mm") + ":00");//
- }
-
- cell.CellStyle = style12;
+ cell.SetCellValue(d.type);
}
+ if (i == 1)
+ {
+ cell.SetCellValue(d.deviceName);
+ }
+ if (i == 2)
+ {
+ cell.SetCellValue(d.value);
+ }
+ if (i == 3)
+ {
+ cell.SetCellValue(d.timestamp.ToString("yyyy-MM-dd HH:mm") + ":00");//
+ }
+
+ cell.CellStyle = style12;
}
}
}
- workbook.Write(fs);
}
+
+
+
+
+ //using (var fs = new FileStream(Path.Combine(filePath, fileName), FileMode.Create, FileAccess.Write))
+ //{
+ //workbook.Write(fs);
+
+ //}
#endregion
- apiResult.Code = "0000";
- apiResult.Data = "history/" + fileName;
+ //apiResult.Code = "0000";
+ //apiResult.Data = "history/" + fileName;
}
catch (Exception exception)
{
apiResult.Code = "9999";
apiResult.Msg = "系統內部錯誤,請聯絡管理者。 Msg: " + exception.Message;
Logger.LogError("【" + controllerName + "/" + actionName + "】" + exception.Message);
- return Ok(apiResult);
+ throw exception;
}
- return Ok(apiResult);
+
+ var ms = new NpoiMemoryStream
+ {
+ AllowClose = false
+ };
+ workbook.Write(ms);
+ ms.Flush();
+ ms.Seek(0, SeekOrigin.Begin);
+ Response.Headers.Add("Access-Control-Expose-Headers", "Content-Disposition");
+ return File(ms, "application/vnd.ms-excel", fileName);
}
///
diff --git a/FrontendWebApi/ApiControllers/HydroMeterController.cs b/FrontendWebApi/ApiControllers/HydroMeterController.cs
index c9ffeef..93f4e02 100644
--- a/FrontendWebApi/ApiControllers/HydroMeterController.cs
+++ b/FrontendWebApi/ApiControllers/HydroMeterController.cs
@@ -462,11 +462,11 @@ namespace FrontendWebApi.ApiControllers
#endregion
foreach (var r in result)
{
- string buildingName = r.Select(x => x.building_name).FirstOrDefault();
- var sheet = workbook.CreateSheet($"{buildingName}電表報表");
- int RowPosition = 0;
- if (result.Count > 0)
+ if (r.Count > 0)
{
+ string buildingName = r.Select(x => x.building_name).FirstOrDefault();
+ var sheet = workbook.CreateSheet($"{buildingName}電表報表");
+ int RowPosition = 0;
#region set cell
IRow row = sheet.CreateRow(RowPosition);
sheet.SetColumnWidth(0, 4 * 160 * 12);