diff --git a/Frontend/_historyData.html b/Frontend/_historyData.html
index 779c37c..9e6f02c 100644
--- a/Frontend/_historyData.html
+++ b/Frontend/_historyData.html
@@ -42,31 +42,22 @@
-
+
-
+
-
-
-
- 設備名稱 |
- 數值 |
- 紀錄時間 |
-
-
-
-
+
@@ -77,6 +68,7 @@
\ No newline at end of file
diff --git a/FrontendWebApi/ApiControllers/HistoryController.cs b/FrontendWebApi/ApiControllers/HistoryController.cs
index 904eb13..ee5d6a7 100644
--- a/FrontendWebApi/ApiControllers/HistoryController.cs
+++ b/FrontendWebApi/ApiControllers/HistoryController.cs
@@ -16,6 +16,9 @@ using System.Threading.Tasks;
using System.Xml;
using NPOI.XSSF.UserModel;
using NPOI.SS.UserModel;
+using Microsoft.AspNetCore.Mvc.Infrastructure;
+using Microsoft.AspNetCore.StaticFiles;
+using NPOI.HPSF;
namespace FrontendWebApi.ApiControllers
{
@@ -39,105 +42,130 @@ namespace FrontendWebApi.ApiControllers
///
///
///
- public FileResult OpeExportExcel([FromBody] List lhe)
+ public ActionResult> OpeExportExcel([FromBody] List lhe)
{
- var 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;
- #endregion
+ ApiResult apiResult = new ApiResult();
- var sheet = workbook.CreateSheet("歷史資料");
- 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);
- 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.CellStyle = styleLine12;
- #endregion
-
- if (lhe.Count > 0)
+ if (lhe == null)
{
- foreach (var he in lhe)
- {
- RowPosition += 1;
- row = sheet.CreateRow(RowPosition);
- for (var i = 0; i < 3; i++)
- {
- cell = row.CreateCell(i);
- if (i == 0)
- {
- cell.SetCellValue(he.device_name);
- }
- if (i == 1)
- {
- cell.SetCellValue(he.value);
- }
- if (i == 2)
- {
- cell.SetCellValue(he.record_time);
- }
-
- cell.CellStyle = style12;
- }
- }
+ apiResult.Code = "0001";
+ apiResult.Msg = "沒有資料匯入";
+ return apiResult;
}
- var ms = new NpoiMemoryStream
+ try
{
- AllowClose = false
- };
- workbook.Write(ms);
- ms.Flush();
- ms.Seek(0, SeekOrigin.Begin);
+ var fileDateName = lhe.FirstOrDefault().dateType == "month" ? lhe.FirstOrDefault().starttime.ToString("yyyy-MM") : lhe.FirstOrDefault().endtime == null ? lhe.FirstOrDefault().starttime.ToString("yyyy-MM-dd") : lhe.FirstOrDefault().starttime.ToString("yyyy-MM-dd") + "_" + ((DateTime)lhe.FirstOrDefault().endtime).ToString("yyyy-MM-dd");
+ var fileName = "廠商資料_"+fileDateName+".xlsx";
+ var filePath = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "excel", "history");
- return File(ms, "application/vnd.ms-excel", "廠商資料.xlsx");
+ if (!System.IO.Directory.Exists(filePath))
+ System.IO.Directory.CreateDirectory(filePath);
+
+ using (var fs = new FileStream(Path.Combine(filePath, fileName), FileMode.Create, FileAccess.Write))
+ {
+ 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;
+ #endregion
+
+ ISheet sheet = workbook.CreateSheet("歷史資料");
+ 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);
+ 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.CellStyle = styleLine12;
+ #endregion
+
+ if (lhe.Count > 0)
+ {
+ foreach (var he in lhe)
+ {
+ RowPosition += 1;
+ row = sheet.CreateRow(RowPosition);
+ for (var i = 0; i < 3; i++)
+ {
+ cell = row.CreateCell(i);
+ if (i == 0)
+ {
+ cell.SetCellValue(he.deviceName);
+ }
+ if (i == 1)
+ {
+ cell.SetCellValue(he.value);
+ }
+ if (i == 2)
+ {
+ cell.SetCellValue(he.timestamp.ToString("yyyy-MM-dd HH:mm") + ":00");//
+ }
+
+ cell.CellStyle = style12;
+ }
+ }
+ }
+
+ workbook.Write(fs);
+ }
+
+ apiResult.Code = "0000";
+ apiResult.Data = fileName;
+ }
+ catch (Exception exception)
+ {
+ apiResult.Code = "9999";
+ apiResult.Msg = "系統內部錯誤,請聯絡管理者。";
+ Logger.LogError("【" + controllerName + "/" + actionName + "】" + exception.Message);
+ return Ok(apiResult);
+ }
+ return Ok(apiResult);
}
///
diff --git a/FrontendWebApi/ApiControllers/LoginController.cs b/FrontendWebApi/ApiControllers/LoginController.cs
index 24b1435..7891d66 100644
--- a/FrontendWebApi/ApiControllers/LoginController.cs
+++ b/FrontendWebApi/ApiControllers/LoginController.cs
@@ -12,6 +12,7 @@ using Repository.BackendRepository.Interface;
using Repository.FrontendRepository.Interface;
using System;
using System.Collections.Generic;
+using System.IdentityModel.Tokens.Jwt;
using System.IO;
using System.Linq;
using System.Net;
@@ -135,5 +136,19 @@ namespace FrontendWebApi.ApiControllers
return Ok(apiResult);
}
+
+ [HttpGet]
+ [Route("api/df")]
+ public ActionResult DownloadFile(string fileName, string token)
+ {
+ var jwt = new JwtSecurityTokenHandler().ReadJwtToken(token);
+ if (jwt == null)
+ return Unauthorized(HttpStatusCode.Unauthorized);
+ else if (fileName == null)
+ return NotFound("找不到文件");
+
+ var filePath = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "excel", "history");
+ return File(System.IO.File.ReadAllBytes(Path.Combine(filePath, fileName)), "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", fileName);
+ }
}
}
diff --git a/FrontendWebApi/Models/HistoryClass.cs b/FrontendWebApi/Models/HistoryClass.cs
index 8a2a10a..d9a08b4 100644
--- a/FrontendWebApi/Models/HistoryClass.cs
+++ b/FrontendWebApi/Models/HistoryClass.cs
@@ -265,8 +265,11 @@ namespace FrontendWebApi.Models
public class HistoryExport
{
- public string device_name { get; set; }
- public string value { get; set; }
- public string record_time { get; set; }
+ public string deviceName { get; set; }
+ public int value { get; set; }
+ public DateTime timestamp { get; set; }
+ public DateTime starttime { get; set; }
+ public DateTime? endtime { get; set; }
+ public string dateType { get; set; }
}
}