using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Logging; using Newtonsoft.Json; using NPOI.SS.UserModel; using NPOI.XSSF.UserModel; using SolarPower.Models; using SolarPower.Models.PowerStation; using SolarPower.Repository.Interface; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Threading.Tasks; namespace SolarPower.Controllers { public class StationReportController : MyBaseController { private readonly IPowerStationRepository powerStationRepository; private readonly IStationReportRepository stationReportRepository; public StationReportController(IPowerStationRepository powerStationRepository, IStationReportRepository stationReportRepository) : base() { this.powerStationRepository = powerStationRepository; this.stationReportRepository = stationReportRepository; } public IActionResult Index() { return View(); } [HttpPost] public async Task>>> GetPowerStationNameList(string filter) { ApiResult>> apiResult = new ApiResult>>(); try { var powerStations = new List(); if (IsPlatformLayer(myUser.Role.Layer)) { powerStations = await powerStationRepository.GetPowerStationsAllWithfilter(filter); } else { powerStations = await powerStationRepository.GetPowerStationsByCompanyIdWithfilter(myUser.CompanyId,filter); } var siteDBNamePowerStationId = new Dictionary>(); var powerStation_Group = powerStations.GroupBy(x => x.CityName).ToList(); foreach (var stations in powerStation_Group) { siteDBNamePowerStationId.Add(stations.Key, stations.ToList()); } apiResult.Code = "0000"; apiResult.Data = siteDBNamePowerStationId; } catch (Exception exception) { apiResult.Code = "9999"; Logger.LogError("【" + controllerName + "/" + actionName + "】"); Logger.LogError("【" + controllerName + "/" + actionName + "】" + exception.Message); } apiResult.Msg = errorCode.GetString(apiResult.Code); return apiResult; } public async Task>> GetTableHead(Select_table post) { ApiResult> apiResult = new ApiResult>(); List inverter = new List(); try { var powerStation = await powerStationRepository.GetOneAsync(post.PowerStation); if(powerStation == null) { apiResult.Code = "0001"; apiResult.Msg = "需加入查詢電站"; return apiResult; } inverter = await stationReportRepository.GetInverterId(powerStation.SiteDB,post); for (int i = 0;i> GetForm(Select_table post) { ApiResult apiResult = new ApiResult(); try { var a = await stationReportRepository.Gettablebody(post); apiResult.Code = "0000"; apiResult.Data = a; } catch (Exception exception) { apiResult.Code = "9999"; Logger.LogError("【" + controllerName + "/" + actionName + "】"); Logger.LogError("【" + controllerName + "/" + actionName + "】" + exception.Message); } return apiResult; } public FileResult ExportExcel(string post) { var postObject = JsonConvert.DeserializeObject(post); 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 foreach(var powerstationid in postObject.PowerStation) { var sheet = workbook.CreateSheet(powerstationid.Name); Select_table select_Table = new Select_table { FormType = postObject.FormType, SearchType = postObject.SearchType, Time = postObject.Time, PowerStation = Convert.ToInt32(powerstationid.Value) }; var Formbody = GetForm(select_Table); var Formhead = GetTableHead(select_Table); int RowPosition = 0; IRow row = sheet.CreateRow(RowPosition); ICell cell = row.CreateCell(0); } var ms = new NpoiMemoryStream { AllowClose = false }; workbook.Write(ms); ms.Flush(); ms.Seek(0, SeekOrigin.Begin); return File(ms, "application/vnd.ms-excel", "text.xlsx"); } } }