From d8fd45e18560317416bc87b7d417d29a8f97cf99 Mon Sep 17 00:00:00 2001 From: dev02 Date: Fri, 25 Nov 2022 12:32:32 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E6=88=90=E8=B3=87=E6=96=99=E6=AD=B7?= =?UTF-8?q?=E7=A8=8B=E5=8C=AF=E5=87=BA=E7=A8=8B=E5=BA=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Frontend/_historyData.html | 80 ++++--- .../ApiControllers/HistoryController.cs | 211 ++++++++++-------- .../ApiControllers/LoginController.cs | 15 ++ FrontendWebApi/Models/HistoryClass.cs | 6 +- .../wwwroot/excel/history/廠商資料.xlsx | Bin 0 -> 5702 bytes 5 files changed, 190 insertions(+), 122 deletions(-) create mode 100644 FrontendWebApi/wwwroot/excel/history/廠商資料.xlsx diff --git a/Frontend/_historyData.html b/Frontend/_historyData.html index 779c37c..f98eedc 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..a876641 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,129 @@ 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 fileName = "廠商資料.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..c823fa4 100644 --- a/FrontendWebApi/Models/HistoryClass.cs +++ b/FrontendWebApi/Models/HistoryClass.cs @@ -265,8 +265,8 @@ 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; } } } diff --git a/FrontendWebApi/wwwroot/excel/history/廠商資料.xlsx b/FrontendWebApi/wwwroot/excel/history/廠商資料.xlsx new file mode 100644 index 0000000000000000000000000000000000000000..bd5d5178a8f0fa8bbe6e50375c63f3439b6d1cb4 GIT binary patch literal 5702 zcmbtYcRZVW-%hQfs7+&ZptWi=Vw6&|Mr#y7jUa@GRa~-1n6~u5WT*zwht9ukU53M@hvF003wKE4G2A z(mJUutK@G(6ac^p@)_#o=;|#gad<2pRMl^hVy5mdG5;u+$QjODJkE&zoq39)JxdT` zALnRrCL@OW+7!LHT3C(fvuFvogYA&$?Vtm5j|(b~xUP>H8G2a^-?Wu-uy#)7t+jaH zwFuvT^njG`CNtV1DgH_FLh3Z%Raw3}nPO4gU3%)BNQsWOYHu+k7he~bdz@MHf77Cm ztZ z)~zNaHpL$NIX%)dE2nRQfHPwCuF(9py#fZxKB0Avu_>o8$eIVW#U zAUp+70u28mO)Fmis@5?8AejmPU?WRosqW$K!8Xyy7q1A*c;MX%^YqJzF8kuD0JR9~|ha1>^C4oc>z;ald-;a1UKW-vzu<}qb*@^X78vNqZZtS7R(C_rDY*vQ?(g)*CG=_ z+35Yg0ZZLlPjEiE?yk9>7fOemX2WY#(o{ayU4PEoolkcsK~1AG!rs3pCHGlzDNV04 zpC$bl;3=uV8EtKcPlX%$ubAj!3twaxd7qARjSo}Y?kY)DpNrPAFJn!0(%oa6>I^{9 zB{c)@SsCwjeY6ZuVa#YyJzaNlpL1{l96mbwq7V5gX(N*T%dqh7>}t;q*=_W>%7ac+ zBNeml5!rhbDFf;wumT-}H%=%z*e1vx@=WF|yO`8xuSA{D+jbktgiHOGm{KNLe{HJHm zb%dw!rbJObLv6~iTr>hwvJcr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