C#控制器保存文件

        /// <summary>
        /// 新旧分摊数据对比
        /// </summary>
        static void AllocRecordSummaryContrast()
        {
            List<AllocRecordSummaryDiff> recordSummaryDiffs = new List<AllocRecordSummaryDiff>();

            AllocationLogBLL bll = new AllocationLogBLL();
            foreach (var dtItem in _dataTypes)
            {
                foreach (var item in _serviceTypePairs)
                {
                    if (item.Key == "ALL")
                    {
                        continue;
                    }
                    List<AllocRecordSummaryDiff> diffs = bll.AllocRecordSummaryContrast(item.Key, dtItem.Key);
                    if (diffs != null && diffs.Count > 0)
                    {
                        recordSummaryDiffs.AddRange(diffs);
                    }
                }
            }




            var table = InitTable(recordSummaryDiffs);
            var exporter = new ExcelExporter();
            var buffer = exporter.ExportAsByteArray(table).GetAwaiter().GetResult();
            //创建本地文件夹
            string fileFolder = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Data");
            if (!Directory.Exists(fileFolder))
                Directory.CreateDirectory(fileFolder);

            //本地文件路径 \\
            var filepath = Path.Combine(fileFolder, $"新旧分摊差异{DateTime.Now.ToString("yyyyMMddHHmmss")}.xlsx");

            //HttpClient _httpClient = new HttpClient() { Timeout = TimeSpan.FromMinutes(30) };
            try
            {

                //创建本地文件写入流
                using (var stream = new FileStream(filepath, FileMode.Create, FileAccess.Write, FileShare.ReadWrite))
                {
                    stream.Write(buffer, 0, buffer.Length);
                }

                Console.WriteLine("导出差异文件:" + filepath);

            }
            catch
            {
                throw;
            }

        }


        /// <summary>
        /// 初始化导出的数据
        /// </summary>
        /// <param name="dataList"></param>
        /// <returns></returns>
        static DataTable InitTable(List<AllocRecordSummaryDiff> dataList)
        {


            var table = new DataTable();
            table.Columns.Add("服务类型");
            table.Columns.Add("业务单号");
            table.Columns.Add("数据类型");
            table.Columns.Add("对账编码");
            table.Columns.Add("总金额");
            table.Columns.Add("总金额差异");
            table.Columns.Add("成本计费重合计");
            table.Columns.Add("成本计费重合计差异");
            table.Columns.Add("应收计费重合计");
            table.Columns.Add("应收计费重合计差异");
            table.Columns.Add("总票数");
            table.Columns.Add("总票数差异");
            table.Columns.Add("备注");

            foreach (var data in dataList)
            {
                var row = table.NewRow();

                row["服务类型"] = data.service_type;
                row["业务单号"] = data.bsn_number;
                row["数据类型"] = data.data_type == "1" ? "预估" : "实际";
                row["对账编码"] = data.rec_code_old;
                row["总金额"] = data.amount_old;
                row["总金额差异"] = data.amount_diff;
                row["成本计费重合计"] = data.server_weight_old;
                row["成本计费重合计差异"] = data.server_weight_diff;
                row["应收计费重合计"] = data.shipper_weight_old;
                row["应收计费重合计差异"] = data.shipper_weight_diff;
                row["总票数"] = data.bill_count_old;
                row["总票数差异"] = data.bill_count_diff;
                row["备注"] = data.remarks;


                table.Rows.Add(row);
            }

            return table;
        }

 

posted @ 2022-06-08 09:08  妖狐鬼魅  阅读(184)  评论(0编辑  收藏  举报