using CRM.Core.BLL.B; using CRM.Core.Common.WebHelper; using CRM.Core.DTO; using CRM.Core.Model.EntityAudit; using CRM.Core.Model.QueryModels; using NPOI.SS.UserModel; using NPOI.SS.Util; using NPOI.XSSF.UserModel; using System; using System.Collections.Concurrent; using System.Collections.Generic; using System.IO; using System.Linq; using WX.CRM.Common; namespace CRM.Core.BLL.Application.Order { public class ExportAudit { private static NLog.Logger Logger = NLog.LogManager.GetCurrentClassLogger(); public retMsg ExportAudit_xlsx(List exportItems, string queryType) { retMsg retMsg = new retMsg() { result = true, retmsg = new ExportItemMsg() { } }; B_Audit_Items_BL b_Audit_Items_BL = new B_Audit_Items_BL(); var checkResult = Check_ExportAudit(exportItems, queryType); retMsg.retmsg.filemsg = checkResult.retmsg; QueryAudit queryAudit = new QueryAudit(); Dictionary Costkvs = queryAudit.GetAuditCost(); IWorkbook wb = new XSSFWorkbook(); Dictionary styles = CreateStyles(wb); foreach (var item in exportItems) { ISheet sheet = wb.CreateSheet(item.Name); //the header row: centered text in 48pt font IRow headerRow = sheet.CreateRow(0); headerRow.HeightInPoints = (40); ICell titleCell = headerRow.CreateCell(0); titleCell.SetCellValue(item.Name); titleCell.CellStyle = (styles[("title")]); sheet.AddMergedRegion(CellRangeAddress.ValueOf("$A$1:$M$1")); //header with month titles IRow rowtop = sheet.CreateRow(1); rowtop.Height = 100 * 6; int incomeNum = 1; rowtop.CreateCell(incomeNum - 1).SetCellValue("月份"); rowtop.CreateCell(incomeNum).SetCellValue("收入"); rowtop.CreateCell(incomeNum + 1).SetCellValue("当月收入"); rowtop.CreateCell(incomeNum + 2).SetCellValue("收入累计"); rowtop.CreateCell(incomeNum + 3).SetCellValue("预收"); rowtop.CreateCell(incomeNum + 4).SetCellValue("累计订单金额"); rowtop.CreateCell(incomeNum + 5).SetCellValue("当月订单净额"); rowtop.CreateCell(7).SetCellValue(""); rowtop.CreateCell(8).SetCellValue(""); int costpriceNum = 10; rowtop.CreateCell(costpriceNum - 1).SetCellValue("月份"); rowtop.CreateCell(costpriceNum).SetCellValue("已摊成本"); rowtop.CreateCell(costpriceNum + 1).SetCellValue("已摊成本累计"); rowtop.CreateCell(costpriceNum + 2).SetCellValue("未摊成本余额"); rowtop.CreateCell(costpriceNum + 3).SetCellValue("累计合同成本"); rowtop.CreateCell(costpriceNum + 4).SetCellValue("合同成本净额"); rowtop.CreateCell(costpriceNum + 5).SetCellValue("每月成本核对"); rowtop.CreateCell(costpriceNum + 6).SetCellValue("每月成本净额核对"); rowtop.CreateCell(costpriceNum + 7).SetCellValue("已摊成本退款"); rowtop.CreateCell(18).SetCellValue(""); int modulepriceNum = 19; rowtop.CreateCell(modulepriceNum).SetCellValue("订单金额"); rowtop.CreateCell(modulepriceNum + 1).SetCellValue("收入核对"); rowtop.CreateCell(21).SetCellValue(""); int modulRefundpriceNum = 23; rowtop.CreateCell(modulRefundpriceNum - 1).SetCellValue("月份"); rowtop.CreateCell(modulRefundpriceNum).SetCellValue("已使用退款"); rowtop.CreateCell(modulRefundpriceNum + 1).SetCellValue("总退款"); rowtop.CreateCell(modulRefundpriceNum + 2).SetCellValue("未使用退款"); rowtop.CreateCell(modulRefundpriceNum + 3).SetCellValue("实际退款"); rowtop.CreateCell(modulRefundpriceNum + 4).SetCellValue("未使用退款(验证)"); rowtop.CreateCell(28).SetCellValue(""); int paypriceNum = 30; rowtop.CreateCell(paypriceNum - 1).SetCellValue("月份"); var cell29 = rowtop.CreateCell(paypriceNum); cell29.SetCellValue("实际收款"); cell29.CellStyle = (styles[("bg_red")]); rowtop.CreateCell(paypriceNum + 1).SetCellValue("累计收款"); var cell32 = rowtop.CreateCell(paypriceNum + 2); cell32.SetCellValue("收款使用"); cell32.CellStyle = (styles[("bg_red")]); rowtop.CreateCell(paypriceNum + 3).SetCellValue("累计使用"); int calNum = 34; rowtop.CreateCell(calNum).SetCellValue("未使用订金(当月结余)"); rowtop.CreateCell(calNum + 1).SetCellValue("当月收款净额(扣退款)"); rowtop.CreateCell(calNum + 2).SetCellValue("每月金额核对"); var monlist = item.categorieskv["income"].keyValues.Keys.ToArray(); int rowcount = item.categorieskv["income"].keyValues.Count(); IRow row0 = sheet.CreateRow(2); row0.Height = 10; //冻结2行1列 sheet.CreateFreezePane(1, 2); var monNow = DateTime.Now.ToString("yyyyMM"); int monNowIndex = 0; for (int i = 0; i < rowcount; i++) { //cellnum定位到当前cell,rownum+1定位到当前row int rownum = i + 3; int mon = monlist[i]; //获取当前月的rownum if (monNow.Equals(mon.ToString())) { monNowIndex = rownum; } IRow row = sheet.CreateRow(rownum); //月份 row.CreateCell(incomeNum - 1).SetCellValue(mon); //收入 var cell1 = row.CreateCell(incomeNum); cell1.SetCellValue(item.categorieskv["income"].keyValues[mon]); cell1.CellStyle = (styles[("bg_red")]); //当月收入 row.CreateCell(incomeNum + 1).SetCellValue(item.categorieskv["monIncome"].keyValues[mon]); //收入累计 int num = incomeNum + 2; row.CreateCell(num).SetCellFormula(string.Format("{0}{1}+{2}{3}", sheetCell[incomeNum], rownum + 1, sheetCell[num], rownum)); //预收 row.CreateCell(incomeNum + 3).SetCellValue(item.categorieskv["depositreceived"].keyValues[mon]); //累计订单金额 int num5 = incomeNum + 4; row.CreateCell(num5).SetCellFormula(string.Format("{0}{1}+{2}{3}", sheetCell[num5 - 2], rownum + 1, sheetCell[num5 - 1], rownum + 1)); // 当月订单净额 int num6 = incomeNum + 5; row.CreateCell(num6).SetCellFormula(string.Format("{0}{1}-{2}{3}", sheetCell[num6 - 1], rownum + 1, sheetCell[num6 - 1], rownum)); row.CreateCell(incomeNum + 6).SetCellValue(""); row.CreateCell(incomeNum + 7).SetCellValue(""); row.CreateCell(costpriceNum - 1).SetCellValue(mon); //已摊成本 row.CreateCell(costpriceNum).SetCellValue(item.categorieskv["costprice"].keyValues[mon]); //已摊成本累计 int num10 = costpriceNum + 1; row.CreateCell(num10).SetCellFormula(string.Format("{0}{1}+{2}{3}", sheetCell[num10 - 1], rownum + 1, sheetCell[num10], rownum)); //未摊成本余额 row.CreateCell(costpriceNum + 2).SetCellValue(item.categorieskv["lastcostprice"].keyValues[mon]); //累计合同成本 int num13 = costpriceNum + 3; row.CreateCell(num13).SetCellFormula(string.Format("{0}{1}+{2}{3}", sheetCell[num13 - 1], rownum + 1, sheetCell[num13 - 2], rownum + 1)); //合同成本净额 int num14 = costpriceNum + 4; row.CreateCell(num14).SetCellFormula(string.Format("{0}{1}-{2}{3}", sheetCell[num14 - 1], rownum + 1, sheetCell[num14 - 1], rownum)); //每月成本核对 row.CreateCell(costpriceNum + 5).SetCellValue(item.categorieskv["checkCostprice"].keyValues[mon]); //每月成本净额核对 int num16 = costpriceNum + 6; var cell15 = row.CreateCell(num16); cell15.SetCellFormula(string.Format("{0}{1}-{2}{3}-{4}{5}", sheetCell[num16 - 2], rownum + 1, sheetCell[num16 - 1], rownum + 1, sheetCell[num16 + 1], rownum + 1)); cell15.CellStyle = (styles[("bg_lime")]); //已摊成本退款 row.CreateCell(costpriceNum + 7).SetCellValue(item.categorieskv["costpriceRefund"].keyValues[mon]); //总数不写成本信息 if (item.id == 1 || item.id == 14) { if (item.id == 14 || mon < 202201) { row.CreateCell(costpriceNum).SetCellValue(0); row.CreateCell(costpriceNum + 1).SetCellValue(0); row.CreateCell(costpriceNum + 2).SetCellValue(0); row.CreateCell(costpriceNum + 3).SetCellValue(0); row.CreateCell(costpriceNum + 4).SetCellValue(0); row.CreateCell(costpriceNum + 5).SetCellValue(0); row.CreateCell(costpriceNum + 6).SetCellValue(0); row.CreateCell(costpriceNum + 7).SetCellValue(0); } else { string template = "软件业务!#+'东方平台(顶底之王)'!#+投顾业务中心!#+内容新媒体!#+'东方平台(优品)'!#"; row.CreateCell(costpriceNum).SetCellFormula(template.Replace("#", string.Format("{0}{1}", sheetCell[costpriceNum], rownum + 1))); row.CreateCell(costpriceNum + 1).SetCellFormula(template.Replace("#", string.Format("{0}{1}", sheetCell[costpriceNum + 1], rownum + 1))); row.CreateCell(costpriceNum + 2).SetCellFormula(template.Replace("#", string.Format("{0}{1}", sheetCell[costpriceNum + 2], rownum + 1))); row.CreateCell(costpriceNum + 3).SetCellFormula(template.Replace("#", string.Format("{0}{1}", sheetCell[costpriceNum + 3], rownum + 1))); row.CreateCell(costpriceNum + 4).SetCellFormula(template.Replace("#", string.Format("{0}{1}", sheetCell[costpriceNum + 4], rownum + 1))); double costnum = 0; Costkvs.TryGetValue(mon, out costnum); row.CreateCell(costpriceNum + 5).SetCellValue(costnum); row.CreateCell(num16).SetCellFormula(string.Format("{0}{1}-{2}{3}-{4}{5}", sheetCell[num16 - 2], rownum + 1, sheetCell[num16 - 1], rownum + 1, sheetCell[num16 + 1], rownum + 1)); row.CreateCell(costpriceNum + 7).SetCellFormula(template.Replace("#", string.Format("{0}{1}", sheetCell[costpriceNum + 7], rownum + 1))); } } row.CreateCell(modulepriceNum - 1).SetCellValue(""); //订单金额 row.CreateCell(modulepriceNum).SetCellValue(item.categorieskv["modulePrice"].keyValues[mon]); //收入核对 var num20 = modulepriceNum + 1; var cell20 = row.CreateCell(num20); cell20.SetCellFormula(string.Format("{0}{1}-{2}{3}+{4}{5}", sheetCell[6], rownum + 1, sheetCell[num20 - 1], rownum + 1, sheetCell[num20 + 3], rownum + 1)); cell20.CellStyle = (styles[("bg_green")]); row.CreateCell(21).SetCellValue(""); //月份 row.CreateCell(modulRefundpriceNum - 1).SetCellValue(mon); //已使用退款 row.CreateCell(modulRefundpriceNum).SetCellValue(item.categorieskv["modulRefundprice"].keyValues[mon]); //总退款 row.CreateCell(modulRefundpriceNum + 1).SetCellValue(item.categorieskv["orderRefundprice"].keyValues[mon]); //未使用退款 row.CreateCell(modulRefundpriceNum + 2).SetCellValue(item.categorieskv["noUseOrderRefundprice"].keyValues[mon]); //实际退款 row.CreateCell(modulRefundpriceNum + 3).SetCellValue(item.categorieskv["actualRefundprice"].keyValues[mon]); //未使用退款(验证) int num27 = modulRefundpriceNum + 4; row.CreateCell(num27).SetCellFormula(string.Format("{0}{1}-{2}{3}-{4}{5}", sheetCell[modulRefundpriceNum + 1], rownum + 1, sheetCell[modulRefundpriceNum], rownum + 1, sheetCell[modulRefundpriceNum + 2], rownum + 1)); row.CreateCell(28).SetCellValue(""); //月份 row.CreateCell(paypriceNum - 1).SetCellValue(mon); //实际收款 row.CreateCell(paypriceNum).SetCellValue(item.categorieskv["payprice"].keyValues[mon]); //累计收款 row.CreateCell(paypriceNum + 1).SetCellFormula(string.Format("{0}{1}+{2}{3}", sheetCell[paypriceNum], rownum + 1, sheetCell[paypriceNum + 1], rownum)); //收款使用 row.CreateCell(paypriceNum + 2).SetCellValue(item.categorieskv["useprice"].keyValues[mon]); //累计使用 row.CreateCell(paypriceNum + 3).SetCellFormula(string.Format("{0}{1}+{2}{3}", sheetCell[paypriceNum + 2], rownum + 1, sheetCell[paypriceNum + 3], rownum)); //未使用订金(当月结余) row.CreateCell(calNum).SetCellFormula(string.Format("{0}{1}-{2}{3}", sheetCell[paypriceNum + 1], rownum + 1, sheetCell[paypriceNum + 3], rownum + 1)); //当月收款净额(扣退款) row.CreateCell(calNum + 1).SetCellFormula(string.Format("{0}{1}-{2}{3}", sheetCell[paypriceNum], rownum + 1, sheetCell[modulRefundpriceNum + 3], rownum + 1)); //每月金额核对 int num36 = calNum + 2; var cell36 = row.CreateCell(num36); cell36.SetCellFormula(string.Format("{0}{1}+{2}{3}+{4}{5}-{6}{7}-{8}{9}-{10}{11}-{12}{13}", sheetCell[calNum], rownum, sheetCell[incomeNum + 3], rownum, sheetCell[paypriceNum], rownum + 1, sheetCell[incomeNum], rownum + 1, sheetCell[incomeNum + 3], rownum + 1, sheetCell[modulRefundpriceNum + 3], rownum + 1, sheetCell[calNum], rownum + 1)); cell36.CellStyle = (styles[("bg_green")]); } //合计 IRow rowSum = sheet.CreateRow(rowcount + 3); rowSum.CreateCell(0).SetCellValue("合计"); var sumCell4 = rowSum.CreateCell(incomeNum + 3); sumCell4.SetCellFormula(string.Format("{0}{1}", sheetCell[incomeNum + 3], rowcount + 3)); sumCell4.CellStyle = styles[("bg_yellow")]; var sumCell33 = rowSum.CreateCell(paypriceNum + 3); sumCell33.SetCellFormula(string.Format("{0}{1}", sheetCell[paypriceNum + 3], rowcount + 3)); sumCell33.CellStyle = styles[("bg_yellow")]; var sumCell34 = rowSum.CreateCell(calNum); sumCell34.SetCellFormula(string.Format("{0}{1}", sheetCell[calNum], rowcount + 3)); sumCell34.CellStyle = styles[("bg_yellow")]; //收入,已摊成本,未摊成本余额,订单金额,已使用退款 int[] sumCellList = { incomeNum, costpriceNum, costpriceNum + 2, modulepriceNum, modulRefundpriceNum }; foreach (var cellitem in sumCellList) { var sumCell = rowSum.CreateCell(cellitem); sumCell.SetCellFormula(string.Format("SUM({0}{1}:{2}{3})", sheetCell[cellitem], 3, sheetCell[cellitem], rowcount + 3)); sumCell.CellStyle = styles[("bg_yellow")]; //sumCell.CellStyle.DataFormat =wb.CreateDataFormat().GetFormat("##,##0.000"); } //总计: IRow rowAllSum1 = sheet.CreateRow(rowcount + 5); rowAllSum1.CreateCell(4).SetCellValue("订单总金额"); IRow rowAllSum2 = sheet.CreateRow(rowcount + 6); //收入+预收+已使用退款 rowAllSum2.CreateCell(4).SetCellFormula(string.Format("{0}{1}+{2}{3}+{4}{5}", sheetCell[incomeNum], rowcount + 4, sheetCell[incomeNum + 3], rowcount + 4, sheetCell[modulRefundpriceNum], rowcount + 4)); IRow rowAllSum3 = sheet.CreateRow(rowcount + 7); rowAllSum3.CreateCell(4).SetCellValue("总金额"); rowAllSum3.CreateCell(5).SetCellValue("退款"); rowAllSum3.CreateCell(6).SetCellValue("扣去退款"); IRow rowAllSum4 = sheet.CreateRow(rowcount + 8); rowAllSum4.CreateCell(4).SetCellValue(item.categorieskv["sumPayprice"].keyValues.FirstOrDefault().Value); rowAllSum4.CreateCell(5).SetCellValue(item.categorieskv["sumRefundprice"].keyValues.FirstOrDefault().Value); rowAllSum4.CreateCell(6).SetCellFormula(string.Format("{0}{1}-{2}{3}", sheetCell[4], rowcount + 9, sheetCell[5], rowcount + 9)); //累计收入+累计预收+未使用订金 IRow rowAllSum5 = sheet.CreateRow(rowcount + 9); rowAllSum5.CreateCell(6).SetCellFormula(string.Format("{0}{1}+{2}{3}+{4}{5}", sheetCell[incomeNum], rowcount + 4, sheetCell[incomeNum + 3], rowcount + 4, sheetCell[calNum], rowcount + 4)); IRow rowAllSum6 = sheet.CreateRow(rowcount + 10); rowAllSum6.CreateCell(6).SetCellFormula(string.Format("{0}{1}-{2}{3}", sheetCell[6], rowcount + 9, sheetCell[6], rowcount + 10)); //总数写个核对校验 var childitems = b_Audit_Items_BL.GetAuditItemsFromId(item.id); if (childitems.Count>0) { //收入,预收,订单金额,总退款,实际收款,收款使用,未使用订金 IRow rowAllSumCheck = sheet.CreateRow(rowcount + 12); var ts = childitems.Select(d => d.item).ToArray();; var temp = $"#-'{string.Join("'!#-'",ts)}'!#"; //var temp = "#-软件!#-'呱呱(总数)'!#-首华!#-'平台一(总数)'!#-通联!#-增量传媒!#-投顾业务中心!#-原东二!#-东二!#-内容三!#-内容二!#-'东方平台(优品)'!#-'东方平台(首华)'!#"; rowAllSumCheck.CreateCell(0).SetCellValue("总数核对"); rowAllSumCheck.CreateCell(incomeNum).SetCellFormula(temp.Replace("#", string.Format("{0}{1}", sheetCell[incomeNum], rowcount + 3))); rowAllSumCheck.CreateCell(incomeNum + 3).SetCellFormula(temp.Replace("#", string.Format("{0}{1}", sheetCell[incomeNum + 3], rowcount + 3))); rowAllSumCheck.CreateCell(modulepriceNum).SetCellFormula(temp.Replace("#", string.Format("{0}{1}", sheetCell[modulepriceNum], rowcount + 3))); rowAllSumCheck.CreateCell(modulRefundpriceNum + 1).SetCellFormula(temp.Replace("#", string.Format("{0}{1}", sheetCell[modulRefundpriceNum + 1], rowcount + 3))); rowAllSumCheck.CreateCell(paypriceNum).SetCellFormula(temp.Replace("#", string.Format("{0}{1}", sheetCell[paypriceNum], rowcount + 3))); rowAllSumCheck.CreateCell(paypriceNum + 2).SetCellFormula(temp.Replace("#", string.Format("{0}{1}", sheetCell[paypriceNum + 2], rowcount + 3))); rowAllSumCheck.CreateCell(calNum).SetCellFormula(temp.Replace("#", string.Format("{0}{1}", sheetCell[calNum], rowcount + 3))); //设置数字格式,防止0.000E出现 int[] rowcount12NumCellList = { incomeNum, incomeNum + 3, modulepriceNum, modulRefundpriceNum + 1, paypriceNum, paypriceNum + 2, calNum }; foreach (var cellitem in rowcount12NumCellList) { sheet.GetRow(rowcount + 12).GetCell(cellitem).CellStyle = (styles[("bg_green")]); } } //折叠行 sheet.GroupRow(4, 16); //sheet.SetRowGroupCollapsed(0, true); for (int i = 0; i < rowcount; i++) { var row = i + 3; //设置红色字体 int[] redNumCellList = { modulepriceNum, modulRefundpriceNum, modulRefundpriceNum + 1, modulRefundpriceNum + 2, modulRefundpriceNum + 3, modulRefundpriceNum + 4 }; foreach (var cellitem in redNumCellList) { sheet.GetRow(row).GetCell(cellitem).CellStyle = (styles[("font_red")]); } //设置当前月黄色 if (row == monNowIndex) { var lastcell = sheet.GetRow(row).LastCellNum; for (int j = 0; j < lastcell - 1; j++) { sheet.GetRow(row - 1).GetCell(j).CellStyle = styles[("bg_yellow")]; } } //设置数字格式,防止0.000E出现 int[] NumCellList = { 2, 4, 6, 13,14,16 }; foreach (var cellitem in NumCellList) { sheet.GetRow(row).GetCell(cellitem).CellStyle = (styles[("bg_white")]); } } //设置单元格自动长度 for (int i = 0; i < 36; i++) { sheet.AutoSizeColumn(i); } } string path = System.AppDomain.CurrentDomain.BaseDirectory; // Write the output to a file string pathdir = "\\Export\\"; if (!Directory.Exists(path + pathdir)) { Directory.CreateDirectory(path + pathdir); } String file = pathdir + string.Format("AuditDataExport{0}.xlsx", DateTime.Now.ToLocalTime().ToString("yyyyMMddHHmmssfffff")); string filepath = path + file; using (FileStream fs = new FileStream(filepath, FileMode.Create)) { wb.Write(fs); } retMsg.retmsg.filepath = file; return retMsg; } private static ConcurrentDictionary rownumkv = new ConcurrentDictionary(); private static IRow getRow(ISheet sheet, int rownum) { IRow rowtop = null; if (rownumkv.TryAdd(rownum, rownum)) { rowtop = sheet.CreateRow(rownum); } else { rowtop = sheet.GetRow(rownum); } return rowtop; } private static Dictionary CreateStyles(IWorkbook wb) { Dictionary styles = new Dictionary(); short borderColor = IndexedColors.Grey50Percent.Index; ICellStyle style; IFont titleFont = wb.CreateFont(); titleFont.FontHeightInPoints = ((short)24); titleFont.Color = (IndexedColors.DarkBlue.Index); style = wb.CreateCellStyle(); style.Alignment = (HorizontalAlignment.Center); style.VerticalAlignment = VerticalAlignment.Center; style.SetFont(titleFont); styles.Add("title", style); style = wb.CreateCellStyle(); style.FillForegroundColor = IndexedColors.Red.Index; style.FillPattern = FillPattern.SolidForeground; style.DataFormat = wb.CreateDataFormat().GetFormat("##,##0.00"); styles.Add("bg_red", style); style = wb.CreateCellStyle(); style.FillForegroundColor = IndexedColors.Yellow.Index; style.BorderLeft = BorderStyle.Dotted; style.BorderRight = BorderStyle.Dotted; style.BorderTop = BorderStyle.Dotted; style.BorderBottom = BorderStyle.Dotted; style.FillPattern = FillPattern.SolidForeground; styles.Add("bg_yellow", style); style = wb.CreateCellStyle(); style.FillForegroundColor = IndexedColors.White.Index; style.BorderLeft = BorderStyle.Dotted; style.BorderRight = BorderStyle.Dotted; style.BorderTop = BorderStyle.Dotted; style.BorderBottom = BorderStyle.Dotted; style.FillPattern = FillPattern.SolidForeground; style.DataFormat = wb.CreateDataFormat().GetFormat("##,##0.00"); styles.Add("bg_white", style); style = wb.CreateCellStyle(); style.FillForegroundColor = IndexedColors.Lime.Index; style.BorderLeft = BorderStyle.Dotted; style.BorderRight = BorderStyle.Dotted; style.BorderTop = BorderStyle.Dotted; style.BorderBottom = BorderStyle.Dotted; style.FillPattern = FillPattern.SolidForeground; style.DataFormat = wb.CreateDataFormat().GetFormat("##,##0.00"); styles.Add("bg_lime", style); style = wb.CreateCellStyle(); style.FillForegroundColor = IndexedColors.Green.Index; style.BorderLeft = BorderStyle.Dotted; style.BorderRight = BorderStyle.Dotted; style.BorderTop = BorderStyle.Dotted; style.BorderBottom = BorderStyle.Dotted; style.FillPattern = FillPattern.SolidForeground; style.DataFormat = wb.CreateDataFormat().GetFormat("##,##0.00"); styles.Add("bg_green", style); var redFont = wb.CreateFont(); redFont.FontHeightInPoints = ((short)11); redFont.Color = IndexedColors.Red.Index; style = wb.CreateCellStyle(); style.SetFont(redFont); style.DataFormat = wb.CreateDataFormat().GetFormat("##,##0.00"); styles.Add("font_red", style); return styles; } /// /// 导出结算数据时,对历史数据进行检查,检查通过将保存新的结算数据。 /// /// /// public retMsg Check_ExportAudit(List exportItems,string QueryType) { var retmsg = new retMsg() { result = true }; var lastmonkey = Int32.Parse(DateTime.Now.AddMonths(-1).ToString("yyyyMM")); var monkey = Int32.Parse(DateTime.Now.ToString("yyyyMM")); var NocheckCategories = new List() { "sumPayprice", "sumRefundprice", "payprice" }; B_Audit_Check_BL b_Audit_Check_BL = new B_Audit_Check_BL(); var querytype = AuditResultQuery.QueryType.income.ToString(); var base_ItemsResult = b_Audit_Check_BL.GetList(d => d.Monkey == lastmonkey&&d.QueryType== querytype).OrderByDescending(d => d.Id).ToDictionary(d => d.itemId + ":" + d.Categorie, d => d); Logger.Info($"开始检查比对monkey:{lastmonkey},将检查到次月之前的历史数据"); bool CheckSuccess = true; foreach (var item in exportItems) { foreach (var categorie in item.categorieskv) { B_Audit_Check Base_b_Audit_Check = new B_Audit_Check(); var key = string.Format("{0}:{1}", item.id, categorie.Key); if (base_ItemsResult.TryGetValue(key, out Base_b_Audit_Check)) { if (NocheckCategories.Contains(categorie.Key)) continue; Logger.Info($"检查{item.id},{categorie.Key},{item.Name},{categorie.Value.name}"); var baseKV = JsonHelper.FromJson>(Base_b_Audit_Check.Resultskv); foreach (var baseKv_item in baseKV) { if (baseKv_item.Key <=lastmonkey) { var newValue = categorie.Value.keyValues[baseKv_item.Key]; var Num = Math.Round(newValue - baseKv_item.Value, 2); if (Num!=0) { CheckSuccess = false; var msg = $"{item.Name},{categorie.Value.name}:{baseKv_item.Key}:old:{baseKv_item.Value},new:{newValue};差额:{Num}"; Logger.Info(msg); retmsg.retmsg += $"{msg}\r\n"; } } } } } } if (QueryType.Equals(AuditResultQuery.QueryType.income.ToString()) || QueryType.Equals(AuditResultQuery.QueryType.cur.ToString())) { SaveExportAudit(exportItems, QueryType); } if (CheckSuccess) { retmsg.retmsg = "检查成功!"; } if (base_ItemsResult.Count == 0) { retmsg.retmsg = $"未检查,{lastmonkey}无结果"; } return retmsg; } public void SaveExportAudit(List exportItems,string QueryType) { B_Audit_Check_BL b_Audit_Check_BL = new B_Audit_Check_BL(); List b_Audit_CheckList = new List(); var monkey = Int32.Parse(DateTime.Now.ToString("yyyyMM")); Logger.Info(string.Format($"开始更新结算结果:{monkey}")); foreach (var item in exportItems) { foreach (var categorie in item.categorieskv) { B_Audit_Check b_Audit_Check = new B_Audit_Check() { Monkey = monkey, QueryType = QueryType, itemId = item.id, Item = item.Name, Categorie = categorie.Key, CategorieName = categorie.Value.name, Resultskv = categorie.Value.keyValues.ToJson(), Ctime = DateTime.Now }; b_Audit_CheckList.Add(b_Audit_Check); } } var categories = b_Audit_CheckList.Select(a => a.Categorie).ToList(); b_Audit_Check_BL.Delete(d => d.Monkey == monkey &&d.QueryType.Equals(QueryType) && categories.Contains(d.Categorie)); b_Audit_Check_BL.AddList(b_Audit_CheckList); Logger.Info(string.Format("开始更新结算结果{0}条", b_Audit_CheckList.Count)); } public class ExportItemMsg { public string filepath { get; set; } public string filemsg { get; set; } } public class ExportItem { public int id { get; set; } public string Name { get; set; } public Dictionary categorieskv { get; set; } } public class Category { public string key { get; set; } public string name { get; set; } public Dictionary keyValues { get; set; } } public class CellInfo { public int cellNum { get; set; } public string cellName { get; set; } public string cellText { get; set; } } private string[] sheetCell = new string[] { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "AA", "AB", "AC", "AD", "AE", "AF", "AG", "AH", "AI", "AJ", "AK", "AL", "AM", "AN", "AO", "AP", "AQ", "AR", "AS", "AT", "AU", "AV", "AW", "AX", "AY", "AZ", "BA", "BB", "BC", "BD", "BE", "BF", "BG", "BH", "BI", "BJ", "BK", "BL", "BM", "BN", "BO", "BP", "BQ", "BR", "BS", "BT", "BU", "BV", "BW", "BX", "BY", "BZ" }; } }