using CallCenter.Utility;
using CallCenterApi.DB;
using CallCenterApi.Interface.Controllers.Base;
using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
using NPOI.SS.Util;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;

namespace CallCenterApi.Interface.Controllers.report
{
    public class ExportQCResultController : BaseController
    {
        List<string> arlist = new List<string>();
        DataTable dtNew = new DataTable();
      
        public ActionResult Index()
        {
            return View();
        }
        ////前端的列表获取表头
        public ActionResult GetColumnList()
        {
            List<Model.QCModel> qclist = new List<Model.QCModel>();

            #region 加载固定项
            DataColumn dc1 = new DataColumn("QCName", Type.GetType("System.String"));
            DataColumn dc2 = new DataColumn("UserName", Type.GetType("System.String"));
            DataColumn dc3 = new DataColumn("UserCode", Type.GetType("System.String"));
            DataColumn dc4 = new DataColumn("ZJCount", Type.GetType("System.String"));
            DataColumn dc5 = new DataColumn("QCTime", Type.GetType("System.String"));

            dtNew.Columns.Add(dc1);
            dtNew.Columns.Add(dc2);
            dtNew.Columns.Add(dc3);
            dtNew.Columns.Add(dc4);
            dtNew.Columns.Add(dc5);


            Model.QCModel qcm1 = new Model.QCModel();
            qcm1.Qcid = "cpr1";
            qcm1.Qcname = "测评人";
            qcm1.Rowspan = "4";
            Model.QCModel qcm2 = new Model.QCModel();
            qcm2.Qcid = "xm2";
            qcm2.Qcname = "姓名";
            qcm2.Rowspan = "4";
            Model.QCModel qcm3 = new Model.QCModel();
            qcm3.Qcid = "gh3";
            qcm3.Qcname = "工号";
            qcm3.Rowspan = "4";
            Model.QCModel qcm4 = new Model.QCModel();
            qcm4.Qcid = "zjts4";
            qcm4.Qcname = "手机号";
            qcm4.Rowspan = "4";

            Model.QCModel qcm5 = new Model.QCModel();
            qcm5.Qcid = "qctime5";
            qcm5.Qcname = "质检时间";
            qcm5.Rowspan = "5";



            qclist.Add(qcm1);
            qclist.Add(qcm2);
            qclist.Add(qcm3);
            qclist.Add(qcm4);

            qclist.Add(qcm5);

            #endregion

            Model.QCModel qcmzf = new Model.QCModel();
            qcmzf.Qcid = "zf999"; 
            qcmzf.Qcname = "总分1";
            qcmzf.Rowspan = "3";

            object obj = null;

            DataSet ds = DbHelperSQL.Query("select F_CategoryId,F_CategoryName,F_ParentId from T_QC_IndexCategory where F_DeleteFlag=0 order by F_CategoryId asc");
            DataTable dsConten = DbHelperSQL.Query("select F_IndexId,F_Content,F_Title,F_Score,F_CategoryId from T_QC_IndexBase order by F_IndexId asc").Tables[0];
            if (ds != null && ds.Tables.Count > 0)
            {
                DataTable dt = ds.Tables[0];
                DataRow[] drlist = dt.Select("F_ParentId=0");
                if (drlist != null)
                {
                    double ctsc = 0;//总分
                    for (int i = 0; i < drlist.Length; i++)
                    {
                        obj = DbHelperSQL.GetSingle("select count(*) from T_QC_IndexBase where F_CategoryId in(select F_CategoryId from T_QC_IndexCategory where F_ParentId=" + drlist[i]["F_CategoryId"] + ")");

                        Model.QCModel qcml = new Model.QCModel();
                        qcml.Qcid = drlist[i]["F_CategoryId"].ToString();
                        qcml.Qcname = drlist[i]["F_CategoryName"].ToString();
                        qcml.Colspan = obj.ToString();
 

                        DataRow[] drlistnew = dt.Select("F_ParentId=" + drlist[i]["F_CategoryId"]);
                        if (drlistnew != null && drlist.Length > 0)
                        {
                            List<Model.QCModel> qcclist = new List<Model.QCModel>();
                            double sc = 0;
                            string strCatid = "";
                            for (int n = 0; n < drlistnew.Length; n++)
                            {
                                Model.QCModel qccml = new Model.QCModel();
                                obj = DbHelperSQL.GetSingle("select count(*) from T_QC_IndexBase where F_CategoryId=" + drlistnew[n]["F_CategoryId"]);
             
                                qccml.Qcid = drlist[i]["F_CategoryId"].ToString() + "-" + drlistnew[n]["F_CategoryId"].ToString();
                                qccml.Qcname = drlistnew[n]["F_CategoryName"].ToString();
                                qccml.Colspan = obj.ToString();

                                DataRow[] drContent = dsConten.Select("F_CategoryId=" + drlistnew[n]["F_CategoryId"]);
                                if (drContent != null && drContent.Length > 0)
                                {
                                    List<Model.QCModel> qccllist = new List<Model.QCModel>();
                                    for (int m = 0; m < drContent.Length; m++)
                                    {
                                        Model.QCModel qccmll = new Model.QCModel();
                                        qccmll.Qcid = drlist[i]["F_CategoryId"].ToString() + "-" + drlistnew[n]["F_CategoryId"].ToString() + "-" + drContent[m]["F_IndexId"].ToString();
                                        qccmll.Qcname = drContent[m]["F_Title"].ToString();
                                        qccmll.Qcscore = drContent[m]["F_Score"].ToString();
                      
                                        if (!string.IsNullOrEmpty(drContent[m]["F_Score"].ToString()))
                                        {
                                            sc += Double.Parse(drContent[m]["F_Score"].ToString());
                                            ctsc += Double.Parse(drContent[m]["F_Score"].ToString());
                                        }

                                        strCatid += drContent[m]["F_IndexId"] + ",";

                                        DataColumn dc = new DataColumn("Q" + drContent[m]["F_IndexId"].ToString(), Type.GetType("System.String"));
                                        dtNew.Columns.Add(dc);

                                        qccllist.Add(qccmll);
                                    }

                                    qccml.Qclist = qccllist;
                                }
                                qcclist.Add(qccml);
                            }

                            if (strCatid != "")
                            {
                                strCatid.Substring(0, strCatid.Length - 1);
                            }
                            arlist.Add(strCatid); //每一组的题id记下来，用在分数总和上。

                       


                            qcml.Qclist = qcclist;
                            qclist.Add(qcml);

                            Model.QCModel qcml1 = new Model.QCModel();
                            qcml1.Qcid = "fx" + drlist[i]["F_CategoryId"].ToString();
                            qcml1.Qcname = "总分";
                            qcml1.Rowspan = "3";
                            qcml1.Qcscore = sc.ToString();
                            qclist.Add(qcml1);
                            DataColumn dcdf = new DataColumn("df" + i.ToString(), Type.GetType("System.String"));
                            dtNew.Columns.Add(dcdf);
                        }

                    }

                    qcmzf.Qcscore = ctsc.ToString();
                }

            }

            qclist.Add(qcmzf);
            DataColumn dczf = new DataColumn("AllScore", Type.GetType("System.String"));
            dtNew.Columns.Add(dczf);
          


            return Success("获取质检表头成功", qclist);
        }

        /// <summary>
        /// 这个是我最新的导出数据20220601
        /// </summary>
        /// <param name="stime"></param>
        /// <param name="endtime"></param>
        /// <param name="dpt"></param>
        /// <returns></returns>
        public DataTable GetExportData(string stime, string endtime, string dpt)
        {
            DataTable dt = new DataTable();
            string sqltimeCallRecords = "";
            string strsql = "";
            if (stime != null && stime.Trim() != "")
            {
                sqltimeCallRecords += " and CONVERT(varchar , F_QCtime, 120)>=CONVERT(varchar , '" + stime.Trim() + " 00:00:01', 120) ";
            }
            if (endtime != null && endtime.Trim() != "")
            {
                sqltimeCallRecords += " and CONVERT(varchar , F_QCtime, 120)<=CONVERT(varchar , '" + endtime.Trim() + " 23:59:59', 120) ";
            }
            if (dpt != null && dpt.ToString() != "")
            {
                strsql = " and ExtNumber in(  select  extNubmer from  extUserCodeContrast where deptId= " + dpt.ToString() + ") ";

                //strsql = " and UserCode in(select F_UserCode from T_Sys_UserAccount where F_DeptId=" + dpt.ToString() + ") ";
            }
       
            DataTable dtcallrecorddata = DbHelperSQL.Query("select  F_QCUserName,UserCode,dbo.GetUserName(UserCode) name ,CallRecordsId,CallNumber,F_QCtime from T_Call_CallRecords where F_QCState=1  " + strsql+sqltimeCallRecords + "order by F_QCTime desc").Tables[0]; //and UserCode != 0

            foreach (DataRow dr in dtcallrecorddata.Rows)
            {
                DataRow drNew = dtNew.NewRow();

                DataTable dtqcres = DbHelperSQL.Query("  select* from T_QC_IndexRes where CallRecordsId = '"+dr["CallRecordsId"].ToString() +"'order by F_IndexId").Tables[0];

                drNew["QCName"] = dr["F_QCUserName"].ToString();
                drNew["UserName"] = dr["name"].ToString();
                drNew["UserCode"] = dr["UserCode"].ToString();
                              
                //质检条数改成手机号
                drNew["ZJCount"] = dr["CallNumber"].ToString();

                drNew["QCTime"] = dr["F_QCtime"].ToString();

                double itemfs = 0;//每列分数和
                for (int i = 0; i < arlist.Count; i++)
                {
                    string strList = arlist[i];
                    string[] strTitleList = strList.Split(',');
                    foreach (string str in strTitleList)
                    {
                        if (str != "")
                        {
                            double fs = 0;//每一项分数
                            object  obj = DbHelperSQL.GetSingle("select F_QCScore from T_QC_IndexRes where    F_IndexId=" + str +"and CallRecordsId="+ dr["CallRecordsId"].ToString());
                            if (obj != null && obj.ToString() != "")
                            {
                                    fs = double.Parse(obj.ToString()) ;
                            }
                            string mm = "";
                            if (fs.ToString().IndexOf('.') > -1 && fs.ToString().Substring(fs.ToString().IndexOf('.') + 1).Length > 2)
                            {
                                mm = fs.ToString("0.00");
                            }
                            else
                            {
                                mm = fs.ToString();
                            }
                            drNew["Q" + str] = mm;
                            itemfs += fs;
                        }
                    }
                    string imm = "";
                    if (itemfs.ToString().IndexOf('.') > -1 && itemfs.ToString().Substring(itemfs.ToString().IndexOf('.') + 1).Length > 2)
                    {
                        imm = itemfs.ToString("0.00");
                    }
                    else
                    {
                        imm = itemfs.ToString();
                    }
                    drNew["df" + i.ToString()] = imm;
                 //   ctfs += itemfs;
                    itemfs = 0;
                }

                dtNew.Rows.Add(drNew);
            }
            return dtNew;

        }


        public List<Model.QCModel> Getqulist()
        {
            List<Model.QCModel> qclist = new List<Model.QCModel>();

            #region 加载固定项
            DataColumn dc1 = new DataColumn("QCName", Type.GetType("System.String"));
            DataColumn dc2 = new DataColumn("UserName", Type.GetType("System.String"));
            DataColumn dc3 = new DataColumn("UserCode", Type.GetType("System.String"));
            DataColumn dc4 = new DataColumn("ZJCount", Type.GetType("System.String"));
            DataColumn dc5 = new DataColumn("QCTime", Type.GetType("System.String"));

            dtNew.Columns.Add(dc1);
            dtNew.Columns.Add(dc2);
            dtNew.Columns.Add(dc3);
            dtNew.Columns.Add(dc4);
            dtNew.Columns.Add(dc5);

            Model.QCModel qcm1 = new Model.QCModel();
            qcm1.Qcid = "cpr1";
            qcm1.Qcname = "测评人";
            qcm1.Rowspan = "4";
            Model.QCModel qcm2 = new Model.QCModel();
            qcm2.Qcid = "xm2";
            qcm2.Qcname = "姓名";
            qcm2.Rowspan = "4";
            Model.QCModel qcm3 = new Model.QCModel();
            qcm3.Qcid = "gh3";
            qcm3.Qcname = "工号";
            qcm3.Rowspan = "4";
            Model.QCModel qcm4 = new Model.QCModel();
            qcm4.Qcid = "zjts4";
            qcm4.Qcname = "质检条数";
            qcm4.Rowspan = "4";

            Model.QCModel qcm5 = new Model.QCModel();
            qcm5.Qcid = "qctime5";
            qcm5.Qcname = "质检时间";
            qcm5.Rowspan = "5";


            qclist.Add(qcm1);
            qclist.Add(qcm2);
            qclist.Add(qcm3);
            qclist.Add(qcm4);
            qclist.Add(qcm5);

            #endregion

            Model.QCModel qcmzf = new Model.QCModel();
            qcmzf.Qcid = "zf999";
            qcmzf.Qcname = "总分1";
            qcmzf.Rowspan = "3";

            object obj = null;

            DataSet ds = DbHelperSQL.Query("select F_CategoryId,F_CategoryName,F_ParentId from T_QC_IndexCategory where F_DeleteFlag=0 order by F_CategoryId asc");
            DataTable dsConten = DbHelperSQL.Query("select F_IndexId,F_Content,F_Title,F_Score,F_CategoryId from T_QC_IndexBase order by F_IndexId asc").Tables[0];
            if (ds != null && ds.Tables.Count > 0)
            {
                DataTable dt = ds.Tables[0];
                DataRow[] drlist = dt.Select("F_ParentId=0");
                if (drlist != null)
                {
                    double ctsc = 0;//总分
                    for (int i = 0; i < drlist.Length; i++)
                    {
                        obj = DbHelperSQL.GetSingle("select count(*) from T_QC_IndexBase where F_CategoryId in(select F_CategoryId from T_QC_IndexCategory where F_ParentId=" + drlist[i]["F_CategoryId"] + ")");

                        Model.QCModel qcml = new Model.QCModel();
                        qcml.Qcid = drlist[i]["F_CategoryId"].ToString();
                        qcml.Qcname = drlist[i]["F_CategoryName"].ToString();
                        qcml.Colspan = obj.ToString();
                        DataRow[] drlistnew = dt.Select("F_ParentId=" + drlist[i]["F_CategoryId"]);
                        if (drlistnew != null && drlist.Length > 0)
                        {
                            List<Model.QCModel> qcclist = new List<Model.QCModel>();
                            double sc = 0;
                            string strCatid = "";
                            for (int n = 0; n < drlistnew.Length; n++)
                            {
                                Model.QCModel qccml = new Model.QCModel();
                                obj = DbHelperSQL.GetSingle("select count(*) from T_QC_IndexBase where F_CategoryId=" + drlistnew[n]["F_CategoryId"]);

                                qccml.Qcid = drlist[i]["F_CategoryId"].ToString() + "-" + drlistnew[n]["F_CategoryId"].ToString();
                                qccml.Qcname = drlistnew[n]["F_CategoryName"].ToString();
                                qccml.Colspan = obj.ToString();

                                DataRow[] drContent = dsConten.Select("F_CategoryId=" + drlistnew[n]["F_CategoryId"]);
                                if (drContent != null && drContent.Length > 0)
                                {
                                    List<Model.QCModel> qccllist = new List<Model.QCModel>();
                                    for (int m = 0; m < drContent.Length; m++)
                                    {
                                        Model.QCModel qccmll = new Model.QCModel();
                                        qccmll.Qcid = drlist[i]["F_CategoryId"].ToString() + "-" + drlistnew[n]["F_CategoryId"].ToString() + "-" + drContent[m]["F_IndexId"].ToString();
                                        qccmll.Qcname = drContent[m]["F_Title"].ToString();
                                        qccmll.Qcscore = drContent[m]["F_Score"].ToString();
                                        if (!string.IsNullOrEmpty(drContent[m]["F_Score"].ToString()))
                                        {
                                            sc += double.Parse(drContent[m]["F_Score"].ToString());
                                        }

                                        strCatid += drContent[m]["F_IndexId"] + ",";

                                        DataColumn dc = new DataColumn("Q" + drContent[m]["F_IndexId"].ToString(), Type.GetType("System.String"));
                                        dtNew.Columns.Add(dc);

                                        qccllist.Add(qccmll);
                                    }

                                    qccml.Qclist = qccllist;
                                }
                                qcclist.Add(qccml);
                            }

                            if (strCatid != "")
                            {
                                strCatid.Substring(0, strCatid.Length - 1);
                            }
                            arlist.Add(strCatid); //每一组的题id记下来，用在分数总和上。

                            ctsc += sc;
                            qcml.Qclist = qcclist;
                            qclist.Add(qcml);

                            Model.QCModel qcml1 = new Model.QCModel();
                            qcml1.Qcid = "fx" + drlist[i]["F_CategoryId"].ToString();
                            qcml1.Qcname = "总分";
                            qcml1.Rowspan = "3";
                            qcml1.Qcscore = sc.ToString();
                            qclist.Add(qcml1);
                            DataColumn dcdf = new DataColumn("df" + i.ToString(), Type.GetType("System.String"));
                            dtNew.Columns.Add(dcdf);
                        }

                    }

                    qcmzf.Qcscore = ctsc.ToString();
                }

            }
            return qclist;
        }
        /// <summary>
        /// 导出数据用的这个接口 
        /// </summary>
        /// <param name="stime"></param>
        /// <param name="endtime"></param>
        /// <param name="dpt"></param>
        /// <returns></returns>
        public string ExportToExcelHeBing(string stime, string endtime, string dpt)
        {
            string Name = "质检数据报表";
            List<Model.QCModel> qclist = Getqulist();

            DataTable dt = GetExportData(stime, endtime, dpt);


            try
            {
                HSSFWorkbook workbook = new HSSFWorkbook();//导出xls
                ISheet sheet = workbook.CreateSheet("sheet1");

                ICellStyle HeadercellStyle = workbook.CreateCellStyle();
                HeadercellStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
                HeadercellStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
                HeadercellStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
                HeadercellStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
                HeadercellStyle.Alignment = HorizontalAlignment.Center;
                HeadercellStyle.VerticalAlignment = NPOI.SS.UserModel.VerticalAlignment.Center;

                HeadercellStyle.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.SkyBlue.Index;
                HeadercellStyle.FillPattern = FillPattern.SolidForeground;
                HeadercellStyle.FillBackgroundColor = NPOI.HSSF.Util.HSSFColor.SkyBlue.Index;

                //字体
                NPOI.SS.UserModel.IFont headerfont = workbook.CreateFont();
                headerfont.Boldweight = (short)FontBoldWeight.Bold;
                headerfont.FontHeightInPoints = 12;
                HeadercellStyle.SetFont(headerfont);


                #region 用column name 作为列名
                int icolIndex = 0;
                IRow headerRow = sheet.CreateRow(0);

                IRow headerRow1 = sheet.CreateRow(1);

                IRow headerRow2 = sheet.CreateRow(2);


                IRow headerRow3 = sheet.CreateRow(3);

                ICell cell01 = headerRow.CreateCell(icolIndex);
                cell01.SetCellValue("测评人");
                cell01.CellStyle = HeadercellStyle;
                icolIndex++;
                ICell cell02 = headerRow.CreateCell(icolIndex);
                cell02.SetCellValue("姓名");
                cell02.CellStyle = HeadercellStyle;
                icolIndex++;

                ICell cell03 = headerRow.CreateCell(icolIndex);
                cell03.SetCellValue("工号");
                cell03.CellStyle = HeadercellStyle;
                icolIndex++;
                ICell cell04 = headerRow.CreateCell(icolIndex);
                cell04.SetCellValue("手机号");
                cell04.CellStyle = HeadercellStyle;
                icolIndex++;


                ICell cell05 = headerRow.CreateCell(icolIndex);
                cell05.SetCellValue("质检时间");
                cell05.CellStyle = HeadercellStyle;
                icolIndex++;



                ICell c11 = headerRow1.CreateCell(0);
                c11.SetCellValue("");
                c11.CellStyle = HeadercellStyle;

                ICell c12 = headerRow1.CreateCell(1);

                c12.SetCellValue("");
                c12.CellStyle = HeadercellStyle;
                ICell c13 = headerRow1.CreateCell(2);
                c13.SetCellValue("");
                c13.CellStyle = HeadercellStyle;

                ICell c14 = headerRow1.CreateCell(3);

                c14.SetCellValue("");
                c14.CellStyle = HeadercellStyle;


                ICell c15 = headerRow1.CreateCell(4);

                c15.SetCellValue("");
                c15.CellStyle = HeadercellStyle;

                ICell c21 = headerRow2.CreateCell(0);
                c21.SetCellValue("");
                c21.CellStyle = HeadercellStyle;

                ICell c22 = headerRow2.CreateCell(1);

                c22.SetCellValue("");
                c22.CellStyle = HeadercellStyle;
                ICell c23 = headerRow2.CreateCell(2);
                c23.SetCellValue("");
                c23.CellStyle = HeadercellStyle;

                ICell c24 = headerRow2.CreateCell(3);

                c24.SetCellValue("");
                c24.CellStyle = HeadercellStyle;



                ICell c25 = headerRow2.CreateCell(4);

                c25.SetCellValue("");
                c25.CellStyle = HeadercellStyle;


                ICell c31 = headerRow3.CreateCell(0);
                c31.SetCellValue("");
                c31.CellStyle = HeadercellStyle;

                ICell c32 = headerRow3.CreateCell(1);

                c32.SetCellValue("");
                c32.CellStyle = HeadercellStyle;
                ICell c33 = headerRow3.CreateCell(2);
                c33.SetCellValue("");
                c33.CellStyle = HeadercellStyle;

                ICell c34 = headerRow3.CreateCell(3);

                c34.SetCellValue("");
                c34.CellStyle = HeadercellStyle;



                ICell c35 = headerRow3.CreateCell(4);

                c35.SetCellValue("");
                c35.CellStyle = HeadercellStyle;

                //起始行，结束行，起始列，结束列

                sheet.AddMergedRegion(new CellRangeAddress(0, 3, 0, 0));
                sheet.AddMergedRegion(new CellRangeAddress(0, 3, 1, 1));
                sheet.AddMergedRegion(new CellRangeAddress(0, 3, 2, 2));

                sheet.AddMergedRegion(new CellRangeAddress(0, 3, 3, 3));

                sheet.AddMergedRegion(new CellRangeAddress(0, 3, 4, 4));

                #endregion

                double allscore = 0;
                int lastcol = 0;

                if (qclist == null || (qclist != null && qclist.Count == 0))
                {
                    foreach (DataColumn dc in dt.Columns)
                    {
                        ICell cell = headerRow.CreateCell(icolIndex);
                        cell.SetCellValue(dc.ColumnName);
                        cell.CellStyle = HeadercellStyle;

                        icolIndex++;
                    }
                }
                else
                {
                    foreach (Model.QCModel dc in qclist)
                    {
                        if (dc.Qclist != null && dc.Qclist.Count != 0)
                        {
                            ICell cell = headerRow.CreateCell(icolIndex); //icolIndex=4
                            cell.SetCellValue(dc.Qcname); //指标分类
                            cell.CellStyle = HeadercellStyle;
                            int collhebing = Convert.ToInt32(dc.Colspan); //12

                            sheet.AddMergedRegion(new CellRangeAddress(0, 0, icolIndex, icolIndex + collhebing - 1));
                            int iii = icolIndex;
                            int i = icolIndex;//=4
                            foreach (Model.QCModel item in dc.Qclist)
                            {

                                ICell celll = headerRow1.CreateCell(i);
                                celll.SetCellValue(item.Qcname); //规范用语 其他客户zhishi
                                celll.CellStyle = HeadercellStyle;
                                int collhebing2 = Convert.ToInt32(item.Colspan);

                                for (int creatcell = 1; creatcell < collhebing2; creatcell++)
                                {
                                    ICell cellcreate = headerRow1.CreateCell(i + creatcell);
                                    cellcreate.SetCellValue(""); //规范用语 其他客户zhishi
                                    cellcreate.CellStyle = HeadercellStyle;
                                }

                                sheet.AddMergedRegion(new CellRangeAddress(1, 1, i, i + collhebing2 - 1));
                                if (item.Qclist != null && item.Qclist.Count > 0)
                                {

                                    foreach (Model.QCModel item1 in item.Qclist)
                                    {
                                        ICell cellq = headerRow2.CreateCell(i);
                                        cellq.SetCellValue(item1.Qcname);
                                        cellq.CellStyle = HeadercellStyle;

                                        ICell cellp = headerRow3.CreateCell(i);
                                        cellp.SetCellValue(item1.Qcscore);
                                        cellp.CellStyle = HeadercellStyle;

                                        allscore = allscore + Convert.ToDouble(item1.Qcscore);
                                        i++;
                                    }
                                }
                                lastcol = i;
                            }
                        }

                    }

                }
                #region 合并单元格
                ICell cell0N = headerRow.CreateCell(lastcol);
                cell0N.SetCellValue("总分");
                cell0N.CellStyle = HeadercellStyle;

                ICell cell1N = headerRow1.CreateCell(lastcol);

                cell1N.SetCellValue("");
                cell1N.CellStyle = HeadercellStyle;

                ICell cell2N = headerRow2.CreateCell(lastcol);

                cell2N.SetCellValue("");
                cell2N.CellStyle = HeadercellStyle;
                ICell cell3N = headerRow3.CreateCell(lastcol);

                cell3N.SetCellValue(allscore);
                cell3N.CellStyle = HeadercellStyle;


              

                sheet.AddMergedRegion(new CellRangeAddress(0, 2, lastcol, lastcol));
                //   sheet.AddMergedRegion(new CellRangeAddress(0, 2, lastcol+1, lastcol + 1));

                #endregion
                ICellStyle cellStyle = workbook.CreateCellStyle();

                //为避免日期格式被Excel自动替换，所以设定 format 为 『@』 表示一率当成text來看
                cellStyle.DataFormat = HSSFDataFormat.GetBuiltinFormat("@");
                cellStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
                cellStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
                cellStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
                cellStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;


                NPOI.SS.UserModel.IFont cellfont = workbook.CreateFont();
                cellfont.Boldweight = (short)FontBoldWeight.Normal;
                cellStyle.SetFont(cellfont);

                //建立内容行
                int iRowIndex = 0;
                foreach (DataRow dr in dt.Rows)
                {
                    int iCellIndex = 0;
                    IRow irow = sheet.CreateRow(iRowIndex + 4);
                    for (int i = 0; i < dt.Columns.Count; i++)
                    {
                        string strsj = string.Empty;
                        if (dr[i] != null)
                        {
                            strsj = dr[i].ToString();
                        }
                        ICell cell = irow.CreateCell(iCellIndex);
                        cell.SetCellValue(strsj);
                        cell.CellStyle = cellStyle;
                        iCellIndex++;
                    }
                    iRowIndex++;
                }


                using (MemoryStream ms = new MemoryStream())
                {
                    workbook.Write(ms);

                    HttpContext curContext = System.Web.HttpContext.Current;


                    // 设置编码和附件格式
                    curContext.Response.ContentType = "application/vnd.ms-excel";
                    curContext.Response.ContentEncoding = Encoding.UTF8;//Encoding.UTF8;
                    curContext.Response.Charset = "";
                    curContext.Response.AppendHeader("Content-Disposition",
                        "attachment;filename=" + HttpUtility.UrlEncode(Name + ".xls", Encoding.UTF8));

                    curContext.Response.BinaryWrite(ms.GetBuffer());

                    workbook = null;
                    ms.Close();
                    ms.Dispose();

                    curContext.Response.End();
                }

                return "";
            }
            catch (Exception e)
            {
                string sss = e.ToString();
                return "导出失败！";
            }

        }


        //前端的列表获取数据
        public ActionResult GetDataList(string stime, string endtime, int pagesize = 10, int pageindex = 1)
        {
            Model.PageData<object> pagedata = new Model.PageData<object>();

            string dpt = RequestString.GetQueryString("dpt");
         

            GetColumnList();

            string sqltimeCallRecords = "";
            string strsql = "";
            if (stime != null && stime.Trim() != "")
            {
                sqltimeCallRecords += " and CONVERT(varchar , F_QCtime, 120)>=CONVERT(varchar , '" + stime.Trim() + " 00:00:01', 120) ";
            }
            if (endtime != null && endtime.Trim() != "")
            {
                sqltimeCallRecords += " and CONVERT(varchar , F_QCtime, 120)<=CONVERT(varchar , '" + endtime.Trim() + " 23:59:59', 120) ";
            }
            if (dpt != null && dpt.ToString() != "")
            {
                strsql = " and ExtNumber in(  select  extNubmer from  extUserCodeContrast where deptId= " + dpt.ToString() + ") ";
            }

            //string tablenew = "(select  F_QCUserName,UserCode,dbo.GetUserName(UserCode) name ,CallId,CallNumber from T_Call_CallRecords ";// where F_QCState=1  and UserCode != 0" + strsql + sqltimeCallRecords;

            //DataTable dtcallrecorddata = DbHelperSQL.Query("select  F_QCUserName,UserCode,dbo.GetUserName(UserCode) name ,CallId,CallNumber from T_Call_CallRecords where F_QCState=1  and UserCode != 0" + strsql + sqltimeCallRecords).Tables[0];

        
            int recordCount = 0;
            var dt = BLL.PagerBLL.GetListPager(
                    "T_Call_CallRecords",
                    "CallId",
                    "F_QCUserName, UserCode, dbo.GetUserName(UserCode) name, CallRecordsId, CallNumber,F_QCTime",
                    " and F_QCState=1 " + strsql + sqltimeCallRecords,// and UserCode != 0
                    "order by F_QCTime desc",
                    pagesize,
                    pageindex,
                    true,
                    out recordCount);


            foreach (DataRow dr in dt.Rows)
            {
                DataRow drNew = dtNew.NewRow();

                DataTable dtqcres = DbHelperSQL.Query("  select* from T_QC_IndexRes where CallRecordsId = '" + dr["CallRecordsId"].ToString() + "'order by F_IndexId").Tables[0];

                drNew["QCName"] = dr["F_QCUserName"].ToString();
                drNew["UserName"] = dr["name"].ToString();
                drNew["UserCode"] = dr["UserCode"].ToString();

                //质检条数改成手机号
                drNew["ZJCount"] = dr["CallNumber"].ToString();

                drNew["QCTime"] = dr["F_QCtime"].ToString();

                double itemfs = 0;//每列分数和
                for (int i = 0; i < arlist.Count; i++)
                {
                    string strList = arlist[i];
                    string[] strTitleList = strList.Split(',');
                    foreach (string str in strTitleList)
                    {
                        if (str != "")
                        {
                            double fs = 0;//每一项分数
                            object obj2 = DbHelperSQL.GetSingle("select F_QCScore from T_QC_IndexRes where    F_IndexId=" + str + "and CallRecordsId=" + dr["CallRecordsId"].ToString());
                            if (obj2 != null && obj2.ToString() != "")
                            {
                                fs = double.Parse(obj2.ToString());
                            }
                            string mm = "";
                            if (fs.ToString().IndexOf('.') > -1 && fs.ToString().Substring(fs.ToString().IndexOf('.') + 1).Length > 2)
                            {
                                mm = fs.ToString("0.00");
                            }
                            else
                            {
                                mm = fs.ToString();
                            }
                            drNew["Q" + str] = mm;
                            itemfs += fs;
                        }
                    }
                    string imm = "";
                    if (itemfs.ToString().IndexOf('.') > -1 && itemfs.ToString().Substring(itemfs.ToString().IndexOf('.') + 1).Length > 2)
                    {
                        imm = itemfs.ToString("0.00");
                    }
                    else
                    {
                        imm = itemfs.ToString();
                    }
                    drNew["df" + i.ToString()] = imm;

                    itemfs = 0;
                }

                dtNew.Rows.Add(drNew);
            }


            var obj = new
            {
                rows = dtNew,
                total = recordCount
            };

            return Content(obj.ToJson());


          //  return Success("获取质检数据成功", dtNew);
        }


    }
}