20220313 3张表格 导出excel功能
This commit is contained in:
146
20220313_Excel/TempTask.WebEntry/Tools/NumZjHelper.cs
Normal file
146
20220313_Excel/TempTask.WebEntry/Tools/NumZjHelper.cs
Normal file
@@ -0,0 +1,146 @@
|
||||
|
||||
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
|
||||
{
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public class NumZjHelper
|
||||
{
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public enum ExcelType
|
||||
{
|
||||
/// <summary>
|
||||
/// 总表
|
||||
/// </summary>
|
||||
[Description("总表(样表).xls")]
|
||||
总表 = 1,
|
||||
/// <summary>
|
||||
/// 城市更新
|
||||
/// </summary>
|
||||
[Description("住建系统抓投资情况通报(城市更新)样表.xlsx")]
|
||||
城市更新 = 2,
|
||||
/// <summary>
|
||||
/// 房地产业
|
||||
/// </summary>
|
||||
[Description("住建系统抓投资情况通报(房地产业+GDP支撑性指标)样表.xlsx")]
|
||||
房地产业 = 3
|
||||
}
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <param name="year"></param>
|
||||
/// <param name="month"></param>
|
||||
/// <param name="week"></param>
|
||||
/// <param name="excelType"></param>
|
||||
/// <param name="sheets"></param>
|
||||
/// <returns></returns>
|
||||
public static string WriteTemplate(int year, int month, int week, ExcelType excelType, List<NumZjConfigSheet> sheets)
|
||||
{
|
||||
try
|
||||
{
|
||||
var _month = month.ToString().Length == 1 ? ("0" + month) : month.ToString();
|
||||
int excelTypeInt = (int)excelType;
|
||||
string template_name = EnumHelper.GetEnumDescription<ExcelType>(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++)
|
||||
{
|
||||
if (i >= datas.Count + (sheet_item.skip_rows?.Count ?? 0))
|
||||
break;
|
||||
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++)
|
||||
{
|
||||
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("文件被占用,请检查文件模板");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user