using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
using NPOI.SS.Util;
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 ElectricitySoldRecordController : MyBaseController<ElectricitySoldRecordController>
    {
        private readonly IElectricitySoldRecordRepository electricitySoldRecordRepository;
        private readonly IPowerStationRepository powerStationRepository;

        public ElectricitySoldRecordController(IElectricitySoldRecordRepository electricitySoldRecordRepository, IPowerStationRepository powerStationRepository) : base()
        {
            this.electricitySoldRecordRepository = electricitySoldRecordRepository;
            this.powerStationRepository = powerStationRepository;
        }
        public IActionResult Index()
        {
            return View();
        }
        public async Task<ApiResult<string>> SaveSoldMoney(ElectricitySoldRecord post)
        {
            ApiResult<string> apiResult = new ApiResult<string>();

            PowerStation powerStation = null;

            try
            {
                powerStation = await powerStationRepository.GetOneAsync(post.PowerstationId);

                if (post.Id == 0)
                {
                    ElectricitySoldRecord record = new ElectricitySoldRecord()
                    {
                        EndAt = post.EndAt,
                        CreatedBy = myUser.Id,
                        Kwh = post.Kwh,
                        Money = post.Money,
                        PowerstationId = post.PowerstationId,
                        StartAt = post.StartAt
                    };
                    List<string> properties = new List<string>()
                    {
                        "EndAt",
                        "CreatedBy",
                        "Kwh",
                        "Money",
                        "PowerstationId",
                        "StartAt"
                    };
                    await electricitySoldRecordRepository.AddAsync(record, properties);

                    apiResult.Code = "0000";
                    apiResult.Msg = "新增成功";
                }
                else
                {
                    ElectricitySoldRecord record = new ElectricitySoldRecord()
                    {
                        Id = post.Id,
                        EndAt = post.EndAt,
                        UpdatedBy = myUser.Id,
                        Kwh = post.Kwh,
                        Money = post.Money,
                        StartAt = post.StartAt
                    };
                    List<string> properties = new List<string>()
                    {
                        "Id",
                        "EndAt",
                        "UpdatedBy",
                        "Kwh",
                        "Money",
                        "StartAt"
                    };
                    await electricitySoldRecordRepository.Update(record, properties);
                    apiResult.Code = "0000";
                    apiResult.Msg = "儲存成功";
                }

            }
            catch (Exception exception)
            {
                apiResult.Code = "9999";
                apiResult.Msg = errorCode.GetString(apiResult.Code);
                string json = System.Text.Json.JsonSerializer.Serialize(post);
                Logger.LogError("【" + controllerName + "/" + actionName + "】" + json);
                Logger.LogError("【" + controllerName + "/" + actionName + "】" + exception.Message);
            }

            return apiResult;
        }

        [HttpPost]
        public async Task<ActionResult> RecordTable(ElectricitySoldRecordTablePost info)
        {
            List<ElectricitySoldRecordTable> electricitySoldRecordTable = new List<ElectricitySoldRecordTable>();
            ApiResult<List<ElectricitySoldRecordTable>> apiResult = new ApiResult<List<ElectricitySoldRecordTable>>();
            try
            {
                if(info.StationId == null || info.Time == null)
                {
                    apiResult.Code = "0000";
                }
                else
                {
                    electricitySoldRecordTable = await electricitySoldRecordRepository.RecordTable(info);
                    foreach (ElectricitySoldRecordTable a in electricitySoldRecordTable)
                    {
                        a.Function = @"
                                <button type='button' class='btn btn-success btn-pills waves-effect waves-themed bill-btn'>發票</button>
                                <button type='button' class='btn btn-primary btn-pills waves-effect waves-themed edit-btn'>修改</button>
                                <button type='button' class='btn btn-danger btn-pills waves-effect waves-themed del-btn'>刪除</button>";
                        a.CreatedDay = Convert.ToDateTime(a.CreatedAt).ToString("yyyy-MM-dd");
                    }
                    apiResult.Code = "0000";
                    apiResult.Data = electricitySoldRecordTable;
                }
            }
            catch (Exception exception)
            {
                apiResult.Code = "9999";
                apiResult.Msg = exception.ToString();

                Logger.LogError("【" + controllerName + "/" + actionName + "】" + "info=" + System.Text.Json.JsonSerializer.Serialize(info));
                Logger.LogError("【" + controllerName + "/" + actionName + "】" + exception.Message);
            }
            var result = Json(new
            {
                data = apiResult
            });
            return result;
        }

        public async Task<ApiResult<ElectricitySoldRecord>> GetOnePowerStation(int id)
        {
            ApiResult<ElectricitySoldRecord> apiResult = new ApiResult<ElectricitySoldRecord>();
            ElectricitySoldRecord record = new ElectricitySoldRecord();
            try
            {
                record = await electricitySoldRecordRepository.GetOneAsync(id);
                apiResult.Code = "0000";
                apiResult.Data = record;

            }
            catch (Exception exception)
            {
                apiResult.Code = "9999";
                Logger.LogError("【" + controllerName + "/" + actionName + "】" + "Id=" + id);
                Logger.LogError("【" + controllerName + "/" + actionName + "】" + exception.Message);

            }

            apiResult.Msg = errorCode.GetString(apiResult.Code);
            return apiResult;
        }

        public async Task<ApiResult<string>> DeleteRecord(int id)
        {
            ApiResult<string> apiResult = new ApiResult<string>();
            try
            {
                await electricitySoldRecordRepository.DeleteOne(id);
                apiResult.Code = "0000";
                apiResult.Msg = "刪除成功";

            }
            catch (Exception exception)
            {
                apiResult.Code = "9999";
                apiResult.Msg = errorCode.GetString(apiResult.Code);
                Logger.LogError("【" + controllerName + "/" + actionName + "】" + "Id=" + id);
                Logger.LogError("【" + controllerName + "/" + actionName + "】" + exception.Message);
            }
            
            return apiResult;

        }


        public FileResult BillExcel (int post)
        {
            var workbook = new XSSFWorkbook();
            #region excel設定
            IFont font26 = workbook.CreateFont();
            font26.FontName = "新細明體";
            font26.FontHeightInPoints = 26;
            font26.IsBold = true;
            ICellStyle style26 = workbook.CreateCellStyle();
            style26.SetFont(font26);
            style26.Alignment = HorizontalAlignment.Center;
            style26.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;
            IFont font14NoBold = workbook.CreateFont();
            font14NoBold.FontName = "新細明體";
            font14NoBold.FontHeightInPoints = 14;
            font14NoBold.IsBold = false;

            IFont font14 = workbook.CreateFont();
            font14.FontName = "新細明體";
            font14.FontHeightInPoints = 14;
            font14.IsBold = true;

            ICellStyle style14 = workbook.CreateCellStyle();
            style14.SetFont(font14);
            style14.Alignment = HorizontalAlignment.Left;
            style14.VerticalAlignment = VerticalAlignment.Center;

            ICellStyle buttomLine14 = workbook.CreateCellStyle();
            buttomLine14.BorderBottom = BorderStyle.Medium;

            IFont fontBlue14 = workbook.CreateFont();
            fontBlue14.FontName = "新細明體";
            fontBlue14.FontHeightInPoints = 14;
            fontBlue14.IsBold = true;
            fontBlue14.Color = NPOI.HSSF.Util.HSSFColor.Indigo.Index;

            ICellStyle buttomLineBlue14 = workbook.CreateCellStyle();
            buttomLineBlue14.SetFont(fontBlue14);
            buttomLineBlue14.BorderBottom = BorderStyle.Medium;

            ICellStyle style14upleft = workbook.CreateCellStyle();
            style14upleft.SetFont(font14);
            style14upleft.Alignment = HorizontalAlignment.Center;
            style14upleft.VerticalAlignment = VerticalAlignment.Center;
            style14upleft.BorderTop = BorderStyle.Medium;
            style14upleft.BorderLeft = BorderStyle.Medium;
            style14upleft.BorderRight = BorderStyle.Thin;
            style14upleft.BorderBottom = BorderStyle.Thin;

            ICellStyle style14up = workbook.CreateCellStyle();
            style14up.SetFont(font14);
            style14up.Alignment = HorizontalAlignment.Center;
            style14up.VerticalAlignment = VerticalAlignment.Center;
            style14up.BorderTop = BorderStyle.Medium;
            style14up.BorderLeft = BorderStyle.Thin;
            style14up.BorderRight = BorderStyle.Thin;
            style14up.BorderBottom = BorderStyle.Thin;

            ICellStyle style14upright = workbook.CreateCellStyle();
            style14upright.SetFont(font14);
            style14upright.Alignment = HorizontalAlignment.Center;
            style14upright.VerticalAlignment = VerticalAlignment.Center;
            style14upright.BorderTop = BorderStyle.Medium;
            style14upright.BorderLeft = BorderStyle.Thin;
            style14upright.BorderRight = BorderStyle.Medium;
            style14upright.BorderBottom = BorderStyle.Thin;

            ICellStyle style14bodyleft = workbook.CreateCellStyle();
            style14bodyleft.SetFont(font14NoBold);
            style14bodyleft.Alignment = HorizontalAlignment.Center;
            style14bodyleft.VerticalAlignment = VerticalAlignment.Center;
            style14bodyleft.BorderTop = BorderStyle.Thin;
            style14bodyleft.BorderLeft = BorderStyle.Medium;
            style14bodyleft.BorderRight = BorderStyle.Thin;
            style14bodyleft.BorderBottom = BorderStyle.Thin;

            ICellStyle style14body = workbook.CreateCellStyle();
            style14body.SetFont(font14NoBold);
            style14body.Alignment = HorizontalAlignment.Center;
            style14body.VerticalAlignment = VerticalAlignment.Center;
            style14body.BorderTop = BorderStyle.Thin;
            style14body.BorderLeft = BorderStyle.Thin;
            style14body.BorderRight = BorderStyle.Thin;
            style14body.BorderBottom = BorderStyle.Thin;

            ICellStyle style14bodyright = workbook.CreateCellStyle();
            style14bodyright.SetFont(font14NoBold);
            style14bodyright.Alignment = HorizontalAlignment.Center;
            style14bodyright.VerticalAlignment = VerticalAlignment.Center;
            style14bodyright.BorderTop = BorderStyle.Thin;
            style14bodyright.BorderLeft = BorderStyle.Thin;
            style14bodyright.BorderRight = BorderStyle.Medium;
            style14bodyright.BorderBottom = BorderStyle.Thin;

            ICellStyle style14bodyrightnoborder = workbook.CreateCellStyle();
            style14bodyrightnoborder.SetFont(font14);
            style14bodyrightnoborder.Alignment = HorizontalAlignment.Left;
            style14bodyrightnoborder.VerticalAlignment = VerticalAlignment.Center;
            style14bodyrightnoborder.BorderTop = BorderStyle.None;
            style14bodyrightnoborder.BorderBottom = BorderStyle.None;
            style14bodyrightnoborder.BorderLeft = BorderStyle.None;
            style14bodyrightnoborder.BorderRight = BorderStyle.Medium;

            ICellStyle style14bodyleftnoborder = workbook.CreateCellStyle();
            style14bodyleftnoborder.SetFont(font14);
            style14bodyleftnoborder.Alignment = HorizontalAlignment.Left;
            style14bodyleftnoborder.VerticalAlignment = VerticalAlignment.Top;
            style14bodyleftnoborder.BorderLeft = BorderStyle.Medium;
            style14bodyleftnoborder.WrapText = true;

            ICellStyle style14bodyleftrigrtbottom = workbook.CreateCellStyle();
            style14bodyleftrigrtbottom.SetFont(font14);
            style14bodyleftrigrtbottom.Alignment = HorizontalAlignment.Left;
            style14bodyleftrigrtbottom.VerticalAlignment = VerticalAlignment.Center;
            style14bodyleftrigrtbottom.BorderRight = BorderStyle.Medium;
            style14bodyleftrigrtbottom.BorderBottom = BorderStyle.Medium;
            style14bodyleftrigrtbottom.BorderLeft = BorderStyle.Medium;
            #endregion

            var bill = electricitySoldRecordRepository.GetBill(post);
            var sheet = workbook.CreateSheet("附件一-開立發票申請表");

            IRow row;
            ICell cell;
            sheet.SetColumnWidth(0, 1 * 160 * 4);
            sheet.SetColumnWidth(1, 4 * 160 * 8);
            sheet.SetColumnWidth(2, 6 * 160 * 8);
            sheet.SetColumnWidth(3, 4 * 160 * 8);
            sheet.SetColumnWidth(4, 6 * 160 * 8);
            sheet.SetColumnWidth(5, 5 * 160 * 8);
            sheet.SetColumnWidth(6, 5 * 160 * 8);

            CellRangeAddress region;
            row = sheet.CreateRow(1); 
            cell = row.CreateCell(1);
            region = new CellRangeAddress(1, 1, 1, 6);
            sheet.AddMergedRegion(region);
            cell.SetCellValue(bill.Result.Name);
            cell.CellStyle = style26;

            if(bill.Result.Logo != null)
            {
                byte[] bytes = System.IO.File.ReadAllBytes(Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "upload", "company_logo", bill.Result.Logo));
                XSSFDrawing patriarch = (XSSFDrawing)sheet.CreateDrawingPatriarch();
                XSSFClientAnchor regionr = (XSSFClientAnchor)patriarch.CreateAnchor(0, 0, 2, 2, 1, 1, 2, 3);
                patriarch.CreatePicture(regionr, workbook.AddPicture(bytes, XSSFWorkbook.PICTURE_TYPE_JPEG));
            }
            
            row = sheet.CreateRow(2);
            cell = row.CreateCell(1);
            region = new CellRangeAddress(2, 2, 1, 6);
            sheet.AddMergedRegion(region);
            cell.SetCellValue("開立統一發票申請表");
            cell.CellStyle = style26;

            row = sheet.CreateRow(4);
            cell = row.CreateCell(5);
            cell.SetCellValue("單據編號:");
            cell.CellStyle = style14;

            cell = row.CreateCell(6);
            cell.SetCellValue("");
            cell.CellStyle = buttomLine14;

            row = sheet.CreateRow(5);           //第5行
            cell = row.CreateCell(1);
            cell.SetCellValue("申  請  人 ：");
            cell.CellStyle = style14;

            cell = row.CreateCell(2);
            cell.SetCellValue("");
            cell.CellStyle = buttomLine14;

            cell = row.CreateCell(3);
            cell.SetCellValue("分       機：");
            cell.CellStyle = style14;

            cell = row.CreateCell(4);
            cell.SetCellValue("");
            cell.CellStyle = buttomLine14;

            row = sheet.CreateRow(6);              //第6行
            cell = row.CreateCell(1);
            cell.SetCellValue("成本中心 ：");
            cell.CellStyle = style14;

            cell = row.CreateCell(2);
            cell.SetCellValue("");
            cell.CellStyle = buttomLine14;

            cell = row.CreateCell(3);
            cell.SetCellValue("行動電話 ：");
            cell.CellStyle = style14;

            cell = row.CreateCell(4);
            cell.SetCellValue("");
            cell.CellStyle = buttomLine14;

            row = sheet.CreateRow(7);               //第7行
            cell = row.CreateCell(1);
            cell.SetCellValue("領取方式 ：");
            cell.CellStyle = style14;

            cell = row.CreateCell(2);
            region = new CellRangeAddress(7, 7, 2, 3);
            sheet.AddMergedRegion(region);
            cell.SetCellValue("□ 親自領取        □ 郵寄領取");
            cell.CellStyle = style14;

            row = sheet.CreateRow(9);                 //第9行
            region = new CellRangeAddress(9, 9, 1, 2);
            sheet.AddMergedRegion(region);
            cell = row.CreateCell(1);
            cell.SetCellValue("統一發票開立基本資料");
            cell.CellStyle = style14;

            row = sheet.CreateRow(10);                       //第10行
            cell = row.CreateCell(1);
            cell.SetCellValue("開立方式：");
            cell.CellStyle = style14;

            region = new CellRangeAddress(10, 10, 2, 6);
            sheet.AddMergedRegion(region);
            cell = row.CreateCell(2);
            cell.SetCellValue("□一次性開立      □ 週期性開立  (自          年         月至         年         月)，請檢附合約影本。");
            cell.CellStyle = style14;

            row = sheet.CreateRow(12);                       //第12行
            cell = row.CreateCell(1);
            cell.SetCellValue("買  受  人 ：");
            cell.CellStyle = style14;

            region = new CellRangeAddress(12, 12, 2, 3);
            sheet.AddMergedRegion(region);
            cell = row.CreateCell(2);
            cell.SetCellValue(bill.Result.TPCInvoiceBuyer);
            cell.CellStyle = buttomLineBlue14;

            row = sheet.CreateRow(13);                       //第13行
            cell = row.CreateCell(1);
            cell.SetCellValue("統一編號 ：");
            cell.CellStyle = style14;

            cell = row.CreateCell(2);
            cell.SetCellValue(bill.Result.GUINumber);
            cell.CellStyle = buttomLineBlue14;


            row = sheet.CreateRow(14);                       //第14行
            cell = row.CreateCell(1);
            cell.SetCellValue("地　　址：");
            cell.CellStyle = style14;

            region = new CellRangeAddress(14, 14, 2, 4);
            sheet.AddMergedRegion(region);
            cell = row.CreateCell(2);
            cell.SetCellValue(bill.Result.TPCInvoiceAddress);
            cell.CellStyle = buttomLineBlue14;

            row = sheet.CreateRow(16);                        //第16行
            region = new CellRangeAddress(16, 16, 1, 2);
            sheet.AddMergedRegion(region);
            cell = row.CreateCell(1);
            cell.SetCellValue("統一發票開立內容：");
            cell.CellStyle = style14;

            row = sheet.CreateRow(17);                        //第17行
            cell = row.CreateCell(1);
            cell.SetCellValue("項次");
            cell.CellStyle = style14upleft;

            cell = row.CreateCell(2);
            cell.SetCellValue("品名");
            cell.CellStyle = style14up;

            cell = row.CreateCell(3);
            cell.SetCellValue("數量");
            cell.CellStyle = style14up;

            cell = row.CreateCell(4);
            cell.SetCellValue("發票開立金額(含稅)");
            cell.CellStyle = style14up;

            cell = row.CreateCell(5);
            region = new CellRangeAddress(17, 17, 5, 6);
            sheet.AddMergedRegion(region);
            cell.SetCellValue("備註(Memo)");
            cell.CellStyle = style14up;

            cell = row.CreateCell(6);
            cell.CellStyle = style14upright;

            row = sheet.CreateRow(18);                       //第18行
            cell = row.CreateCell(1);
            cell.SetCellValue(1);
            cell.CellStyle = style14bodyleft;

            cell = row.CreateCell(2);
            cell.SetCellValue("太陽光電售電");
            cell.CellStyle = style14body;

            cell = row.CreateCell(3);
            cell.SetCellValue(1);
            cell.CellStyle = style14body;

            cell = row.CreateCell(4);
            cell.SetCellValue(bill.Result.Money);

            ICellStyle style0 = workbook.CreateCellStyle();
            IDataFormat dataformat = workbook.CreateDataFormat();
            style0.DataFormat = dataformat.GetFormat("$#,##0");
            style0.SetFont(font14NoBold);
            style0.Alignment = HorizontalAlignment.Center;
            style0.VerticalAlignment = VerticalAlignment.Center;
            style0.BorderTop = BorderStyle.Thin;
            style0.BorderLeft = BorderStyle.Thin;
            style0.BorderRight = BorderStyle.Thin;
            style0.BorderBottom = BorderStyle.Thin;
            cell.CellStyle = style0;

            cell = row.CreateCell(5);
            cell.SetCellValue(bill.Result.TPCMeterNumber);
            region = new CellRangeAddress(18, 18, 5, 6);
            sheet.AddMergedRegion(region);
            cell.CellStyle = style14body;

            cell = row.CreateCell(6);
            cell.CellStyle = style14bodyright;

            
            for(var index = 19;index < 26;index++)
            {
                row = sheet.CreateRow(index);                       //第19行
                cell = row.CreateCell(1);
                if(index != 25){
                    cell.SetCellValue(index - 17);
                }
                else{
                    cell.SetCellValue("合計");
                }
                cell.CellStyle = style14bodyleft;

                cell = row.CreateCell(2);
                cell.CellStyle = style14body;

                cell = row.CreateCell(3);
                cell.CellStyle = style14body;

                cell = row.CreateCell(4);
                if (index == 25){
                    cell.CellStyle = style0;
                    cell.SetCellValue(Math.Round(bill.Result.Money,2));
                }
                else{
                    cell.CellStyle = style14body;
                }
                

                cell = row.CreateCell(5);
                region = new CellRangeAddress(index, index, 5, 6);
                sheet.AddMergedRegion(region);
                cell.CellStyle = style14body;

                cell = row.CreateCell(6);
                cell.CellStyle = style14bodyright;
            }         //第19-25行

            row = sheet.CreateRow(26);                        //第26行
            region = new CellRangeAddress(26, 26, 1, 6);
            sheet.AddMergedRegion(region);
            cell = row.CreateCell(1);
            cell.SetCellValue("說明(請略述發票開立性質及金額計算)：");
            cell.CellStyle = style14bodyleftnoborder;

            cell = row.CreateCell(6);
            cell.CellStyle = style14bodyrightnoborder;


            row = sheet.CreateRow(27);                        //第27行
            row.Height = 40 * 15;
            region = new CellRangeAddress(27, 29, 1, 6);
            sheet.AddMergedRegion(region);
            cell = row.CreateCell(1);
            cell.SetCellValue($"{bill.Result.PowerName}-太陽光電售電收入 \r計算期間:{bill.Result.StartAt}-{bill.Result.EndAt} \r售電收入 = ( 度數 {bill.Result.Kwh} * 費率{bill.Result.PowerRate}) * 1.05 = {Math.Round(bill.Result.Kwh * bill.Result.PowerRate * 1.05,2) } 元");
            cell.CellStyle = style14bodyleftnoborder;
            cell = row.CreateCell(6);
            cell.CellStyle = style14bodyrightnoborder;

            row = sheet.CreateRow(28);                        //第28行
            row.Height = 40 * 15;
            cell = row.CreateCell(1);
            cell.CellStyle = style14bodyleftnoborder;
            cell = row.CreateCell(6);
            cell.CellStyle = style14bodyrightnoborder;

            row = sheet.CreateRow(29);                        //第29行
            row.Height = 40 * 15;
            cell = row.CreateCell(1);
            cell.CellStyle = style14bodyleftnoborder;
            cell = row.CreateCell(6);
            cell.CellStyle = style14bodyrightnoborder;

            row = sheet.CreateRow(30);                        //第30行
            cell = row.CreateCell(1);
            cell.CellStyle = style14bodyleftnoborder;
            cell = row.CreateCell(6);
            cell.CellStyle = style14bodyrightnoborder;

            row = sheet.CreateRow(31);                        //第31行
            cell = row.CreateCell(1);
            cell.SetCellValue("入帳會計科目:");
            cell.CellStyle = style14bodyleftnoborder;

            cell = row.CreateCell(2);
            cell.SetCellValue("售電收入");
            cell.CellStyle = style14;

            cell = row.CreateCell(6);
            cell.CellStyle = style14bodyrightnoborder;

            row = sheet.CreateRow(32);                        //第32行
            cell = row.CreateCell(1);
            cell.SetCellValue("入帳成本中心:");
            cell.CellStyle = style14bodyleftnoborder;

            cell = row.CreateCell(6);
            cell.CellStyle = style14bodyrightnoborder;

            row = sheet.CreateRow(33);                        //第32行
            cell = row.CreateCell(1);
            cell.CellStyle = style14bodyleftnoborder;

            cell = row.CreateCell(6);
            cell.CellStyle = style14bodyrightnoborder;

            row = sheet.CreateRow(34);                        //第34行
            cell = row.CreateCell(1);
            cell.SetCellValue("備註：");
            cell.CellStyle = style14bodyleftnoborder;

            cell = row.CreateCell(6);
            cell.CellStyle = style14bodyrightnoborder;

            row = sheet.CreateRow(35);                        //第35行
            cell = row.CreateCell(1);
            region = new CellRangeAddress(35, 35, 1, 6);
            sheet.AddMergedRegion(region);
            cell.SetCellValue(" 1.請申請人提醒客戶付款期限為收到發票後30天內，以匯款方式給付於旭天能源指定之帳戶。");
            cell.CellStyle = style14bodyleftnoborder;

            cell = row.CreateCell(6);
            cell.CellStyle = style14bodyrightnoborder;

            row = sheet.CreateRow(36);                        //第36行
            cell = row.CreateCell(1);
            region = new CellRangeAddress(36, 36, 1, 6);
            sheet.AddMergedRegion(region);
            cell.SetCellValue(" 2.申請人應於收到客戶匯款時，填具收入繳存單交予出納，並請於收入繳存單上註明發票號碼。");
            cell.CellStyle = style14bodyleftnoborder;

            cell = row.CreateCell(6);
            cell.CellStyle = style14bodyrightnoborder;

            row = sheet.CreateRow(37);                        //第37行
            cell = row.CreateCell(1);
            region = new CellRangeAddress(37, 37, 1, 6);
            sheet.AddMergedRegion(region);
            cell.SetCellValue(" 3. 發票如需作廢或更改者，應於次月五日前通知會計部，逾期恕不受理。");
            cell.CellStyle = style14bodyleftnoborder;

            cell = row.CreateCell(6);
            cell.CellStyle = style14bodyrightnoborder;

            row = sheet.CreateRow(38);                        //第38行
            cell = row.CreateCell(1);
            region = new CellRangeAddress(38, 38, 1, 6);
            sheet.AddMergedRegion(region);
            cell.SetCellValue(" 4. 發票內容確認無誤後，請於發票領取人簽收欄內簽名。");
            cell.CellStyle = style14bodyleftnoborder;

            cell = row.CreateCell(6);
            cell.CellStyle = style14bodyrightnoborder;

            row = sheet.CreateRow(39);                        //第39行
            cell = row.CreateCell(1);
            region = new CellRangeAddress(39, 39, 1, 6);
            sheet.AddMergedRegion(region);
            cell.SetCellValue(" 5. 申請人填具申請表單，至少須經L3主管（含）或以上之主管核准。");
            cell.CellStyle = style14bodyleftnoborder;

            cell = row.CreateCell(6);
            cell.CellStyle = style14bodyrightnoborder;

            row = sheet.CreateRow(40);                        //第39行
            cell = row.CreateCell(1);
            region = new CellRangeAddress(40, 40, 1, 6);
            sheet.AddMergedRegion(region);
            cell.SetCellValue(" 6. 若對上述內容有任何問題，請電會計部-旭天:Yuchi Lin #15220");
            cell.CellStyle = style14bodyleftrigrtbottom;

            for(int a = 2; a < 7; a++)
            {
                cell = row.CreateCell(a);
                cell.CellStyle = style14bodyleftrigrtbottom;
            }

            row = sheet.CreateRow(43);           //第5行
            cell = row.CreateCell(1);
            cell.SetCellValue("申  請  人 ：");
            cell.CellStyle = style14;

            cell = row.CreateCell(2);
            cell.CellStyle = buttomLine14;

            cell = row.CreateCell(3);
            cell.SetCellValue("部門主管：    ");
            cell.CellStyle = style14;

            cell = row.CreateCell(4);
            cell.CellStyle = buttomLine14;

            row = sheet.CreateRow(45);           //第5行
            cell = row.CreateCell(5);
            cell.SetCellValue("發票領取人簽收:");
            cell.CellStyle = style14;

            cell = row.CreateCell(6);
            cell.CellStyle = buttomLine14;

            var ms = new NpoiMemoryStream
            {
                AllowClose = false
            };
            workbook.Write(ms);
            ms.Flush();
            ms.Seek(0, SeekOrigin.Begin);
            return File(ms, "application/vnd.ms-excel", "台電躉售發票" + ".xlsx");
        }

        
    }
}
