using Furion;
using Furion.FriendlyException;
using Furion.LinqBuilder;
using Newtonsoft.Json.Linq;
using NPOI.SS.UserModel;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using TempTask.WebEntry.ApiController.Dto;
namespace TempTask.WebEntry.Tools
{
///
///
///
public class NumZjHelper
{
///
///
///
public enum ExcelType
{
///
/// 总表
///
[Description("总表(样表).xls")]
总表 = 1,
///
/// 城市更新
///
[Description("住建系统抓投资情况通报(城市更新)样表.xlsx")]
城市更新 = 2,
///
/// 房地产业
///
[Description("住建系统抓投资情况通报(房地产业+GDP支撑性指标)样表.xlsx")]
房地产业 = 3
}
///
///
///
///
///
///
///
///
///
public static string WriteTemplate(int year, int month, int week, ExcelType excelType, List sheets)
{
try
{
var _month = month.ToString().Length == 1 ? ("0" + month) : month.ToString();
int excelTypeInt = (int)excelType;
string template_name = EnumHelper.GetEnumDescription(excelType);
string excelFilePath = $"{App.WebHostEnvironment.WebRootPath}\\ExcelTemplate\\{template_name}";
string outputPath = string.Empty;
if (!string.IsNullOrEmpty(excelFilePath))
{
using (FileStream excelFileStream = System.IO.File.OpenRead(excelFilePath))
{
bool isCompatible = ExcelHelper.GetIsCompatible(excelFilePath);
IWorkbook workbook = ExcelHelper.CreateWorkbook(isCompatible, excelFileStream);
foreach (var sheet_item in sheets)
{
if (sheet_item.url.IsNullOrEmpty())
continue;
ISheet sheet = null;
//if (int.TryParse(sheet_name.sheet_no, out sheetIndex))
//{
sheet = workbook.GetSheetAt(sheet_item.sheet_no - 1);
//}
//else
//{
// sheet = workbook.GetSheet(sheet_name.Value);
//}
var api_result = HttpHelper.CallUrl(sheet_item.url, $"months={year}{_month}&w={week}");
var obj = JObject.Parse(api_result);
var datas = obj["data"] as JArray;
//从第几行开始 , 比如 行号是4 , 就写3
var startRowIndex = sheet_item.start_row - 1;
//当前已经跳了几行
int current_skip_count = 0;
for (int i = 0; i < sheet_item.null_row_count; i++)
{
var c_rowindex = startRowIndex + i;
if (sheet_item.skip_rows != null && sheet_item.skip_rows.Contains(c_rowindex + 1))
{
current_skip_count++;
continue;
}
int cell_start_index = sheet_item.start_cell.ToExcelColumnIndex();
for (int x = cell_start_index; x < cell_start_index + sheet_item.null_cell_count; x++)
{
if (i >= datas.Count + (sheet_item.skip_rows?.Count ?? 0))
{
sheet.GetRow(c_rowindex).GetCell(x).SetCellValue("/");
}
else
{
var column_letter = App.Configuration[$"column_name_match_{excelTypeInt}_{sheet_item.sheet_no}:{ x.ToExcelColumnName()}"];
if (string.IsNullOrWhiteSpace(column_letter))
continue;
var jtoken = datas[i - current_skip_count][column_letter];
var cellvalue = string.Empty;
if (jtoken.Type == JTokenType.Null)
cellvalue = "/";
else
cellvalue = jtoken.ToString();
sheet.GetRow(c_rowindex).GetCell(x).SetCellValue(cellvalue);
}
}
//var x = 0;
//foreach (var cell in cells)
//{
// if (x < sheet_item.skip)
// {
// x++;
// continue;
// }
// //if (cell.Value.ToString().StartsWith("3302"))
// // continue;
// if (x >= sheet_item.null_cell_count)
// continue;
// sheet.GetRow(c_rowindex).GetCell(sheet_item.start_cell.ToExcelColumnIndex() - 1 + x - sheet_item.skip).SetCellValue(cell.Value.ToString());
// x++;
//}
}
}
var file = new FileInfo(excelFilePath);
var savePath = file.DirectoryName + "\\OutPut\\";
if (!Directory.Exists(savePath))
Directory.CreateDirectory(savePath);
outputPath = savePath + year + "_" + _month + "_" + week + "_" + template_name;
using (var filess = new FileStream(outputPath, FileMode.Create, FileAccess.Write, FileShare.Read))
{
workbook.Write(filess);
}
}
}
return outputPath;
}
catch (System.IO.IOException ioex)
{
throw Oops.Oh("文件被占用,请检查文件模板");
}
}
}
}