Files
rmutr_report/Controllers/KPI.Controller.cs
kamonwan taengsuk 4a2e9731cd add kpi excel
2022-10-26 22:02:29 +07:00

209 lines
13 KiB
C#

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Data;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using ClosedXML.Excel;
using DocumentFormat.OpenXml.Office2010.ExcelAc;
using DocumentFormat.OpenXml.Spreadsheet;
using ExcelDataReader;
using FastReport;
using FastReport.Export.Csv;
using FastReport.Export.Mht;
using FastReport.Export.OoXML;
using FastReport.Export.Pdf;
using Microsoft.AspNetCore.Mvc;
using Microsoft.VisualBasic;
using rmutr_report.Models;
using rmutr_report.Models.Hr;
using Swashbuckle.AspNetCore.Annotations;
namespace rmutr_report.Controllers
{
[SwaggerTag("สำหรับรายงานตัวชี้วัดและเป้าหมาย")]
public class KPI : Controller
{
readonly Setting _setting;
public KPI(Setting setting)
{
_setting = setting;
}
[HttpPost, Route("reports/kpi/{type}")]
[ApiExplorerSettings(GroupName = "reports")]
public IActionResult GetKpiReport([FromRoute] string type, [FromBody] List<kpi> _kpi)
{
DataTable table = new DataTable();
var workbook1 = new XLWorkbook();
var ws = workbook1.Worksheets.Add("kpi");
table.TableName = "kpi";
ws.Cell("A1").Value = "ประเด็นยุทธศาสตร์/เป้าหมายหลัก/ตัวชี้วัด";
ws.Cell("A1").Style.Alignment.WrapText = true;
ws.Range("A1:A2").Column(1).Merge();
ws.Range("B1:C2").Merge().Value = "ตัวชี้วัด";
ws.Range("D2").Value = "หน่วย";
ws.Range("E2").Value = "ค่าเป้าหมาย";
ws.Range("F1:F2").Merge().Value = "ผู้รับผิดชอบกำกับดูแล";
ws.Range("G1:G2").Merge().Value = "ผู้รับผิดชอบในการปฎิบัติงาน";
ws.Range("H1:H2").Merge().Value = "ผู้รวบรวมและรายงานผลตัวชี้วัด";
ws.Range("A1:A2").Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
ws.Range("B1:C2").Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
ws.Range("B1:C2").Style.Alignment.Vertical = XLAlignmentVerticalValues.Center;
ws.Range("D1:E1").Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
ws.Range("D2").Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
ws.Range("E2").Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
ws.Range("F1:F2").Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
ws.Cell("F1").Style.Alignment.Vertical = XLAlignmentVerticalValues.Center;
ws.Range("G1:G2").Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
ws.Cell("G1").Style.Alignment.Vertical = XLAlignmentVerticalValues.Center;
ws.Range("H1:H2").Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
ws.Cell("H1").Style.Alignment.Vertical = XLAlignmentVerticalValues.Center;
//ws.Cell("B1:C2").Style.Alignment.Vertical = XLAlignmentVerticalValues.Center;
ws.Range("A1:A2").Style.Fill.BackgroundColor = XLColor.SkyBlue;
ws.Range("B1:C2").Style.Fill.BackgroundColor = XLColor.SkyBlue;
ws.Range("D1:E1").Style.Fill.BackgroundColor = XLColor.SkyBlue;
ws.Range("D2").Style.Fill.BackgroundColor = XLColor.SkyBlue;
ws.Range("E2").Style.Fill.BackgroundColor = XLColor.SkyBlue;
ws.Range("F1:F2").Style.Fill.BackgroundColor = XLColor.SkyBlue;
ws.Range("G1:G2").Style.Fill.BackgroundColor = XLColor.SkyBlue;
ws.Range("H1:H2").Style.Fill.BackgroundColor = XLColor.SkyBlue;
ws.Range("A1:A2").Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
ws.Range("B1:C2").Style.Border.BottomBorder = XLBorderStyleValues.Thin;
ws.Range("D1:E1").Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
ws.Range("D2").Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
ws.Range("E2").Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
ws.Range("F1:F2").Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
ws.Range("G1:G2").Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
ws.Range("H1:H2").Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
ws.Column(1).Width = 20;
ws.Column(2).Width = 5;
ws.Column(3).Width = 30;
ws.Column(4).Width = 10;
ws.Column(5).Width = 20;
ws.Column(6).Width = 40;
ws.Column(7).Width = 40;
ws.Column(8).Width = 40;
ws.Column(2).Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
ws.Column(3).Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
ws.Column(4).Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
ws.Column(5).Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
ws.Column(6).Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
ws.Column(7).Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
ws.Column(8).Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
ws.Column(1).Style.Font.FontName = "TH Sarabun New";
ws.Column(1).Style.Font.FontSize = 16;
ws.Column(2).Style.Font.FontName = "TH Sarabun New";
ws.Column(2).Style.Font.FontSize = 16;
ws.Column(3).Style.Font.FontName = "TH Sarabun New";
ws.Column(3).Style.Font.FontSize = 16;
ws.Column(4).Style.Font.FontName = "TH Sarabun New";
ws.Column(4).Style.Font.FontSize = 16;
ws.Column(5).Style.Font.FontName = "TH Sarabun New";
ws.Column(5).Style.Font.FontSize = 16;
ws.Column(6).Style.Font.FontName = "TH Sarabun New";
ws.Column(6).Style.Font.FontSize = 16;
ws.Column(7).Style.Font.FontName = "TH Sarabun New";
ws.Column(7).Style.Font.FontSize = 16;
ws.Column(8).Style.Font.FontName = "TH Sarabun New";
ws.Column(8).Style.Font.FontSize = 16;
int col = 1;
int row = 3;
foreach (var kpis in _kpi)
{
if (_kpi != null)
{
ws.Range("D1:E1").Merge().Value = "ค่าเป้าหมาย ปี " + kpis.academic_year.Substring(2, 2);
// var mergedRange = ws.MergedRanges.First(r => r.Contains("A"));
// mergedRange.Style.Fill.BackgroundColor = XLColor.Bisque;
foreach (var header in kpis.header_data)
{
ws.Cell(row, 1).Value = header.header_1;
ws.Cell(row, 1).Style.Fill.BackgroundColor = XLColor.Bisque;
ws.Range(ws.Cell(row, 1),ws.Cell(row, 8)).Merge().Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
row++;
ws.Cell(row, 1).Value = header.header_2;
ws.Cell(row, 1).Style.Fill.BackgroundColor = XLColor.LavenderBlush;
ws.Range(ws.Cell(row, 1),ws.Cell(row, 8)).Merge().Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
row++;
ws.Cell(row, 1).Value = header.header_3;
ws.Cell(row, 1).Style.Fill.BackgroundColor = XLColor.Beige;
ws.Range(ws.Cell(row, 1),ws.Cell(row, 8)).Merge().Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
row++;
}
var data = kpis.header_data.Select(d => d.data).ToList();
foreach (var detail in data)
{
foreach (var data1 in detail)
{
ws.Cell(row, 2).Value = data1.row;
ws.Cell(row, 3).Value = data1.name;
ws.Cell(row, 4).Value = data1.unit;
ws.Cell(row, 5).Value = data1.value;
ws.Cell(row, 6).Value = data1.responsible_person;
ws.Cell(row, 7).Value = data1.responsible_work;
ws.Cell(row, 8).Value = data1.collector;
ws.Cell(row, 1).Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
ws.Cell(row, 1).Style.Border.LeftBorder = XLBorderStyleValues.Thin;
ws.Cell(row, 1).Style.Border.RightBorder = XLBorderStyleValues.Thin;
ws.Cell(row,2).Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
ws.Cell(row,2).Style.Border.LeftBorder = XLBorderStyleValues.Thin;
ws.Cell(row,2).Style.Border.RightBorder = XLBorderStyleValues.Thin;
ws.Cell(row, 3).Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
ws.Cell(row, 3).Style.Border.LeftBorder = XLBorderStyleValues.Thin;
ws.Cell(row, 3).Style.Border.RightBorder = XLBorderStyleValues.Thin;
ws.Cell(row, 4).Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
ws.Cell(row, 4).Style.Border.LeftBorder = XLBorderStyleValues.Thin;
ws.Cell(row, 4).Style.Border.RightBorder = XLBorderStyleValues.Thin;
ws.Cell(row, 5).Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
ws.Cell(row, 5).Style.Border.LeftBorder = XLBorderStyleValues.Thin;
ws.Cell(row, 5).Style.Border.RightBorder = XLBorderStyleValues.Thin;
ws.Cell(row, 6).Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
ws.Cell(row, 6).Style.Border.LeftBorder = XLBorderStyleValues.Thin;
ws.Cell(row, 6).Style.Border.RightBorder = XLBorderStyleValues.Thin;
ws.Cell(row, 7).Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
ws.Cell(row, 7).Style.Border.LeftBorder = XLBorderStyleValues.Thin;
ws.Cell(row, 7).Style.Border.RightBorder = XLBorderStyleValues.Thin;
ws.Cell(row, 8).Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
ws.Cell(row, 8).Style.Border.LeftBorder = XLBorderStyleValues.Thin;
ws.Cell(row, 8).Style.Border.RightBorder = XLBorderStyleValues.Thin;
row++;
// ws.Cell(row, 4).Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
// ws.Cell(row, 5).Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
// ws.Cell(row, 6).Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
// ws.Cell(row, 7).Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
// ws.Cell(row, 8).Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
}
//row++;
}
}
}
using (var stream = new MemoryStream())
{
workbook1.SaveAs(stream);
var content = stream.ToArray();
string date = DateTime.Now.ToString("yyyyMMddHHmmss");
return File(
content,
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
"kpi_" + date + ".xlsx");
}
}
}
}