using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using ClosedXML.Excel;
using DocumentFormat.OpenXml.Drawing.Charts;
using FastReport;
using FastReport.Export.Csv;
using FastReport.Export.Mht;
using FastReport.Export.OoXML;
using FastReport.Export.Pdf;
using Microsoft.AspNetCore.Mvc;
using rmutr_report.Models;
using rmutr_report.Models.Hr;
using Swashbuckle.AspNetCore.Annotations;

namespace rmutr_report.Controllers
{
    [SwaggerTag("สำหรับรายงานสรุปแผนความต้องการ")]
    public class SummaryController : Controller
    {
        readonly Setting _setting;

        public SummaryController(Setting setting)
        {
            this._setting = setting;
        }

        [HttpPost, Route("reports/summary_of_equipment/{type}")]
        [ApiExplorerSettings(GroupName = "reports")]
        public IActionResult GetSum1Report([FromRoute] string type,
            [FromBody] summary_of_equipment summary_of_equipments)
        {
            var _summary_of_equipment = new List<summary_of_equipment>() {summary_of_equipments};
            var workbook = new XLWorkbook();
            var ws = workbook.Worksheets.Add("รายการก่อสร้างอาคาร");
            var imagePath = _setting.report_path + @"Logo-RMUTR.png";
            ws.AddPicture(imagePath).MoveTo(ws.Cell("A1")).Scale(0.3);
            ws.Range("A4:A6").Merge().Value = "ลำดับความสำคัญ(1)";
            ws.Cell("A4").Style.Alignment.WrapText = true;
            ws.Range("A4:A6").Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
            ws.Range("A4:A6").Style.Alignment.Vertical = XLAlignmentVerticalValues.Center;
            ws.Range("A4:A6").Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
            ws.Cell("A4").Style.Font.FontName = "TH Sarabun New";
            ws.Cell("A4").Style.Font.FontSize = 14;
            ws.Range("B4:B6").Merge().Value = "รายการ";
            ws.Cell("B4").Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
            ws.Cell("B4").Style.Alignment.Vertical = XLAlignmentVerticalValues.Center;
            ws.Cell("B4").Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
            ws.Cell("B4").Style.Font.FontName = "TH Sarabun New";
            ws.Cell("B4").Style.Font.FontSize = 14;
            ws.Range("C5:E5").Merge().Value = "ประเภทของครุภัณฑ์";
            ws.Range("C5:E5").Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
            ws.Range("C5:E5").Style.Alignment.Vertical = XLAlignmentVerticalValues.Center;
            ws.Range("C5:E5").Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
            ws.Cell("C5").Style.Font.FontName = "TH Sarabun New";
            ws.Cell("C5").Style.Font.FontSize = 11;
            ws.Range("C6").Value = "ทดแทนของเดิม";
            ws.Cell("C6").Style.Alignment.WrapText = true;
            ws.Cell("C6").Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
            ws.Cell("C6").Style.Alignment.Vertical = XLAlignmentVerticalValues.Center;
            ws.Cell("C6").Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
            ws.Cell("C6").Style.Font.FontName = "TH Sarabun New";
            ws.Cell("C6").Style.Font.FontSize = 11;
            ws.Range("D6").Value = "เพิ่มประสิทธิภาพ";
            ws.Cell("D6").Style.Font.FontName = "TH Sarabun New";
            ws.Cell("D6").Style.Font.FontSize = 11;
            ws.Cell("D6").Style.Alignment.WrapText = true;
            ws.Cell("D6").Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
            ws.Cell("D6").Style.Alignment.Vertical = XLAlignmentVerticalValues.Center;
            ws.Cell("D6").Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
            ws.Range("E6").Value = "ครุภัณฑ์ใหม่";
            ws.Cell("E6").Style.Font.FontName = "TH Sarabun New";
            ws.Cell("E6").Style.Font.FontSize = 11;
            ws.Cell("E6").Style.Alignment.WrapText = true;
            ws.Cell("E6").Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
            ws.Cell("E6").Style.Alignment.Vertical = XLAlignmentVerticalValues.Center;
            ws.Cell("E6").Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
            ws.Range("F5:F6").Merge().Value = "หน่วยนับ";
            ws.Cell("F5").Style.Font.FontName = "TH Sarabun New";
            ws.Cell("F5").Style.Font.FontSize = 12;
            //ws.Range("F3").Style.Font.Bold = true;
            ws.Range("F5:F6").Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
            ws.Range("F5:F6").Style.Alignment.Vertical = XLAlignmentVerticalValues.Center;
            ws.Range("F5:F6").Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
            ws.Range("G5:G6").Merge().Value = "จำนวน";
            ws.Cell("G5").Style.Font.FontName = "TH Sarabun New";
            ws.Cell("G5").Style.Font.FontSize = 12;
            //ws.Range("G3").Style.Font.Bold = true;
            ws.Range("G5:G6").Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
            ws.Range("G5:G6").Style.Alignment.Vertical = XLAlignmentVerticalValues.Center;
            ws.Range("G5:G6").Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
            ws.Range("G5").Style.Alignment.SetTextRotation(90);
            ws.Range("H5:H6").Merge().Value = "ราคาต่อหน่วย";
            ws.Cell("H5").Style.Font.FontName = "TH Sarabun New";
            ws.Cell("H5").Style.Font.FontSize = 12;
            //ws.Range("H3").Style.Font.Bold = true;
            ws.Range("H5:H6").Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
            ws.Range("H5:H6").Style.Alignment.Vertical = XLAlignmentVerticalValues.Center;
            ws.Range("H5:H6").Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
            ws.Range("I4:M4").Merge().Value = "แผนความต้องการครุภัณฑ์";
            ws.Cell("I4").Style.Font.FontName = "TH Sarabun New";
            ws.Cell("I4").Style.Font.FontSize = 14;
            //ws.Range("I2").Style.Font.Bold = true;
            ws.Range("I4:M4").Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
            ws.Range("I4:M4").Style.Alignment.Vertical = XLAlignmentVerticalValues.Center;
            ws.Range("I4:M4").Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
            ws.Range("I5:M5").Merge().Value = "วงเงิน";
            ws.Cell("I5").Style.Font.FontName = "TH Sarabun New";
            ws.Cell("I5").Style.Font.FontSize = 14;
            ws.Range("I5:M5").Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
            ws.Range("I5:M5").Style.Alignment.Vertical = XLAlignmentVerticalValues.Center;
            ws.Range("I5:M5").Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
            ws.Range("N4:N6").Merge().Value = "คำอธิบายความพร้อม/ประโยชน์ที่จะได้รับ";
            ws.Cell("N4").Style.Font.FontName = "TH Sarabun New";
            ws.Cell("N4").Style.Font.FontSize = 14;
            ws.Cell("N4").Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
            ws.Cell("N4").Style.Alignment.Vertical = XLAlignmentVerticalValues.Center;
            ws.Range("N4:N6").Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
            ws.Row(4).Height = 30;
            ws.Row(5).Height = 30;
            ws.Row(6).Height = 30;
            ws.Column(1).Width = 9;
            ws.Column(2).Width = 40;
            ws.Column(3).Width = 5;
            ws.Column(4).Width = 5;
            ws.Column(5).Width = 5;
            ws.Column(6).Width = 10;
            ws.Column(7).Width = 10;
            ws.Column(8).Width = 10;
            ws.Column(9).Width = 10;
            ws.Column(10).Width = 10;
            ws.Column(11).Width = 10;
            ws.Column(12).Width = 10;
            ws.Column(13).Width = 10;
            ws.Column(14).Width = 40;
            int row = 7;
            int no = 1;

         if (summary_of_equipments != null)
            {
                string second = "ปีงบประมาณ " + summary_of_equipments.academic_year_name_th + " " +
                                summary_of_equipments.header_name;
                string third = "หน่วยงาน/คณะ " + summary_of_equipments.agency_name_th + " พื้นที่ " +
                               summary_of_equipments.location;
                ws.Range("A1:N1").Merge().Value =
                    "สรุปแผนความต้องการ รายการก่อสร้างอาคารหรือสิ่งก่อสร้างและปรับปรุงซ่อมแซมต่อเติม ของมหาวิทยาลัยเทคโนโลยีราชมงคลรัตนโกสินทร์";
                ws.Range("A1:N1").Merge().Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
                ws.Range("A1:N1").Merge().Style.Alignment.Vertical = XLAlignmentVerticalValues.Center;
                ws.Range("A2:N2").Merge().Value = second;
                ws.Range("A2:N2").Merge().Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
                ws.Range("A2:N2").Merge().Style.Alignment.Vertical = XLAlignmentVerticalValues.Center;
                ws.Range("A3:N3").Merge().Value = third;
                ws.Range("A3:N3").Merge().Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
                ws.Range("A3:N3").Merge().Style.Alignment.Vertical = XLAlignmentVerticalValues.Center;
                ws.Range("A1:A3").Style.Font.Bold = true;
                ws.Range("A1:A3").Style.Font.FontSize = 16;
                ws.Range("A1:A3").Style.Font.FontName = "TH Sarabun New";
                ws.Range("C4:H4").Merge().Value = "ปี " + summary_of_equipments.academic_year;
                ws.Cell("C4").Style.Font.FontName = "TH Sarabun New";
                ws.Cell("C4").Style.Font.FontSize = 14;
                ws.Range("C4:H4").Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
                ws.Range("C4:H4").Style.Alignment.Vertical = XLAlignmentVerticalValues.Center;
                ws.Range("C4:H4").Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
                ws.Cell("I6").Value = summary_of_equipments.budget_year_1;
                ws.Cell("I6").Style.Font.FontName = "TH Sarabun New";
                ws.Cell("I6").Style.Font.FontSize = 12;
                ws.Cell("I6").Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
                ws.Cell("I6").Style.Alignment.Vertical = XLAlignmentVerticalValues.Center;
                ws.Cell("I6").Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
                ws.Cell("J6").Value = summary_of_equipments.budget_year_2;
                ws.Cell("J6").Style.Font.FontName = "TH Sarabun New";
                ws.Cell("J6").Style.Font.FontSize = 12;
                ws.Cell("J6").Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
                ws.Cell("J6").Style.Alignment.Vertical = XLAlignmentVerticalValues.Center;
                ws.Cell("J6").Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
                ws.Cell("K6").Value = summary_of_equipments.budget_year_3;
                ws.Cell("K6").Style.Font.FontName = "TH Sarabun New";
                ws.Cell("K6").Style.Font.FontSize = 12;
                ws.Cell("K6").Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
                ws.Cell("K6").Style.Alignment.Vertical = XLAlignmentVerticalValues.Center;
                ws.Cell("K6").Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
                ws.Cell("L6").Value = summary_of_equipments.budget_year_4;
                ws.Cell("L6").Style.Font.FontName = "TH Sarabun New";
                ws.Cell("L6").Style.Font.FontSize = 12;
                ws.Cell("L6").Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
                ws.Cell("L6").Style.Alignment.Vertical = XLAlignmentVerticalValues.Center;
                ws.Cell("L6").Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
                ws.Cell("M6").Value = summary_of_equipments.budget_year_5;
                ws.Cell("M6").Style.Font.FontName = "TH Sarabun New";
                ws.Cell("M6").Style.Font.FontSize = 12;
                ws.Cell("M6").Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
                ws.Cell("M6").Style.Alignment.Vertical = XLAlignmentVerticalValues.Center;
                ws.Cell("M6").Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
                foreach (var equipments in summary_of_equipments.data)
                {
                   
                    ws.Cell(row, 1).Value = no;
                    no++;
                    ws.Cell(row, 2).Value = equipments.list;
                    ws.Cell(row, 3).Value = equipments.type_1;
                    ws.Cell(row, 4).Value = equipments.type_2;
                    ws.Cell(row, 5).Value = equipments.type_3;
                    ws.Cell(row, 6).Value = equipments.unit;
                    ws.Cell(row, 7).Value = equipments.quantity;
                    ws.Cell(row, 8).Value = equipments.unit_price;
                    ws.Cell(row, 9).Value = equipments.price_1;
                    ws.Cell(row, 10).Value = equipments.price_2;
                    ws.Cell(row, 11).Value = equipments.price_3;
                    ws.Cell(row, 12).Value = equipments.price_4;
                    ws.Cell(row, 13).Value = equipments.price_5;
                    ws.Cell(row, 14).Value = equipments.description;
                  
                    ws.Cell(row, 1).Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
                    ws.Cell(row, 2).Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
                    ws.Cell(row, 3).Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
                    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;
                    ws.Cell(row, 9).Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
                    ws.Cell(row, 10).Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
                    ws.Cell(row, 11).Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
                    ws.Cell(row, 12).Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
                    ws.Cell(row, 13).Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
                    ws.Cell(row, 14).Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
                   
                    ws.Range(ws.Cell(row, 1), ws.Cell(row, 14)).Style.Font.FontName =
                        "TH Sarabun New";
                    ws.Range(ws.Cell(row, 1), ws.Cell(row, 14)).Style.Font.FontSize = 11;
                    ws.Range(ws.Cell(row, 1), ws.Cell(row, 14)).Style.Alignment.WrapText = true;
                    ws.Range(ws.Cell(row, 1), ws.Cell(row, 14)).Style.Alignment.Vertical =
                        XLAlignmentVerticalValues.Center;
                    ws.Cell(row, 1).Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
                    ws.Cell(row, 2).Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Left;
                    ws.Cell(row, 3).Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
                    ws.Cell(row, 4).Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
                    ws.Cell(row, 5).Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
                    ws.Cell(row, 6).Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
                    ws.Cell(row, 7).Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
                    ws.Cell(row, 8).Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Right;
                    ws.Cell(row, 9).Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Right;
                    ws.Cell(row, 10).Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Right;
                    ws.Cell(row, 11).Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Right;
                    ws.Cell(row, 12).Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Right;
                    ws.Cell(row, 13).Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Right;
                    ws.Cell(row, 14).Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Left;
               
                    row++;
                }
            }
            Report report = new Report();
            report.Load(_setting.report_path + "summary_of_equipment.frx");
            report.RegisterData(_summary_of_equipment, "summary_of_equipment");
            report.Prepare();

            MemoryStream stream = new MemoryStream();
            switch (type)
            {
                case "pdf":
                    PDFExport pdf = new PDFExport();
                    report.Export(pdf, stream);
                    stream.Seek(0, SeekOrigin.Begin);
                    return File(stream, "application/pdf");
                case "mht":
                    MHTExport mht = new MHTExport();
                    report.Export(mht, stream);
                    stream.Seek(0, SeekOrigin.Begin);
                    return File(stream, "multipart/related");
                    break;
                case "csv":
                    CSVExport csv = new CSVExport();
                    report.Export(csv, stream);
                    stream.Seek(0, SeekOrigin.Begin);
                    return File(stream, "text/csv");
                    break;
                case "xlsx": case "xls":
                    workbook.SaveAs(stream);
                    var content = stream.ToArray();
                    string date = DateTime.Now.ToString("yyyyMMddHHmmss");
                    return File(
                        content,
                        "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
                        "equipments_" + date + ".xlsx");
            }
           
            // using (var stream1 = new MemoryStream())
            // {
            //     workbook.SaveAs(stream1);
            //     var content = stream1.ToArray();
            //     string date = DateTime.Now.ToString("yyyyMMddHHmmss");
            //     return File(
            //         content,
            //         "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
            //         "equipments_" + date + ".xlsx");
            // }
             return Ok();
        
    }

        [HttpPost, Route("reports/summary_of_equipment_five_year/{type}")]
        [ApiExplorerSettings(GroupName = "reports")]
        public IActionResult GetEquipmentReport([FromRoute] string type,
            [FromBody] summary_of_equipment_five_year summary_of_equipments)
        {
            var workbook = new XLWorkbook();
            var ws = workbook.Worksheets.Add("รายการครุภัณฑ์");
            var imagePath =   _setting.report_path + @"Logo-RMUTR.png";
            ws.AddPicture(imagePath).MoveTo(ws.Cell("A1")).Scale(0.27);
            ws.Range("A1:P1").Merge().Value =
                "สรุปแผนความต้องการ รายการครุภัณฑ์ ของมหาวิทยาลัยเทคโนโลยีราชมงคลรัตนโกสินทร์";
            ws.Range("A1:P1").Merge().Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
            ws.Range("A1:P1").Merge().Style.Alignment.Vertical = XLAlignmentVerticalValues.Center;
            ws.Cell("A1").Style.Font.Bold = true;
            ws.Cell("A1").Style.Font.FontName = "TH Sarabun New";
            ws.Cell("A1").Style.Font.FontSize = 16;
            ws.Range("A2:A4").Merge().Value = "ลำดับความสำคัญ(1)";
            ws.Cell("A2").Style.Alignment.WrapText = true;
            ws.Range("A2:A4").Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
            ws.Range("A2:A4").Style.Alignment.Vertical = XLAlignmentVerticalValues.Center;
            ws.Range("A2:A4").Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
            ws.Cell("A2").Style.Font.FontName = "TH Sarabun New";
            ws.Cell("A2").Style.Font.FontSize = 14;
            ws.Range("B2:B4").Merge().Value = "รายการ";
            ws.Cell("B2").Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
            ws.Cell("B2").Style.Alignment.Vertical = XLAlignmentVerticalValues.Center;
            ws.Cell("B2").Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
            ws.Cell("B2").Style.Font.FontName = "TH Sarabun New";
            ws.Cell("B2").Style.Font.FontSize = 14;
            ws.Range("C3:E3").Merge().Value = "ประเภทของครุภัณฑ์";
            ws.Range("C3:E3").Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
            ws.Range("C3:E3").Style.Alignment.Vertical = XLAlignmentVerticalValues.Center;
            ws.Range("C3:E3").Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
            ws.Cell("C3").Style.Font.FontName = "TH Sarabun New";
            ws.Cell("C3").Style.Font.FontSize = 11;
            ws.Range("C4").Value = "ทดแทนของเดิม";
            ws.Cell("C4").Style.Alignment.WrapText = true;
            ws.Cell("C4").Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
            ws.Cell("C4").Style.Alignment.Vertical = XLAlignmentVerticalValues.Center;
            ws.Cell("C4").Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
            ws.Cell("C4").Style.Font.FontName = "TH Sarabun New";
            ws.Cell("C4").Style.Font.FontSize = 11;
            ws.Range("D4").Value = "เพิ่มประสิทธิภาพ";
            ws.Cell("D4").Style.Font.FontName = "TH Sarabun New";
            ws.Cell("D4").Style.Font.FontSize = 11;
            ws.Cell("D4").Style.Alignment.WrapText = true;
            ws.Cell("D4").Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
            ws.Cell("D4").Style.Alignment.Vertical = XLAlignmentVerticalValues.Center;
            ws.Cell("D4").Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
            ws.Range("E4").Value = "ครุภัณฑ์ใหม่";
            ws.Cell("E4").Style.Font.FontName = "TH Sarabun New";
            ws.Cell("E4").Style.Font.FontSize = 11;
            ws.Cell("E4").Style.Alignment.WrapText = true;
            ws.Cell("E4").Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
            ws.Cell("E4").Style.Alignment.Vertical = XLAlignmentVerticalValues.Center;
            ws.Cell("E4").Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
            ws.Range("F3:F4").Merge().Value = "หน่วยนับ";
            ws.Cell("F3").Style.Font.FontName = "TH Sarabun New";
            ws.Cell("F3").Style.Font.FontSize = 12;
            //ws.Range("F3").Style.Font.Bold = true;
            ws.Range("F3:F4").Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
            ws.Range("F3:F4").Style.Alignment.Vertical = XLAlignmentVerticalValues.Center;
            ws.Range("F3:F4").Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
            ws.Range("G3:G4").Merge().Value = "จำนวน";
            ws.Cell("G3").Style.Font.FontName = "TH Sarabun New";
            ws.Cell("G3").Style.Font.FontSize = 12;
            //ws.Range("G3").Style.Font.Bold = true;
            ws.Range("G3:G4").Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
            ws.Range("G3:G4").Style.Alignment.Vertical = XLAlignmentVerticalValues.Center;
            ws.Range("G3:G4").Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
            ws.Range("G3").Style.Alignment.SetTextRotation(90);
            ws.Range("H3:H4").Merge().Value = "ราคาต่อหน่วย";
            ws.Cell("H3").Style.Font.FontName = "TH Sarabun New";
            ws.Cell("H3").Style.Font.FontSize = 12;
            //ws.Range("H3").Style.Font.Bold = true;
            ws.Range("H3:H4").Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
            ws.Range("H3:H4").Style.Alignment.Vertical = XLAlignmentVerticalValues.Center;
            ws.Range("H3:H4").Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
            ws.Range("I2:M2").Merge().Value = "แผนความต้องการครุภัณฑ์";
            ws.Cell("I2").Style.Font.FontName = "TH Sarabun New";
            ws.Cell("I2").Style.Font.FontSize = 14;
            //ws.Range("I2").Style.Font.Bold = true;
            ws.Range("I2:M2").Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
            ws.Range("I2:M2").Style.Alignment.Vertical = XLAlignmentVerticalValues.Center;
            ws.Range("I2:M2").Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
            ws.Range("I3:M3").Merge().Value = "วงเงิน";
            ws.Cell("I3").Style.Font.FontName = "TH Sarabun New";
            ws.Cell("I3").Style.Font.FontSize = 14;
            ws.Range("I3:M3").Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
            ws.Range("I3:M3").Style.Alignment.Vertical = XLAlignmentVerticalValues.Center;
            ws.Range("I3:M3").Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
            ws.Cell("N4").Value = "วงเงิน";
            ws.Cell("N4").Style.Font.FontName = "TH Sarabun New";
            ws.Cell("N4").Style.Font.FontSize = 12;
            //ws.Range("N4").Style.Font.Bold = true;
            ws.Cell("N4").Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
            ws.Cell("N4").Style.Alignment.Vertical = XLAlignmentVerticalValues.Center;
            ws.Cell("N4").Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
            ws.Cell("O4").Value = "ชื่อบริษัท";
            ws.Cell("O4").Style.Font.FontName = "TH Sarabun New";
            ws.Cell("O4").Style.Font.FontSize = 12;
            //ws.Range("O4").Style.Font.Bold = true;
            ws.Cell("O4").Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
            ws.Cell("O4").Style.Alignment.Vertical = XLAlignmentVerticalValues.Center;
            ws.Cell("O4").Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
            ws.Range("P2:P4").Merge().Value = "คำอธิบายความพร้อม/ประโยชน์ที่จะได้รับ";
            ws.Cell("P2").Style.Font.FontName = "TH Sarabun New";
            ws.Cell("P2").Style.Font.FontSize = 14;
            //ws.Cell("P2").Style.Font.Bold = true;
            ws.Cell("P2").Style.Alignment.WrapText = true;
            ws.Cell("P2").Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
            ws.Cell("P2").Style.Alignment.Vertical = XLAlignmentVerticalValues.Center;
            ws.Range("P2:P4").Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
            ws.Row(1).Height = 60;
            ws.Column(1).Width = 8;
            ws.Column(2).Width = 40;
            ws.Column(3).Width = 5;
            ws.Column(4).Width = 5;
            ws.Column(5).Width = 5;
            ws.Column(6).Width = 10;
            ws.Column(7).Width = 10;
            ws.Column(8).Width = 10;
            ws.Column(9).Width = 10;
            ws.Column(10).Width = 10;
            ws.Column(11).Width = 10;
            ws.Column(12).Width = 10;
            ws.Column(13).Width = 10;
            ws.Column(14).Width = 10;
            ws.Column(15).Width = 10;
            ws.Column(16).Width = 50;
        
            int row = 5;
            int no = 1;
        
            // foreach (var equipments in summary_of_equipments)
            // {
                if (summary_of_equipments != null)
                {
                    ws.Range("C2:H2").Merge().Value = "ปี " + summary_of_equipments.academic_year;
                    ws.Cell("C2").Style.Font.FontName = "TH Sarabun New";
                    ws.Cell("C2").Style.Font.FontSize = 14;
                    ws.Range("C2:H2").Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
                    ws.Range("C2:H2").Style.Alignment.Vertical = XLAlignmentVerticalValues.Center;
                    ws.Range("C2:H2").Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
                    ws.Cell("I4").Value = summary_of_equipments.budget_year_1;
                    ws.Cell("I4").Style.Font.FontName = "TH Sarabun New";
                    ws.Cell("I4").Style.Font.FontSize = 12;
                    ws.Cell("I4").Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
                    ws.Cell("I4").Style.Alignment.Vertical = XLAlignmentVerticalValues.Center;
                    ws.Cell("I4").Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
                    ws.Cell("J4").Value = summary_of_equipments.budget_year_2;
                    ws.Cell("J4").Style.Font.FontName = "TH Sarabun New";
                    ws.Cell("J4").Style.Font.FontSize = 12;
                    ws.Cell("J4").Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
                    ws.Cell("J4").Style.Alignment.Vertical = XLAlignmentVerticalValues.Center;
                    ws.Cell("J4").Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
                    ws.Cell("K4").Value = summary_of_equipments.budget_year_3;
                    ws.Cell("K4").Style.Font.FontName = "TH Sarabun New";
                    ws.Cell("K4").Style.Font.FontSize = 12;
                    ws.Cell("K4").Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
                    ws.Cell("K4").Style.Alignment.Vertical = XLAlignmentVerticalValues.Center;
                    ws.Cell("K4").Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
                    ws.Cell("L4").Value = summary_of_equipments.budget_year_4;
                    ws.Cell("L4").Style.Font.FontName = "TH Sarabun New";
                    ws.Cell("L4").Style.Font.FontSize = 12;
                    ws.Cell("L4").Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
                    ws.Cell("L4").Style.Alignment.Vertical = XLAlignmentVerticalValues.Center;
                    ws.Cell("L4").Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
                    ws.Cell("M4").Value = summary_of_equipments.budget_year_5;
                    ws.Cell("M4").Style.Font.FontName = "TH Sarabun New";
                    ws.Cell("M4").Style.Font.FontSize = 12;
                    ws.Cell("M4").Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
                    ws.Cell("M4").Style.Alignment.Vertical = XLAlignmentVerticalValues.Center;
                    ws.Cell("M4").Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
                    ws.Range("N2:O3").Merge().Value = "ใบเสนอราคาครุภัณฑ์ ปี " + summary_of_equipments.academic_year;
                    ws.Cell("N2").Style.Font.FontName = "TH Sarabun New";
                    ws.Cell("N2").Style.Font.FontSize = 14;
                    ws.Range("N2:O3").Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
                    ws.Range("N2:O3").Style.Alignment.Vertical = XLAlignmentVerticalValues.Center;
                    ws.Range("N2:O3").Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
                    ws.Cell("N2").Style.Alignment.WrapText = true;
                    foreach (var equipment in summary_of_equipments.data)
                    {
                        ws.Cell(row, 1).Value = no;
                        no++;
                        ws.Cell(row, 2).Value = equipment.list;
                        ws.Cell(row, 3).Value = equipment.type_1;
                        ws.Cell(row, 4).Value = equipment.type_2;
                        ws.Cell(row, 5).Value = equipment.type_3;
                        ws.Cell(row, 6).Value = equipment.unit;
                        ws.Cell(row, 7).Value = equipment.quantity;
                        ws.Cell(row, 8).Value = equipment.unit_price;
                        ws.Cell(row, 9).Value = equipment.price_1;
                        ws.Cell(row, 10).Value = equipment.price_2;
                        ws.Cell(row, 11).Value = equipment.price_3;
                        ws.Cell(row, 12).Value = equipment.price_4;
                        ws.Cell(row, 13).Value = equipment.price_5;
                        ws.Cell(row, 14).Value = equipment.amount;
                        ws.Cell(row, 15).Value = equipment.company;
                        ws.Cell(row, 16).Value = equipment.description;
                        ws.Cell(row, 1).Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
                        ws.Cell(row, 2).Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
                        ws.Cell(row, 3).Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
                        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;
                        ws.Cell(row, 9).Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
                        ws.Cell(row, 10).Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
                        ws.Cell(row, 11).Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
                        ws.Cell(row, 12).Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
                        ws.Cell(row, 13).Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
                        ws.Cell(row, 14).Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
                        ws.Cell(row, 15).Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
                        ws.Cell(row, 16).Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
                        ws.Range(ws.Cell(row, 1), ws.Cell(row, 16)).Style.Font.FontName =
                            "TH Sarabun New";
                        ws.Range(ws.Cell(row, 1), ws.Cell(row, 16)).Style.Font.FontSize = 12;
                        ws.Range(ws.Cell(row, 1), ws.Cell(row, 16)).Style.Alignment.WrapText = true;
                        ws.Range(ws.Cell(row, 1), ws.Cell(row, 16)).Style.Alignment.Vertical =
                            XLAlignmentVerticalValues.Center;
                        ws.Cell(row, 1).Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
                        ws.Cell(row, 2).Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Left;
                        ws.Cell(row, 3).Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
                        ws.Cell(row, 4).Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
                        ws.Cell(row, 5).Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
                        ws.Cell(row, 6).Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
                        ws.Cell(row, 7).Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Right;
                        ws.Cell(row, 8).Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Right;
                        ws.Cell(row, 9).Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Right;
                        ws.Cell(row, 10).Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Right;
                        ws.Cell(row, 11).Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Right;
                        ws.Cell(row, 12).Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Right;
                        ws.Cell(row, 13).Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Right;
                        ws.Cell(row, 14).Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Right;
                        ws.Cell(row, 15).Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Left;
                        ws.Cell(row, 16).Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Left;
                        row++;
                    }
                }
            //}
        
            using (var stream = new MemoryStream())
            {
                workbook.SaveAs(stream);
                var content = stream.ToArray();
                string date = DateTime.Now.ToString("yyyyMMddHHmmss");
                // switch (type)
                // {
                //     case "pdf":
                //         //Response.Clear();  
                //         Response.Headers.Clear();  
                //         Response.ContentType = "application/pdf";  
                //         Response.Headers.Add("Content-Length", content.Length.ToString());  
                //         stream.Seek(0, SeekOrigin.Begin);
                //         return File(content,
                //             "application/pdf",
                //             "equipment_" + date + ".pdf");
                //     case "xlsx":
                return File(
                    content,
                    "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
                    "equipment_" + date + ".xlsx");
                //}

            }
        }

        [HttpPost, Route("reports/statement_of_equipment/{type}")]
        [ApiExplorerSettings(GroupName = "reports")]
        public IActionResult GetStEquipmentReport([FromRoute] string type, [FromBody] statement_of_equipment equipments)
        {
            var a = equipments.page_1.ToArray();
            foreach (var aa in a)
            {
                var s1 = aa.page_1_detail.Sum(d => d.quantity);
                var s2 = aa.page_1_detail.Sum(d => d.unit_price);
                var s3 = aa.page_1_detail.Sum(d => d.amount);
                foreach (var b in aa.page_1_detail)
                {
                    if (b.quantity != null)
                    {
                        aa.sum1 = s1;
                    }

                    if (b.quantity == null)
                    {
                        aa.sum1 = 0;
                    }

                    if (b.unit_price != null)
                    {
                        aa.sum2 = s2;
                    }

                    if (b.unit_price == null)
                    {
                        aa.sum2 = 0;
                    }

                    if (b.amount != null)
                    {
                        aa.sum3 = s3;
                    }

                    if (b.amount == null)
                    {
                        aa.sum3 = 0;
                    }
                }
            }

            var _statement_of_equipment = new List<statement_of_equipment>() {equipments};

            Report report = new Report();
            report.Load(_setting.report_path + "statement_of_equipment.frx");
            report.RegisterData(_statement_of_equipment, "statement_of_equipment");
            report.Prepare();

            MemoryStream stream = new MemoryStream();
            switch (type)
            {
                case "pdf":
                    PDFExport pdf = new PDFExport();
                    report.Export(pdf, stream);
                    stream.Seek(0, SeekOrigin.Begin);
                    return File(stream, "application/pdf");

                case "xls":
                    Excel2007Export excel = new Excel2007Export();
                    report.Export(excel, stream);
                    stream.Seek(0, SeekOrigin.Begin);
                    return File(stream, "application/vnd.ms-excel");
                    break;
                case "xlsx":
                    Excel2007Export excel1 = new Excel2007Export();
                    report.Export(excel1, stream);
                    stream.Seek(0, SeekOrigin.Begin);
                    return File(stream, "application/vnd.ms-excel");
                    break;
                case "mht":
                    MHTExport mht = new MHTExport();
                    report.Export(mht, stream);
                    stream.Seek(0, SeekOrigin.Begin);
                    return File(stream, "multipart/related");
                    break;
                case "csv":
                    CSVExport csv = new CSVExport();
                    report.Export(csv, stream);
                    stream.Seek(0, SeekOrigin.Begin);
                    return File(stream, "text/csv");
                    break;
            }

            return Ok();
        }

        
        [HttpPost, Route("reports/summary_building_construction/{type}")]
        [ApiExplorerSettings(GroupName = "reports")]
        public IActionResult GetSum2Report([FromRoute] string type,
            [FromBody] summary_building_construction summary_building_constructions)
        {
            {
                var workbook = new XLWorkbook();
                var ws = workbook.Worksheets.Add("รายการก่อสร้างอาคาร");
                var imagePath = _setting.report_path + @"Logo-RMUTR.png";
                ws.AddPicture(imagePath).MoveTo(ws.Cell("A1")).Scale(0.3);
                ws.Range("A4:A6").Merge().Value = "ลำดับความสำคัญ(1)";
                ws.Range("A4:A6").Style.Alignment.WrapText = true;
                ws.Range("A4:A6").Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
                ws.Range("A4:A6").Style.Alignment.Vertical = XLAlignmentVerticalValues.Center;
                ws.Range("A4:A6").Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
                ws.Cell("A4").Style.Font.FontName = "TH Sarabun New";
                ws.Cell("A4").Style.Font.FontSize = 14;
                ws.Range("A4:A6").Style.Fill.BackgroundColor = XLColor.MistyRose;
                ws.Range("B4:B6").Merge().Value = "รายการ";
                ws.Cell("B4").Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
                ws.Cell("B4").Style.Alignment.Vertical = XLAlignmentVerticalValues.Center;
                ws.Cell("B4").Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
                ws.Cell("B4").Style.Font.FontName = "TH Sarabun New";
                ws.Cell("B4").Style.Font.FontSize = 14;
                ws.Range("B4:B6").Style.Fill.BackgroundColor = XLColor.MistyRose;
                ws.Range("C4:C6").Merge().Value = "จำนวน";
                ws.Cell("C4").Style.Font.FontName = "TH Sarabun New";
                ws.Cell("C4").Style.Font.FontSize = 14;
                ws.Range("C4:C6").Style.Fill.BackgroundColor = XLColor.MistyRose;
                ws.Range("C4:C6").Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
                ws.Range("C4:C6").Style.Alignment.Vertical = XLAlignmentVerticalValues.Center;
                ws.Range("C4:C6").Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
                ws.Range("C4").Style.Alignment.SetTextRotation(90);
                ws.Range("D4:F4").Merge().Value = "อายุการใช้งาน";
                ws.Cell("D4").Style.Font.FontName = "TH Sarabun New";
                ws.Cell("D4").Style.Font.FontSize = 14;
                ws.Range("D4:F4").Style.Fill.BackgroundColor = XLColor.MistyRose;
                ws.Range("D4:F4").Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
                ws.Range("D4:F4").Style.Alignment.Vertical = XLAlignmentVerticalValues.Center;
                ws.Range("D4:F4").Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
                ws.Cell("D5").Value = "ต่ำกว่า 10 ปี";
                ws.Cell("D5").Style.Alignment.WrapText = true;
                ws.Cell("D5").Style.Font.FontName = "TH Sarabun New";
                ws.Cell("D5").Style.Font.FontSize = 10;
                ws.Cell("D5").Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
                ws.Cell("D5").Style.Alignment.Vertical = XLAlignmentVerticalValues.Center;
                ws.Cell("D5").Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
                ws.Cell("E5").Value = "10-20 ปี";
                ws.Cell("E5").Style.Alignment.WrapText = true;
                ws.Cell("E5").Style.Font.FontName = "TH Sarabun New";
                ws.Cell("E5").Style.Font.FontSize = 10;
                ws.Cell("E5").Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
                ws.Cell("E5").Style.Alignment.Vertical = XLAlignmentVerticalValues.Center;
                ws.Cell("E5").Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
                ws.Cell("F5").Value = "มากกว่า 20 ปี";
                ws.Cell("F5").Style.Alignment.WrapText = true;
                ws.Cell("F5").Style.Font.FontName = "TH Sarabun New";
                ws.Cell("F5").Style.Font.FontSize = 10;
                ws.Cell("F5").Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
                ws.Cell("F5").Style.Alignment.Vertical = XLAlignmentVerticalValues.Center;
                ws.Cell("F5").Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
                ws.Range("D5:F5").Style.Fill.BackgroundColor = XLColor.MistyRose;
                ws.Cell("D6").Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
                ws.Cell("E6").Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
                ws.Cell("F6").Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
                ws.Range("G4:G6").Merge().Value = "ปัญหาของอาคาร";
                ws.Cell("G4").Style.Font.FontName = "TH Sarabun New";
                ws.Cell("G4").Style.Font.FontSize = 14;
                ws.Range("G4:G6").Style.Fill.BackgroundColor = XLColor.MistyRose;
                ws.Range("G4:G6").Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
                ws.Range("G4:G6").Style.Alignment.Vertical = XLAlignmentVerticalValues.Center;
                ws.Range("G4:G6").Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
                ws.Range("H4:I4").Merge().Value = "ประเภท";
                ws.Cell("H4").Style.Font.FontName = "TH Sarabun New";
                ws.Cell("H4").Style.Font.FontSize = 14;
                ws.Range("H4:I4").Style.Fill.BackgroundColor = XLColor.MistyRose;
                ws.Range("H4:I4").Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
                ws.Range("H4:I4").Style.Alignment.Vertical = XLAlignmentVerticalValues.Center;
                ws.Range("H4:I4").Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
                ws.Cell("H5").Value = "ทดแทนอาคารเดิม";
                ws.Cell("H5").Style.Alignment.WrapText = true;
                ws.Cell("H5").Style.Font.FontName = "TH Sarabun New";
                ws.Cell("H5").Style.Font.FontSize = 10;
                ws.Cell("H5").Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
                ws.Cell("H5").Style.Alignment.Vertical = XLAlignmentVerticalValues.Center;
                ws.Cell("H5").Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
                ws.Cell("I5").Value = "อาคารใหม่";
                ws.Cell("I5").Style.Font.FontName = "TH Sarabun New";
                ws.Cell("I5").Style.Font.FontSize = 10;
                ws.Cell("I5").Style.Alignment.WrapText = true;
                ws.Cell("I5").Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
                ws.Cell("I5").Style.Alignment.Vertical = XLAlignmentVerticalValues.Center;
                ws.Cell("I5").Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
                ws.Range("H5:I5").Style.Fill.BackgroundColor = XLColor.MistyRose;
                ws.Range("J4:J5").Merge().Value = "ปรับปรุง/ซ่อมแซม/ต่อเติม";
                ws.Range("J4:J5").Style.Alignment.WrapText = true;
                ws.Cell("J4").Style.Font.FontName = "TH Sarabun New";
                ws.Cell("J4").Style.Font.FontSize = 14;
                ws.Range("J4:J5").Style.Fill.BackgroundColor = XLColor.MistyRose;
                ws.Range("J4:J5").Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
                ws.Range("J4:J5").Style.Alignment.Vertical = XLAlignmentVerticalValues.Center;
                ws.Range("J4:J5").Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
                ws.Cell("J6").Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
                ws.Cell("I6").Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
                ws.Cell("H6").Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
                ws.Range("K4:K5").Merge().Value = "วงเงินที่จะเสนอของบประมาณ";
                ws.Range("K4:K5").Style.Alignment.WrapText = true;
                ws.Cell("K4").Style.Font.FontName = "TH Sarabun New";
                ws.Cell("K4").Style.Font.FontSize = 14;
                ws.Range("K4:K5").Style.Fill.BackgroundColor = XLColor.MistyRose;
                ws.Range("K4:K5").Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
                ws.Range("K4:K5").Style.Alignment.Vertical = XLAlignmentVerticalValues.Center;
                ws.Range("K4:K5").Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
                ws.Cell("K6").Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
                ws.Range("L4:P4").Merge().Value = "ปีงบประมาณที่จะเสนอขอ";
                ws.Range("L4:P4").Style.Alignment.WrapText = true;
                ws.Cell("L4").Style.Font.FontName = "TH Sarabun New";
                ws.Cell("L4").Style.Font.FontSize = 14;
                ws.Range("L4:P4").Style.Fill.BackgroundColor = XLColor.MistyRose;
                ws.Range("L4:P4").Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
                ws.Range("L4:P4").Style.Alignment.Vertical = XLAlignmentVerticalValues.Center;
                ws.Range("L4:P4").Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
                ws.Range("L6:P6").Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
                ws.Cell("Q5").Value = "BOQ";
                ws.Cell("Q5").Style.Alignment.WrapText = true;
                ws.Cell("Q5").Style.Font.FontName = "TH Sarabun New";
                ws.Cell("Q5").Style.Font.FontSize = 10;
                ws.Cell("Q5").Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
                ws.Cell("Q5").Style.Alignment.Vertical = XLAlignmentVerticalValues.Center;
                ws.Cell("Q5").Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
                ws.Cell("R5").Value = "แบบรูป";
                ws.Cell("R5").Style.Font.FontName = "TH Sarabun New";
                ws.Cell("R5").Style.Font.FontSize = 10;
                ws.Range("Q5:R5").Style.Fill.BackgroundColor = XLColor.MistyRose;
                ws.Cell("R5").Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
                ws.Cell("R5").Style.Alignment.Vertical = XLAlignmentVerticalValues.Center;
                ws.Cell("R5").Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
                ws.Range("Q6:R6").Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
                ws.Range("S4:S5").Merge().Value = "คำอธิบาย";
                ws.Range("S4:S5").Style.Alignment.WrapText = true;
                ws.Cell("S4").Style.Font.FontName = "TH Sarabun New";
                ws.Cell("S4").Style.Font.FontSize = 14;
                ws.Range("S4:S5").Style.Fill.BackgroundColor = XLColor.MistyRose;
                ws.Range("S4:S5").Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
                ws.Range("S4:S5").Style.Alignment.Vertical = XLAlignmentVerticalValues.Center;
                ws.Range("S4:S5").Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
                ws.Cell("S6").Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
                ws.Row(4).Height = 30;
                ws.Row(5).Height = 30;
                ws.Row(6).Height = 30;
                ws.Column(1).Width = 9;
                ws.Column(2).Width = 40;
                ws.Column(3).Width = 5;
                ws.Column(4).Width = 5;
                ws.Column(5).Width = 5;
                ws.Column(6).Width = 5;
                ws.Column(7).Width = 30;
                ws.Column(8).Width = 5;
                ws.Column(9).Width = 5;
                ws.Column(10).Width = 10;
                ws.Column(11).Width = 20;
                ws.Column(12).Width = 5;
                ws.Column(13).Width = 5;
                ws.Column(14).Width = 5;
                ws.Column(15).Width = 5;
                ws.Column(16).Width = 5;
                ws.Column(17).Width = 15;
                ws.Column(18).Width = 15;
                ws.Column(19).Width = 50;

                int row = 7;
                int no = 1;
               
                // foreach (var build in summary_building_constructions)
                // {
                    if (summary_building_constructions != null)
                    {
                        //string first = "สรุปแผนความต้องการ รายการก่อสร้างอาคารหรือสิ่งก่อสร้างและปรับปรุงซ่อมแซมต่อเติม ของมหาวิทยาลัยเทคโนโลยีราชมงคลรัตนโกสินทร์";
                        string second = "ปีงบประมาณ " + summary_building_constructions.academic_year_name_th + " " + summary_building_constructions.header_name;
                        string third = "หน่วยงาน/คณะ " + summary_building_constructions.agency_name_th + " พื้นที่ " + summary_building_constructions.location;
                        ws.Range("A1:S1").Merge().Value = "สรุปแผนความต้องการ รายการก่อสร้างอาคารหรือสิ่งก่อสร้างและปรับปรุงซ่อมแซมต่อเติม ของมหาวิทยาลัยเทคโนโลยีราชมงคลรัตนโกสินทร์";
                        ws.Range("A1:S1").Merge().Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
                        ws.Range("A1:S1").Merge().Style.Alignment.Vertical = XLAlignmentVerticalValues.Center;
                        ws.Range("A2:S2").Merge().Value = second;
                        ws.Range("A2:S2").Merge().Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
                        ws.Range("A2:S2").Merge().Style.Alignment.Vertical = XLAlignmentVerticalValues.Center;
                        ws.Range("A3:S3").Merge().Value = third;
                        ws.Range("A3:S3").Merge().Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
                        ws.Range("A3:S3").Merge().Style.Alignment.Vertical = XLAlignmentVerticalValues.Center;
                        ws.Range("A1:A3").Style.Font.Bold = true;
                        ws.Range("A1:A3").Style.Font.FontSize = 16;
                        ws.Range("A1:A3").Style.Font.FontName = "TH Sarabun New";
                        ws.Cell("L5").Value = summary_building_constructions.budget_year_1;
                        ws.Cell("L5").Style.Alignment.WrapText = true;
                        ws.Cell("L5").Style.Font.FontName = "TH Sarabun New";
                        ws.Cell("L5").Style.Font.FontSize = 10;
                        ws.Cell("L5").Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
                        ws.Cell("L5").Style.Alignment.Vertical = XLAlignmentVerticalValues.Center;
                        ws.Cell("L5").Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
                        ws.Cell("M5").Value = summary_building_constructions.budget_year_2;
                        ws.Cell("M5").Style.Alignment.WrapText = true;
                        ws.Cell("M5").Style.Font.FontName = "TH Sarabun New";
                        ws.Cell("M5").Style.Font.FontSize = 10;
                        ws.Cell("M5").Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
                        ws.Cell("M5").Style.Alignment.Vertical = XLAlignmentVerticalValues.Center;
                        ws.Cell("M5").Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
                        ws.Cell("N5").Value = summary_building_constructions.budget_year_3;
                        ws.Cell("N5").Style.Alignment.WrapText = true;
                        ws.Cell("N5").Style.Font.FontName = "TH Sarabun New";
                        ws.Cell("N5").Style.Font.FontSize = 10;
                        ws.Cell("N5").Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
                        ws.Cell("N5").Style.Alignment.Vertical = XLAlignmentVerticalValues.Center;
                        ws.Cell("N5").Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
                        ws.Cell("O5").Value = summary_building_constructions.budget_year_4;
                        ws.Cell("O5").Style.Alignment.WrapText = true;
                        ws.Cell("O5").Style.Font.FontName = "TH Sarabun New";
                        ws.Cell("O5").Style.Font.FontSize = 10;
                        ws.Cell("O5").Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
                        ws.Cell("O5").Style.Alignment.Vertical = XLAlignmentVerticalValues.Center;
                        ws.Cell("O5").Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
                        ws.Cell("P5").Value = summary_building_constructions.budget_year_5;
                        ws.Cell("P5").Style.Alignment.WrapText = true;
                        ws.Cell("P5").Style.Font.FontName = "TH Sarabun New";
                        ws.Cell("P5").Style.Font.FontSize = 10;
                        ws.Cell("P5").Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
                        ws.Cell("P5").Style.Alignment.Vertical = XLAlignmentVerticalValues.Center;
                        ws.Cell("P5").Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
                        ws.Range("L5:P5").Style.Fill.BackgroundColor = XLColor.MistyRose;
                        ws.Range("Q4:R4").Merge().Value = "ความพร้อมสิ่งก่อสร้าง ปี "+summary_building_constructions.academic_year;
                        ws.Range("Q4:R4").Style.Fill.BackgroundColor = XLColor.MistyRose;
                        ws.Cell("Q4").Style.Font.FontName = "TH Sarabun New";
                        ws.Cell("Q4").Style.Font.FontSize = 14;
                        ws.Range("Q4:R4").Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
                        ws.Range("Q4:R4").Style.Alignment.Vertical = XLAlignmentVerticalValues.Center;
                        ws.Range("Q4:R4").Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
                        ws.Range("L6:R6").Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
                        ws.Range("D6:F6").Style.Fill.BackgroundColor = XLColor.Gainsboro;
                        ws.Range("H6:S6").Style.Fill.BackgroundColor = XLColor.Gainsboro;
                        foreach (var building in summary_building_constructions.data)
                        {
                            ws.Cell(row, 1).Value = no;
                            no++;
                            ws.Cell(row, 2).Value = building.list;
                            ws.Cell(row, 3).Value = building.total;
                            ws.Cell(row, 4).Value = building.lesser;
                            ws.Cell(row, 5).Value = building.between;
                            ws.Cell(row, 6).Value = building.over;
                            ws.Cell(row, 7).Value = building.problem;
                            ws.Cell(row, 8).Value = building.type_1;
                            ws.Cell(row, 9).Value = building.type_2;
                            ws.Cell(row, 10).Value = building.renovated;
                            ws.Cell(row, 11).Value = building.budget;
                            ws.Cell(row, 12).Value = building.price_1;
                            ws.Cell(row, 13).Value = building.price_2;
                            ws.Cell(row, 14).Value = building.price_3;
                            ws.Cell(row, 15).Value = building.price_4;
                            ws.Cell(row, 16).Value = building.price_5;
                            ws.Cell(row, 17).Value = building.boq;
                            ws.Cell(row, 18).Value = building.image;
                            ws.Cell(row, 19).Value = building.description;
                            ws.Cell(row, 1).Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
                            ws.Cell(row, 2).Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
                            ws.Cell(row, 3).Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
                            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;
                            ws.Cell(row, 9).Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
                            ws.Cell(row, 10).Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
                            ws.Cell(row, 11).Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
                            ws.Cell(row, 12).Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
                            ws.Cell(row, 13).Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
                            ws.Cell(row, 14).Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
                            ws.Cell(row, 15).Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
                            ws.Cell(row, 16).Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
                            ws.Cell(row, 17).Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
                            ws.Cell(row, 18).Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
                            ws.Cell(row, 19).Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
                            ws.Range(ws.Cell(row, 1), ws.Cell(row, 19)).Style.Font.FontName =
                                "TH Sarabun New";
                            ws.Range(ws.Cell(row, 1), ws.Cell(row, 19)).Style.Font.FontSize = 11;
                            ws.Range(ws.Cell(row, 1), ws.Cell(row, 19)).Style.Alignment.WrapText = true;
                            ws.Range(ws.Cell(row, 1), ws.Cell(row, 19)).Style.Alignment.Vertical =
                                XLAlignmentVerticalValues.Center;
                            ws.Cell(row, 1).Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
                            ws.Cell(row, 2).Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Left;
                            ws.Cell(row, 3).Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
                            ws.Cell(row, 4).Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
                            ws.Cell(row, 5).Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
                            ws.Cell(row, 6).Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
                            ws.Cell(row, 7).Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Left;
                            ws.Cell(row, 8).Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
                            ws.Cell(row, 9).Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
                            ws.Cell(row, 10).Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
                            ws.Cell(row, 11).Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
                            ws.Cell(row, 12).Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Right;
                            ws.Cell(row, 13).Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Right;
                            ws.Cell(row, 14).Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Right;
                            ws.Cell(row, 15).Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Right;
                            ws.Cell(row, 16).Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Right;
                            ws.Cell(row, 17).Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
                            ws.Cell(row, 18).Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
                            ws.Cell(row, 19).Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Left;
                            row++;
                        }
                    }
                //}

                using (var stream = new MemoryStream())
                {
                    workbook.SaveAs(stream);
                    var content = stream.ToArray();
                    string date = DateTime.Now.ToString("yyyyMMddHHmmss");
                    return File(
                        content,
                        "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
                        "building_" + date + ".xlsx");
                }
            }
        }
    }
}