using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace FEQ0000
{
    public static class UTIL_EQ0000
    {
        public static string getmaxstr(string src, int len = 30)
        {
            if (src.Length > len) return src.Substring(0, len) + "...";
            else return src;
        }

        public static string MakeDataTableToHTML(List<dsPurchase.EETGW_PurchaseEBRow> rows, string savefilenamexls = "")
        {

            libxl.Book book = null;// = new libxl.BinBook();
            libxl.Sheet sheet = null;


            libxl.Format fTitle = null;
            libxl.Format fDataStr = null;
            libxl.Format fDataNum = null;

            if (savefilenamexls.isEmpty() == false)
            {
                book = new libxl.XmlBook();
                book.setKey(FCOMMON.info.libxlCompany, FCOMMON.info.libxlKey);
                sheet = book.addSheet("Data");

                fTitle = book.addFormat();
                fDataStr = book.addFormat();
                fDataNum = book.addFormat();

                fDataStr.alignH = libxl.AlignH.ALIGNH_CENTER;
                fDataStr.alignV = libxl.AlignV.ALIGNV_CENTER;
                fDataStr.setBorder(libxl.BorderStyle.BORDERSTYLE_THIN);
                fDataStr.fillPattern = libxl.FillPattern.FILLPATTERN_SOLID;
                fDataStr.patternForegroundColor = libxl.Color.COLOR_WHITE;

                fTitle.alignV = libxl.AlignV.ALIGNV_CENTER;
                fTitle.alignH = libxl.AlignH.ALIGNH_CENTER;
                fTitle.setBorder(libxl.BorderStyle.BORDERSTYLE_THIN);
                fTitle.fillPattern = libxl.FillPattern.FILLPATTERN_SOLID;
                fTitle.patternForegroundColor = libxl.Color.COLOR_GRAY25;

                fDataNum.alignH = libxl.AlignH.ALIGNH_GENERAL;
                fDataNum.alignV = libxl.AlignV.ALIGNV_CENTER;
                fDataNum.setBorder(libxl.BorderStyle.BORDERSTYLE_THIN);
                fDataNum.fillPattern = libxl.FillPattern.FILLPATTERN_SOLID;
                fDataNum.patternForegroundColor = libxl.Color.COLOR_WHITE;

            }

            //목록을 본문에 추가한다
            System.Text.StringBuilder sb = new StringBuilder();
            sb.AppendLine("<table border='1' cellspacing='0' cellpadding='1'>");
            sb.AppendLine("<tr>");
            sb.AppendLine("<td style='border:none;text-align:center;'>&nbsp;</td>");
            sb.AppendLine("<td style='border:none;text-align:center;'>&nbsp;</td>");
            sb.AppendLine("<td style='border:none;text-align:center;'>&nbsp;</td>");
            sb.AppendLine("<td style='border:none;text-align:center;'>&nbsp;</td>");
            sb.AppendLine("<td style='border:none;text-align:center;'>&nbsp;</td>");
            sb.AppendLine("<td style='border:none;text-align:center;'>&nbsp;</td>");
            sb.AppendLine("<td style='border:none;text-align:center;'>&nbsp;</td>");
            sb.AppendLine("<td style='text-align:center;'>TOTAL</td>");
            sb.AppendLine("<td style='text-align:center;' colspan='2'><strong>{금액}</strong></td>");
            sb.AppendLine("<td style='border:none;text-align:center;'>&nbsp;</td>");
            sb.AppendLine("<td style='border:none;text-align:center;'>&nbsp;</td>");
            sb.AppendLine("</tr>");
            sb.AppendLine("<tr>");
            sb.AppendLine("<td style='text-align:center;background-color: aqua;'>No</td>");
            sb.AppendLine("<td style='text-align:center;background-color: aqua;'>SC</td>");
            sb.AppendLine("<td style='text-align:center;background-color: aqua;'>자재번호</td>");
            sb.AppendLine("<td style='text-align:center;background-color: aqua;'>청구자</td>");
            sb.AppendLine("<td style='text-align:center;background-color: aqua;'>품목</td>");
            sb.AppendLine("<td style='text-align:center;background-color: aqua;'>규격</td>");
            sb.AppendLine("<td style='text-align:center;background-color: aqua;'>수량</td>");
            sb.AppendLine("<td style='text-align:center;background-color: aqua;'>단가</td>");
            sb.AppendLine("<td style='text-align:center;background-color: aqua;'>금액</td>");
            sb.AppendLine("<td style='text-align:center;background-color: aqua;'>공급업체</td>");
            sb.AppendLine("<td style='text-align:center;background-color: aqua;'>용도</td>");
            sb.AppendLine("<td style='text-align:center;background-color: aqua;'>요청부서</td>");
            sb.AppendLine("</tr>");
            sb.AppendLine("<tbody>");

            var row = 0;
            var col = 0;
            if (sheet != null)
            {
                var basewidth = 15;
                sheet.setRow(row, 30);
                sheet.setCol(col, basewidth); sheet.writeStr(row, col++, "No", fTitle);
                sheet.setCol(col, basewidth); sheet.writeStr(row, col++, "S/C", fTitle);
                sheet.setCol(col, basewidth); sheet.writeStr(row, col++, "자재번호", fTitle);
                sheet.setCol(col, basewidth); sheet.writeStr(row, col++, "청구자", fTitle);
                sheet.setCol(col, (int)(basewidth * 2)); sheet.writeStr(row, col++, "품목", fTitle);
                sheet.setCol(col, (int)(basewidth * 3)); sheet.writeStr(row, col++, "규격", fTitle);
                sheet.setCol(col, (int)(basewidth * 0.5)); sheet.writeStr(row, col++, "수량", fTitle);
                sheet.setCol(col, (int)(basewidth * 0.5)); sheet.writeStr(row, col++, "단가", fTitle);
                sheet.setCol(col, basewidth); sheet.writeStr(row, col++, "금액", fTitle);
                sheet.setCol(col, basewidth); sheet.writeStr(row, col++, "공급업체", fTitle);
                sheet.setCol(col, basewidth); sheet.writeStr(row, col++, "용도", fTitle);
                sheet.setCol(col, basewidth); sheet.writeStr(row, col++, "요청부서", fTitle);

            }


            decimal totalamt = 0;
            row = 1;
            col = 0;
            foreach (dsPurchase.EETGW_PurchaseEBRow dr in rows)
            {
                var s요청 = dr.IsrequestNameNull() ? "" : dr.requestName;//  fpSpread1_Sheet1.Cells[i, col_요청].Value;
                var sSID = dr.IssidNull() ? "" : dr.sid;// fpSpread1_Sheet1.Cells[i, col_SID].Value;
                var s품명 = dr.IspumnameNull() ? "" : dr.pumname;// fpSpread1_Sheet1.Cells[i, col_품명].Value;
                var s규격 = dr.IspumunitNull() ? "" : dr.pumscale;// fpSpread1_Sheet1.Cells[i, col_규격].Value;
                var qty = dr.IspumqtyNull() ? 0 : dr.pumqty;// fpSpread1_Sheet1.Cells[i, col_수량].Value;
                var price = dr.IspumpriceNull() ? 0 : dr.pumprice;// fpSpread1_Sheet1.Cells[i, col_단가].Value;
                var amt = dr.IspumamtNull() ? 0 : dr.pumamt;// fpSpread1_Sheet1.Cells[i, col_금액].Value;
                var s공급 = dr.IssupplyNull() ? "" : dr.supply;// fpSpread1_Sheet1.Cells[i, col_공급].Value;
                var s비고 = dr.IsbigoNull() ? "" : dr.bigo;// fpSpread1_Sheet1.Cells[i, col_비고].Value;
                var sSITE = dr.IssiteNull() ? "" : dr.site;// fpSpread1_Sheet1.Cells[i, col_site].Value;
                var s분류 = dr.IsdeptNull() ? "" : dr.dept;
                var sSC = dr.IsscNull() ? "" : dr.sc;//


                totalamt += amt;

                sb.AppendLine("<tr>");
                sb.AppendLine($"<td>{row}</td>");
                sb.AppendLine("<td>" + getmaxstr(sSC) + "</td>");
                sb.AppendLine("<td>" + getmaxstr(sSID) + "</td>");
                sb.AppendLine("<td>" + getmaxstr(s요청) + "</td>");
                sb.AppendLine("<td>" + getmaxstr(s품명) + "</td>");
                sb.AppendLine("<td>" + getmaxstr(s규격) + "</td>");
                sb.AppendLine("<td style='text-align:center'>" + qty.ToString("N0") + "</td>");
                sb.AppendLine("<td style='text-align:right'>" + price.ToString("N0") + "</td>");
                sb.AppendLine("<td style='text-align:right'>" + amt.ToString("N0") + "</td>");
                sb.AppendLine("<td>" + getmaxstr(s공급) + "</td>");
                sb.AppendLine("<td>" + s비고 + "</td>");
                sb.AppendLine("<td>전자실</td>");

                if (sheet != null)
                {
                    col = 0;
                    sheet.setRow(row, 23);
                    sheet.writeNum(row, col++, row, fDataNum);
                    sheet.writeStr(row, col++, sSC, fDataStr);
                    sheet.writeStr(row, col++, sSID, fDataStr);
                    sheet.writeStr(row, col++, s요청, fDataStr);
                    sheet.writeStr(row, col++, s품명, fDataStr);
                    sheet.writeStr(row, col++, s규격, fDataStr);
                    sheet.writeNum(row, col++, qty, fDataNum);
                    sheet.writeNum(row, col++, (int)price, fDataNum);
                    sheet.writeNum(row, col++, (int)amt, fDataNum);
                    sheet.writeStr(row, col++, s공급, fDataStr);
                    sheet.writeStr(row, col++, s비고, fDataStr);
                    sheet.writeStr(row, col++, "전자실", fDataStr);
                    row += 1;
                }

                ////sb.AppendLine("<td>" + getmaxstr(sSTIE) + "</td>");
                //''sb.AppendLine("<td>" + getmaxstr(s분류) + "</td>");
                //sb.AppendLine("<td>" + getmaxstr(sCRCF) + "</td>");
                //sb.AppendLine("<td>" + getmaxstr(s수령) + "</td>");



                sb.AppendLine("</tr>");
            }

            sb.AppendLine("<tr>");
            sb.AppendLine("<td style='border:none;text-align:center;'>&nbsp;</td>");
            sb.AppendLine("<td style='border:none;text-align:center;'>&nbsp;</td>");
            sb.AppendLine("<td style='border:none;text-align:center;'>&nbsp;</td>");
            sb.AppendLine("<td style='border:none;text-align:center;'>&nbsp;</td>");
            sb.AppendLine("<td style='border:none;text-align:center;'>&nbsp;</td>");
            sb.AppendLine("<td style='border:none;text-align:center;'>&nbsp;</td>");
            sb.AppendLine("<td style='border:none;text-align:center;'>&nbsp;</td>");
            sb.AppendLine("<td style='text-align:center;'>TOTAL</td>");
            sb.AppendLine("<td style='text-align:center;' colspan='2'><strong>{금액}</strong></td>");
            sb.AppendLine("<td style='border:none;text-align:center;'>&nbsp;</td>");
            sb.AppendLine("</tr>");

            sb.AppendLine("</tbody>");
            sb.AppendLine("</table>");


            if (sheet != null)
            {
                col = 0;
                sheet.setRow(row, 30);
                sheet.writeStr(row, col++, "합계", fTitle);
                sheet.writeStr(row, col++, "", fTitle);
                sheet.writeStr(row, col++, "", fTitle);
                sheet.writeStr(row, col++, "", fTitle);
                sheet.writeStr(row, col++, "", fTitle);
                sheet.writeStr(row, col++, "", fTitle);
                sheet.writeStr(row, col++, "", fTitle);
                sheet.writeNum(row, col++, (int)totalamt, fTitle);
                sheet.setMerge(row, row, col - 1, col);
                sheet.writeStr(row, col++, "", fTitle);
                sheet.writeStr(row, col++, "", fTitle);
                sheet.writeStr(row, col++, "", fTitle);
                sheet.writeStr(row, col++, "", fTitle);
                row += 1;
            }


            if (book != null)
            {
                try
                {
                    book.save(savefilenamexls);
                }
                catch { }
            }
            return sb.ToString().Replace("{금액}", totalamt.ToString("N0"));
        }

        public static string MakeDataTableToHTML(List<dsPurchase.PurchaseRow> rows, string savefilenamexls = "")
        {

            libxl.Book book = null;// = new libxl.BinBook();
            libxl.Sheet sheet = null;


            libxl.Format fTitle = null;
            libxl.Format fDataStr = null;
            libxl.Format fDataNum = null;

            if (savefilenamexls.isEmpty() == false)
            {
                book = new libxl.XmlBook();
                book.setKey(FCOMMON.info.libxlCompany, FCOMMON.info.libxlKey);
                sheet = book.addSheet("Data");

                fTitle = book.addFormat();
                fDataStr = book.addFormat();
                fDataNum = book.addFormat();

                fDataStr.alignH = libxl.AlignH.ALIGNH_CENTER;
                fDataStr.alignV = libxl.AlignV.ALIGNV_CENTER;
                fDataStr.setBorder(libxl.BorderStyle.BORDERSTYLE_THIN);
                fDataStr.fillPattern = libxl.FillPattern.FILLPATTERN_SOLID;
                fDataStr.patternForegroundColor = libxl.Color.COLOR_WHITE;

                fTitle.alignV = libxl.AlignV.ALIGNV_CENTER;
                fTitle.alignH = libxl.AlignH.ALIGNH_CENTER;
                fTitle.setBorder(libxl.BorderStyle.BORDERSTYLE_THIN);
                fTitle.fillPattern = libxl.FillPattern.FILLPATTERN_SOLID;
                fTitle.patternForegroundColor = libxl.Color.COLOR_GRAY25;

                fDataNum.alignH = libxl.AlignH.ALIGNH_GENERAL;
                fDataNum.alignV = libxl.AlignV.ALIGNV_CENTER;
                fDataNum.setBorder(libxl.BorderStyle.BORDERSTYLE_THIN);
                fDataNum.fillPattern = libxl.FillPattern.FILLPATTERN_SOLID;
                fDataNum.patternForegroundColor = libxl.Color.COLOR_WHITE;

            }

            //목록을 본문에 추가한다
            System.Text.StringBuilder sb = new StringBuilder();
            sb.AppendLine("<table border='1' cellspacing='0' cellpadding='1'>");
            sb.AppendLine("<tr>");
            sb.AppendLine("<td style='border:none;text-align:center;'>&nbsp;</td>");
            sb.AppendLine("<td style='border:none;text-align:center;'>&nbsp;</td>");
            sb.AppendLine("<td style='border:none;text-align:center;'>&nbsp;</td>");
            sb.AppendLine("<td style='border:none;text-align:center;'>&nbsp;</td>");
            sb.AppendLine("<td style='border:none;text-align:center;'>&nbsp;</td>");
            sb.AppendLine("<td style='border:none;text-align:center;'>&nbsp;</td>");
            sb.AppendLine("<td style='border:none;text-align:center;'>&nbsp;</td>");
            sb.AppendLine("<td style='text-align:center;'>TOTAL</td>");
            sb.AppendLine("<td style='text-align:center;' colspan='2'><strong>{금액}</strong></td>");
            sb.AppendLine("<td style='border:none;text-align:center;'>&nbsp;</td>");
            sb.AppendLine("</tr>");
            sb.AppendLine("<tr>");
            sb.AppendLine("<td style='text-align:center;background-color: aqua;'>No</td>");
            sb.AppendLine("<td style='text-align:center;background-color: aqua;'>SC</td>");
            sb.AppendLine("<td style='text-align:center;background-color: aqua;'>자재번호</td>");
            sb.AppendLine("<td style='text-align:center;background-color: aqua;'>청구자</td>");
            sb.AppendLine("<td style='text-align:center;background-color: aqua;'>품목</td>");
            sb.AppendLine("<td style='text-align:center;background-color: aqua;'>규격</td>");
            sb.AppendLine("<td style='text-align:center;background-color: aqua;'>수량</td>");
            sb.AppendLine("<td style='text-align:center;background-color: aqua;'>단가</td>");
            sb.AppendLine("<td style='text-align:center;background-color: aqua;'>금액</td>");
            sb.AppendLine("<td style='text-align:center;background-color: aqua;'>공급업체</td>");
            sb.AppendLine("<td style='text-align:center;background-color: aqua;'>용도</td>");
            sb.AppendLine("</tr>");
            sb.AppendLine("<tbody>");

            var row = 0;
            var col = 0;
            if (sheet != null)
            {
                var basewidth = 15;
                sheet.setRow(row, 30);
                sheet.setCol(col, basewidth); sheet.writeStr(row, col++, "No", fTitle);
                sheet.setCol(col, basewidth); sheet.writeStr(row, col++, "S/C", fTitle);
                sheet.setCol(col, basewidth); sheet.writeStr(row, col++, "자재번호", fTitle);
                sheet.setCol(col, basewidth); sheet.writeStr(row, col++, "청구자", fTitle);
                sheet.setCol(col, (int)(basewidth * 2)); sheet.writeStr(row, col++, "품목", fTitle);
                sheet.setCol(col, (int)(basewidth * 3)); sheet.writeStr(row, col++, "규격", fTitle);
                sheet.setCol(col, (int)(basewidth * 0.5)); sheet.writeStr(row, col++, "수량", fTitle);
                sheet.setCol(col, (int)(basewidth * 0.5)); sheet.writeStr(row, col++, "단가", fTitle);
                sheet.setCol(col, basewidth); sheet.writeStr(row, col++, "금액", fTitle);
                sheet.setCol(col, basewidth); sheet.writeStr(row, col++, "공급업체", fTitle);
                sheet.setCol(col, basewidth); sheet.writeStr(row, col++, "용도", fTitle);

            }


            decimal totalamt = 0;
            row = 1;
            col = 0;
            foreach (dsPurchase.PurchaseRow dr in rows)
            {
                var s요청 = dr.IsrequestNameNull() ? "" : dr.requestName;//  fpSpread1_Sheet1.Cells[i, col_요청].Value;
                var sSID = dr.IssidNull() ? "" : dr.sid;// fpSpread1_Sheet1.Cells[i, col_SID].Value;
                var s품명 = dr.IspumnameNull() ? "" : dr.pumname;// fpSpread1_Sheet1.Cells[i, col_품명].Value;
                var s규격 = dr.IspumunitNull() ? "" : dr.pumscale;// fpSpread1_Sheet1.Cells[i, col_규격].Value;
                var qty = dr.IspumqtyNull() ? 0 : dr.pumqty;// fpSpread1_Sheet1.Cells[i, col_수량].Value;
                var price = dr.IspumpriceNull() ? 0 : dr.pumprice;// fpSpread1_Sheet1.Cells[i, col_단가].Value;
                var amt = dr.IspumamtNull() ? 0 : dr.pumamt;// fpSpread1_Sheet1.Cells[i, col_금액].Value;
                var s공급 = dr.IssupplyNull() ? "" : dr.supply;// fpSpread1_Sheet1.Cells[i, col_공급].Value;
                var s비고 = dr.IsbigoNull() ? "" : dr.bigo;// fpSpread1_Sheet1.Cells[i, col_비고].Value;
                //var sSITE = dr.IssiteNull() ? "" : dr.site;// fpSpread1_Sheet1.Cells[i, col_site].Value;
                var s분류 = dr.IsdeptNull() ? "" : dr.dept;
                var sSC = dr.IsscNull() ? "" : dr.sc;//


                totalamt += amt;

                sb.AppendLine("<tr>");
                sb.AppendLine($"<td>{row}</td>");
                sb.AppendLine("<td>" + getmaxstr(sSC) + "</td>");
                sb.AppendLine("<td>" + getmaxstr(sSID) + "</td>");
                sb.AppendLine("<td>" + getmaxstr(s요청) + "</td>");
                sb.AppendLine("<td>" + getmaxstr(s품명) + "</td>");
                sb.AppendLine("<td>" + getmaxstr(s규격) + "</td>");
                sb.AppendLine("<td style='text-align:center'>" + qty.ToString("N0") + "</td>");
                sb.AppendLine("<td style='text-align:right'>" + price.ToString("N0") + "</td>");
                sb.AppendLine("<td style='text-align:right'>" + amt.ToString("N0") + "</td>");
                sb.AppendLine("<td>" + getmaxstr(s공급) + "</td>");
                sb.AppendLine("<td>" + s비고 + "</td>");

                if (sheet != null)
                {
                    col = 0;
                    sheet.setRow(row, 23);
                    sheet.writeNum(row, col++, row, fDataNum);
                    sheet.writeStr(row, col++, sSC, fDataStr);
                    sheet.writeStr(row, col++, sSID, fDataStr);
                    sheet.writeStr(row, col++, s요청, fDataStr);
                    sheet.writeStr(row, col++, s품명, fDataStr);
                    sheet.writeStr(row, col++, s규격, fDataStr);
                    sheet.writeNum(row, col++, qty, fDataNum);
                    sheet.writeNum(row, col++, (int)price, fDataNum);
                    sheet.writeNum(row, col++, (int)amt, fDataNum);
                    sheet.writeStr(row, col++, s공급, fDataStr);
                    sheet.writeStr(row, col++, s비고, fDataStr);
                    row += 1;
                }

                ////sb.AppendLine("<td>" + getmaxstr(sSTIE) + "</td>");
                //''sb.AppendLine("<td>" + getmaxstr(s분류) + "</td>");
                //sb.AppendLine("<td>" + getmaxstr(sCRCF) + "</td>");
                //sb.AppendLine("<td>" + getmaxstr(s수령) + "</td>");



                sb.AppendLine("</tr>");
            }

            sb.AppendLine("<tr>");
            sb.AppendLine("<td style='border:none;text-align:center;'>&nbsp;</td>");
            sb.AppendLine("<td style='border:none;text-align:center;'>&nbsp;</td>");
            sb.AppendLine("<td style='border:none;text-align:center;'>&nbsp;</td>");
            sb.AppendLine("<td style='border:none;text-align:center;'>&nbsp;</td>");
            sb.AppendLine("<td style='border:none;text-align:center;'>&nbsp;</td>");
            sb.AppendLine("<td style='border:none;text-align:center;'>&nbsp;</td>");
            sb.AppendLine("<td style='border:none;text-align:center;'>&nbsp;</td>");
            sb.AppendLine("<td style='text-align:center;'>TOTAL</td>");
            sb.AppendLine("<td style='text-align:center;' colspan='2'><strong>{금액}</strong></td>");
            sb.AppendLine("</tr>");

            sb.AppendLine("</tbody>");
            sb.AppendLine("</table>");


            if (sheet != null)
            {
                col = 0;
                sheet.setRow(row, 30);
                sheet.writeStr(row, col++, "합계", fTitle);
                sheet.writeStr(row, col++, "", fTitle);
                sheet.writeStr(row, col++, "", fTitle);
                sheet.writeStr(row, col++, "", fTitle);
                sheet.writeStr(row, col++, "", fTitle);
                sheet.writeStr(row, col++, "", fTitle);
                sheet.writeStr(row, col++, "", fTitle);
                sheet.writeNum(row, col++, (int)totalamt, fTitle);
                sheet.setMerge(row, row, col - 1, col);
                sheet.writeStr(row, col++, "", fTitle);
                sheet.writeStr(row, col++, "", fTitle);
                sheet.writeStr(row, col++, "", fTitle);
                row += 1;
            }


            if (book != null)
            {
                try
                {
                    book.save(savefilenamexls);
                }
                catch { }
            }
            return sb.ToString().Replace("{금액}", totalamt.ToString("N0"));
        }

        public static string MakeDataTableToHTML(List<dsPurchase.EETGW_PurchaseCRRow> rows, string savefilenamexls = "")
        {

            libxl.Book book = null;// = new libxl.BinBook();
            libxl.Sheet sheet = null;


            libxl.Format fTitle = null;
            libxl.Format fDataStr = null;
            libxl.Format fDataNum = null;

            if (savefilenamexls.isEmpty() == false)
            {
                book = new libxl.XmlBook();
                book.setKey(FCOMMON.info.libxlCompany, FCOMMON.info.libxlKey);
                sheet = book.addSheet("Data");

                fTitle = book.addFormat();
                fDataStr = book.addFormat();
                fDataNum = book.addFormat();

                fDataStr.alignH = libxl.AlignH.ALIGNH_CENTER;
                fDataStr.alignV = libxl.AlignV.ALIGNV_CENTER;
                fDataStr.setBorder(libxl.BorderStyle.BORDERSTYLE_THIN);
                fDataStr.fillPattern = libxl.FillPattern.FILLPATTERN_SOLID;
                fDataStr.patternForegroundColor = libxl.Color.COLOR_WHITE;

                fTitle.alignV = libxl.AlignV.ALIGNV_CENTER;
                fTitle.alignH = libxl.AlignH.ALIGNH_CENTER;
                fTitle.setBorder(libxl.BorderStyle.BORDERSTYLE_THIN);
                fTitle.fillPattern = libxl.FillPattern.FILLPATTERN_SOLID;
                fTitle.patternForegroundColor = libxl.Color.COLOR_GRAY25;

                fDataNum.alignH = libxl.AlignH.ALIGNH_GENERAL;
                fDataNum.alignV = libxl.AlignV.ALIGNV_CENTER;
                fDataNum.setBorder(libxl.BorderStyle.BORDERSTYLE_THIN);
                fDataNum.fillPattern = libxl.FillPattern.FILLPATTERN_SOLID;
                fDataNum.patternForegroundColor = libxl.Color.COLOR_WHITE;

            }

            //목록을 본문에 추가한다
            System.Text.StringBuilder sb = new StringBuilder();
            sb.AppendLine("<table border='1' cellspacing='0' cellpadding='1'>");
            sb.AppendLine("<tr>");
            sb.AppendLine("<td style='border:none;text-align:center;'>&nbsp;</td>");
            sb.AppendLine("<td style='border:none;text-align:center;'>&nbsp;</td>");
            sb.AppendLine("<td style='border:none;text-align:center;'>&nbsp;</td>");
            sb.AppendLine("<td style='border:none;text-align:center;'>&nbsp;</td>");
            sb.AppendLine("<td style='border:none;text-align:center;'>&nbsp;</td>");
            sb.AppendLine("<td style='border:none;text-align:center;'>&nbsp;</td>");
            sb.AppendLine("<td style='text-align:center;'>TOTAL</td>");
            sb.AppendLine("<td style='text-align:center;' colspan='2'><strong>{금액}</strong></td>");
            sb.AppendLine("<td style='border:none;text-align:center;'>&nbsp;</td>");
            sb.AppendLine("</tr>");
            sb.AppendLine("<tr>");
            sb.AppendLine("<td style='text-align:center;background-color: aqua;'>SC</td>");
            sb.AppendLine("<td style='text-align:center;background-color: aqua;'>자재번호</td>");
            sb.AppendLine("<td style='text-align:center;background-color: aqua;'>청구자</td>");
            sb.AppendLine("<td style='text-align:center;background-color: aqua;'>품목</td>");
            sb.AppendLine("<td style='text-align:center;background-color: aqua;'>규격</td>");
            sb.AppendLine("<td style='text-align:center;background-color: aqua;'>수량</td>");
            sb.AppendLine("<td style='text-align:center;background-color: aqua;'>단가</td>");
            sb.AppendLine("<td style='text-align:center;background-color: aqua;'>금액</td>");
            sb.AppendLine("<td style='text-align:center;background-color: aqua;'>공급업체</td>");
            sb.AppendLine("<td style='text-align:center;background-color: aqua;'>용도</td>");
            sb.AppendLine("</tr>");
            sb.AppendLine("<tbody>");

            var row = 0;
            var col = 0;
            if (sheet != null)
            {
                var basewidth = 15;
                sheet.setRow(row, 30);
                sheet.setCol(col, basewidth); sheet.writeStr(row, col++, "S/C", fTitle);
                sheet.setCol(col, basewidth); sheet.writeStr(row, col++, "자재번호", fTitle);
                sheet.setCol(col, basewidth); sheet.writeStr(row, col++, "청구자", fTitle);
                sheet.setCol(col, (int)(basewidth * 2)); sheet.writeStr(row, col++, "품목", fTitle);
                sheet.setCol(col, (int)(basewidth * 3)); sheet.writeStr(row, col++, "규격", fTitle);
                sheet.setCol(col, (int)(basewidth * 0.5)); sheet.writeStr(row, col++, "수량", fTitle);
                sheet.setCol(col, (int)(basewidth * 0.5)); sheet.writeStr(row, col++, "단가", fTitle);
                sheet.setCol(col, basewidth); sheet.writeStr(row, col++, "금액", fTitle);
                sheet.setCol(col, basewidth); sheet.writeStr(row, col++, "공급업체", fTitle);
                sheet.setCol(col, basewidth); sheet.writeStr(row, col++, "용도", fTitle);

            }


            decimal totalamt = 0;
            row = 1;
            col = 0;
            foreach (dsPurchase.EETGW_PurchaseCRRow dr in rows)
            {
                var s요청 = dr.IsrequestNameNull() ? "" : dr.requestName;//  fpSpread1_Sheet1.Cells[i, col_요청].Value;
                var sSID = dr.IssidNull() ? "" : dr.sid;// fpSpread1_Sheet1.Cells[i, col_SID].Value;
                var s품명 = dr.IspumnameNull() ? "" : dr.pumname;// fpSpread1_Sheet1.Cells[i, col_품명].Value;
                var s규격 = dr.IspumunitNull() ? "" : dr.pumscale;// fpSpread1_Sheet1.Cells[i, col_규격].Value;
                var qty = dr.IspumqtyNull() ? 0 : dr.pumqty;// fpSpread1_Sheet1.Cells[i, col_수량].Value;
                var price = dr.IspumpriceNull() ? 0 : dr.pumprice;// fpSpread1_Sheet1.Cells[i, col_단가].Value;
                var amt = dr.IspumamtNull() ? 0 : dr.pumamt;// fpSpread1_Sheet1.Cells[i, col_금액].Value;
                var s공급 = dr.IssupplyNull() ? "" : dr.supply;// fpSpread1_Sheet1.Cells[i, col_공급].Value;
                var s비고 = dr.IsbigoNull() ? "" : dr.bigo;// fpSpread1_Sheet1.Cells[i, col_비고].Value;
                //var sSITE = dr.IssiteNull() ? "" : dr.site;// fpSpread1_Sheet1.Cells[i, col_site].Value;
                var s분류 = dr.IsdeptNull() ? "" : dr.dept;
                var sSC = dr.IsscNull() ? "" : dr.sc;//


                totalamt += amt;

                sb.AppendLine("<tr>");
                sb.AppendLine("<td>" + getmaxstr(sSC) + "</td>");
                sb.AppendLine("<td>" + getmaxstr(sSID) + "</td>");
                sb.AppendLine("<td>" + getmaxstr(s요청) + "</td>");
                sb.AppendLine("<td>" + getmaxstr(s품명) + "</td>");
                sb.AppendLine("<td>" + getmaxstr(s규격) + "</td>");
                sb.AppendLine("<td style='text-align:center'>" + qty.ToString("N0") + "</td>");
                sb.AppendLine("<td style='text-align:right'>" + price.ToString("N0") + "</td>");
                sb.AppendLine("<td style='text-align:right'>" + amt.ToString("N0") + "</td>");
                sb.AppendLine("<td>" + getmaxstr(s공급) + "</td>");
                sb.AppendLine("<td>" + s비고 + "</td>");

                if (sheet != null)
                {
                    col = 0;
                    sheet.setRow(row, 23);
                    sheet.writeStr(row, col++, sSC, fDataStr);
                    sheet.writeStr(row, col++, sSID, fDataStr);
                    sheet.writeStr(row, col++, s요청, fDataStr);
                    sheet.writeStr(row, col++, s품명, fDataStr);
                    sheet.writeStr(row, col++, s규격, fDataStr);
                    sheet.writeNum(row, col++, qty, fDataNum);
                    sheet.writeNum(row, col++, (int)price, fDataNum);
                    sheet.writeNum(row, col++, (int)amt, fDataNum);
                    sheet.writeStr(row, col++, s공급, fDataStr);
                    sheet.writeStr(row, col++, s비고, fDataStr);
                    row += 1;
                }

                ////sb.AppendLine("<td>" + getmaxstr(sSTIE) + "</td>");
                //''sb.AppendLine("<td>" + getmaxstr(s분류) + "</td>");
                //sb.AppendLine("<td>" + getmaxstr(sCRCF) + "</td>");
                //sb.AppendLine("<td>" + getmaxstr(s수령) + "</td>");



                sb.AppendLine("</tr>");
            }

            sb.AppendLine("<tr>");
            sb.AppendLine("<td style='border:none;text-align:center;'>&nbsp;</td>");
            sb.AppendLine("<td style='border:none;text-align:center;'>&nbsp;</td>");
            sb.AppendLine("<td style='border:none;text-align:center;'>&nbsp;</td>");
            sb.AppendLine("<td style='border:none;text-align:center;'>&nbsp;</td>");
            sb.AppendLine("<td style='border:none;text-align:center;'>&nbsp;</td>");
            sb.AppendLine("<td style='border:none;text-align:center;'>&nbsp;</td>");
            sb.AppendLine("<td style='text-align:center;'>TOTAL</td>");
            sb.AppendLine("<td style='text-align:center;' colspan='2'><strong>{금액}</strong></td>");
            sb.AppendLine("</tr>");

            sb.AppendLine("</tbody>");
            sb.AppendLine("</table>");


            if (sheet != null)
            {
                col = 0;
                sheet.setRow(row, 30);
                sheet.writeStr(row, col++, "합계", fTitle);
                sheet.writeStr(row, col++, "", fTitle);
                sheet.writeStr(row, col++, "", fTitle);
                sheet.writeStr(row, col++, "", fTitle);
                sheet.writeStr(row, col++, "", fTitle);
                sheet.writeStr(row, col++, "", fTitle);
                sheet.writeNum(row, col++, (int)totalamt, fTitle);
                sheet.setMerge(row, row, col - 1, col);
                sheet.writeStr(row, col++, "", fTitle);
                sheet.writeStr(row, col++, "", fTitle);
                sheet.writeStr(row, col++, "", fTitle);
                row += 1;
            }


            if (book != null)
            {
                try
                {
                    book.save(savefilenamexls);
                }
                catch { }
            }
            return sb.ToString().Replace("{금액}", totalamt.ToString("N0"));
        }
    }
    public static class DatabaseManager
    {
        private static System.Data.SqlClient.SqlConnection getCn()
        {
            string cs = Properties.Settings.Default.gwcs;
            System.Data.SqlClient.SqlConnection cn = new System.Data.SqlClient.SqlConnection();
            cn.ConnectionString = cs;
            return cn;
        }
        public static List<String> getEQGroupLiist(string GroupColumn, string table)
        {
            List<string> retval = new List<string>();
            var cn = getCn();
            cn.Open();
            var sql = "select {0} from {1} where gcode='" + FCOMMON.info.Login.gcode + "' and isnull({0},'') != '' group by {0} order by {0}";

            if (GroupColumn.IndexOf("+") == -1)
                sql = string.Format(sql, "[" + GroupColumn + "]", table);
            else
                sql = string.Format(sql, GroupColumn, table);


            var cmd = new System.Data.SqlClient.SqlCommand(sql, cn);
            var rdr = cmd.ExecuteReader();
            while (rdr.Read())
            {
                retval.Add(rdr[0].ToString());
            }
            cmd.Dispose();
            cn.Close();
            cn.Dispose();
            return retval;
        }
    }
}
