344 lines
18 KiB
C#
344 lines
18 KiB
C#
using Furion;
|
|
using Furion.FriendlyException;
|
|
using NPOI.HSSF.UserModel;
|
|
using NPOI.SS.UserModel;
|
|
using NPOI.XSSF.UserModel;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Text.RegularExpressions;
|
|
using System.Threading.Tasks;
|
|
using Vote.Services.Dto;
|
|
using Vote.Services.Entities;
|
|
|
|
namespace Vote.Services.Tools
|
|
{
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
public static class ExcelHelper
|
|
{
|
|
static ExcelHelper()
|
|
{
|
|
System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance);
|
|
}
|
|
/// <summary>
|
|
/// 判断是否为兼容模式
|
|
/// </summary>
|
|
/// <param name="filePath"></param>
|
|
/// <returns></returns>
|
|
public static bool GetIsCompatible(string filePath)
|
|
{
|
|
return filePath.EndsWith(".xls", StringComparison.OrdinalIgnoreCase);
|
|
}
|
|
/// <summary>
|
|
/// 创建工作薄
|
|
/// </summary>
|
|
/// <param name="isCompatible"></param>
|
|
/// <returns></returns>
|
|
public static IWorkbook CreateWorkbook(bool isCompatible)
|
|
{
|
|
if (isCompatible)
|
|
{
|
|
return new HSSFWorkbook();
|
|
}
|
|
else
|
|
{
|
|
return new XSSFWorkbook();
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 创建工作薄(依据文件流)
|
|
/// </summary>
|
|
/// <param name="isCompatible"></param>
|
|
/// <param name="stream"></param>
|
|
/// <returns></returns>
|
|
public static IWorkbook CreateWorkbook(bool isCompatible, dynamic stream)
|
|
{
|
|
if (isCompatible)
|
|
{
|
|
return new HSSFWorkbook(stream);
|
|
}
|
|
else
|
|
{
|
|
return new XSSFWorkbook(stream);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
/// 列名字母转索引
|
|
/// </summary>
|
|
/// <param name="columnName"></param>
|
|
/// <returns></returns>
|
|
public static int ToExcelColumnIndex(this string columnName)
|
|
{
|
|
if (!Regex.IsMatch(columnName.ToUpper(), @"[A-Z]+")) { throw new Exception("invalid parameter"); }
|
|
int index = 0;
|
|
char[] chars = columnName.ToUpper().ToCharArray();
|
|
for (int i = 0; i < chars.Length; i++)
|
|
{
|
|
index += ((int)chars[i] - (int)'A' + 1) * (int)Math.Pow(26, chars.Length - i - 1);
|
|
}
|
|
return index - 1;
|
|
}
|
|
/// <summary>
|
|
/// 列索引转字母
|
|
/// </summary>
|
|
/// <param name="index"></param>
|
|
/// <returns></returns>
|
|
public static string ToExcelColumnName(this int index)
|
|
{
|
|
if (index < 0) { throw new Exception("invalid parameter"); }
|
|
List<string> chars = new List<string>();
|
|
do
|
|
{
|
|
if (chars.Count > 0) index--;
|
|
chars.Insert(0, ((char)(index % 26 + (int)'A')).ToString());
|
|
index = (int)((index - index % 26) / 26);
|
|
} while (index > 0);
|
|
return String.Join(string.Empty, chars.ToArray());
|
|
}
|
|
|
|
|
|
private static List<ProjectsList2Output> GetDataByType(List<ProjectsList2Output> list, Entities.EnumProjectType type)
|
|
{
|
|
return list.Where(a => a.type == type).ToList();
|
|
}
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public static string WriteTemplate(List<ProjectsList2Output> list, int start_row, string start_column)
|
|
{
|
|
try
|
|
{
|
|
string template_name = "2024年度宁波市结构优质认定项目投票结果.xlsx";
|
|
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);
|
|
ISheet sheet = null;
|
|
sheet = workbook.GetSheetAt(0);
|
|
Dictionary<string, string> dic_sheet_config = new Dictionary<string, string>();
|
|
var data = GetDataByType(list, Entities.EnumProjectType.FangJian);
|
|
//从第几行开始 , 比如 行号是4 , 就写3
|
|
var startRowIndex = start_row - 1;
|
|
for (int i = 0; i < data.Count; i++)
|
|
{
|
|
var c_rowindex = startRowIndex + i;
|
|
int cell_start_index = start_column.ToExcelColumnIndex();
|
|
sheet.GetRow(c_rowindex).GetCell(cell_start_index).SetCellValue(data[i].no_count);
|
|
sheet.GetRow(c_rowindex).GetCell(cell_start_index + 1).SetCellValue(data[i].yes_count);
|
|
sheet.GetRow(c_rowindex).GetCell(cell_start_index + 2).SetCellValue(data[i].is_agree ? "通过" : "不通过");
|
|
}
|
|
var length0 = data.Count;
|
|
data = GetDataByType(list, Entities.EnumProjectType.ShiZheng);
|
|
for (int i = 0; i < data.Count; i++)
|
|
{
|
|
var c_rowindex = length0 + (length0 > 0 ? 1 : 0) + startRowIndex + i;
|
|
int cell_start_index = start_column.ToExcelColumnIndex();
|
|
sheet.GetRow(c_rowindex).GetCell(cell_start_index).SetCellValue(data[i].no_count);
|
|
sheet.GetRow(c_rowindex).GetCell(cell_start_index + 1).SetCellValue(data[i].yes_count);
|
|
sheet.GetRow(c_rowindex).GetCell(cell_start_index + 2).SetCellValue(data[i].is_agree ? "通过" : "不通过");
|
|
}
|
|
var length1 = data.Count;
|
|
data = GetDataByType(list, Entities.EnumProjectType.GuiDaoGongCheng);
|
|
for (int i = 0; i < data.Count; i++)
|
|
{
|
|
var c_rowindex = length0 + (length0 > 0 ? 1 : 0) + length1 + (length1 > 0 ? 1 : 0) + startRowIndex + i;
|
|
int cell_start_index = start_column.ToExcelColumnIndex();
|
|
sheet.GetRow(c_rowindex).GetCell(cell_start_index).SetCellValue(data[i].no_count);
|
|
sheet.GetRow(c_rowindex).GetCell(cell_start_index + 1).SetCellValue(data[i].yes_count);
|
|
sheet.GetRow(c_rowindex).GetCell(cell_start_index + 2).SetCellValue(data[i].is_agree ? "通过" : "不通过");
|
|
}
|
|
var length2 = data.Count;
|
|
data = GetDataByType(list, Entities.EnumProjectType.DianLiGongCheng);
|
|
for (int i = 0; i < data.Count; i++)
|
|
{
|
|
var c_rowindex = length0 + (length0 > 0 ? 1 : 0) + length1 + (length1 > 0 ? 1 : 0) + length2 + (length2 > 0 ? 1 : 0) + startRowIndex + i;
|
|
int cell_start_index = start_column.ToExcelColumnIndex();
|
|
sheet.GetRow(c_rowindex).GetCell(cell_start_index).SetCellValue(data[i].no_count);
|
|
sheet.GetRow(c_rowindex).GetCell(cell_start_index + 1).SetCellValue(data[i].yes_count);
|
|
sheet.GetRow(c_rowindex).GetCell(cell_start_index + 2).SetCellValue(data[i].is_agree ? "通过" : "不通过");
|
|
}
|
|
var length3 = data.Count;
|
|
data = GetDataByType(list, Entities.EnumProjectType.JiaoTongGongCheng);
|
|
for (int i = 0; i < data.Count; i++)
|
|
{
|
|
var c_rowindex = length0 + (length0 > 0 ? 1 : 0) + length1 + (length1 > 0 ? 1 : 0) + length2 + (length2 > 0 ? 1 : 0) + length3 + (length3 > 0 ? 1 : 0) + startRowIndex + i;
|
|
int cell_start_index = start_column.ToExcelColumnIndex();
|
|
sheet.GetRow(c_rowindex).GetCell(cell_start_index).SetCellValue(data[i].no_count);
|
|
sheet.GetRow(c_rowindex).GetCell(cell_start_index + 1).SetCellValue(data[i].yes_count);
|
|
sheet.GetRow(c_rowindex).GetCell(cell_start_index + 2).SetCellValue(data[i].is_agree ? "通过" : "不通过");
|
|
}
|
|
var length4 = data.Count;
|
|
data = GetDataByType(list, Entities.EnumProjectType.ShuiLiGongCheng);
|
|
for (int i = 0; i < data.Count; i++)
|
|
{
|
|
var c_rowindex = length0 + (length0 > 0 ? 1 : 0) + length1 + (length1 > 0 ? 1 : 0) + length2 + (length2 > 0 ? 1 : 0) + length3 + (length3 > 0 ? 1 : 0) + length4 + (length4 > 0 ? 1 : 0) + startRowIndex + i;
|
|
int cell_start_index = start_column.ToExcelColumnIndex();
|
|
sheet.GetRow(c_rowindex).GetCell(cell_start_index).SetCellValue(data[i].no_count);
|
|
sheet.GetRow(c_rowindex).GetCell(cell_start_index + 1).SetCellValue(data[i].yes_count);
|
|
sheet.GetRow(c_rowindex).GetCell(cell_start_index + 2).SetCellValue(data[i].is_agree ? "通过" : "不通过");
|
|
}
|
|
var file = new FileInfo(excelFilePath);
|
|
var savePath = file.DirectoryName + "\\OutPut\\";
|
|
if (!Directory.Exists(savePath))
|
|
Directory.CreateDirectory(savePath);
|
|
outputPath = savePath + DateTime.Now.ToString("yyyyMMddHHmmsss") + "-" + template_name;
|
|
using (var filess = new FileStream(outputPath, FileMode.Create, FileAccess.Write, FileShare.Read))
|
|
{
|
|
workbook.Write(filess, true);
|
|
}
|
|
}
|
|
}
|
|
return outputPath;
|
|
}
|
|
catch (System.IO.IOException ioex)
|
|
{
|
|
throw Oops.Oh("文件被占用,请检查文件模板");
|
|
}
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public static string WriteExcelNingBoZhiChun(List<nbzc_person> data)
|
|
{
|
|
try
|
|
{
|
|
string template_name = "共赴宁波之春模板.xlsx";
|
|
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);
|
|
ISheet sheet = null;
|
|
sheet = workbook.GetSheetAt(0);
|
|
//从第几行开始 , 比如 行号是4 , 就写3
|
|
var startRowIndex = 1;
|
|
for (int i = 0; i < data.Count; i++)
|
|
{
|
|
var c_rowindex = startRowIndex + i;
|
|
var row = sheet.CreateRow(c_rowindex);
|
|
int cell_start_index = 0;
|
|
row.CreateCell(cell_start_index).SetCellValue(data[i].CreatedTime.Value.ToString("yyyy-MM-dd HH:mm:ss"));
|
|
row.CreateCell(cell_start_index + 1).SetCellValue(data[i].name);
|
|
row.CreateCell(cell_start_index + 2).SetCellValue(data[i].phone);
|
|
row.CreateCell(cell_start_index + 3).SetCellValue(data[i].weixin_number);
|
|
row.CreateCell(cell_start_index + 4).SetCellValue(data[i].cardno);
|
|
row.CreateCell(cell_start_index + 5).SetCellValue(data[i].hangye);
|
|
row.CreateCell(cell_start_index + 6).SetCellValue(data[i].date);
|
|
row.CreateCell(cell_start_index + 7).SetCellValue(data[i].line);
|
|
row.CreateCell(cell_start_index + 8).SetCellValue(data[i].address);
|
|
}
|
|
var file = new FileInfo(excelFilePath);
|
|
var savePath = file.DirectoryName + "\\OutPut\\";
|
|
if (!Directory.Exists(savePath))
|
|
Directory.CreateDirectory(savePath);
|
|
outputPath = savePath + DateTime.Now.ToString("yyyyMMddHHmmsss") + "-" + template_name;
|
|
using (var filess = new FileStream(outputPath, FileMode.Create, FileAccess.Write, FileShare.Read))
|
|
{
|
|
workbook.Write(filess, true);
|
|
}
|
|
}
|
|
}
|
|
return outputPath;
|
|
}
|
|
catch (System.IO.IOException ioex)
|
|
{
|
|
throw Oops.Oh("文件被占用,请检查文件模板");
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public static string WriteExcelOutsideWallBuilding(List<OutsideWallBuildingsOutput> data)
|
|
{
|
|
try
|
|
{
|
|
string template_name = "外墙问卷调查模板.xlsx";
|
|
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);
|
|
ISheet sheet = null;
|
|
sheet = workbook.GetSheetAt(0);
|
|
//从第几行开始 , 比如 行号是4 , 就写3
|
|
var startRowIndex = 1;
|
|
for (int i = 0; i < data.Count; i++)
|
|
{
|
|
var c_rowindex = startRowIndex + i;
|
|
var row = sheet.CreateRow(c_rowindex);
|
|
int cell_start_index = 0;
|
|
row.CreateCell(cell_start_index).SetCellValue(data[i].submitCode);
|
|
row.CreateCell(cell_start_index + 1).SetCellValue(data[i].communityName);
|
|
row.CreateCell(cell_start_index + 2).SetCellValue(data[i].createtime.Value.ToString("yyyy-MM-dd HH:mm:ss"));
|
|
row.CreateCell(cell_start_index + 3).SetCellValue(data[i].BuildingName);
|
|
row.CreateCell(cell_start_index + 4).SetCellValue(data[i].Address);
|
|
row.CreateCell(cell_start_index + 5).SetCellValue(data[i].LevelCount?.ToString());
|
|
row.CreateCell(cell_start_index + 6).SetCellValue(data[i].Households?.ToString());
|
|
row.CreateCell(cell_start_index + 7).SetCellValue(data[i].BuildingUnit);
|
|
row.CreateCell(cell_start_index + 8).SetCellValue(data[i].DesingerUnit);
|
|
row.CreateCell(cell_start_index + 9).SetCellValue(data[i].ConstructionUnit);
|
|
row.CreateCell(cell_start_index + 10).SetCellValue(data[i].MonitorUnit);
|
|
row.CreateCell(cell_start_index + 11).SetCellValue(data[i].WuYeUnit);
|
|
row.CreateCell(cell_start_index + 12).SetCellValue(data[i].curwallproblems);
|
|
row.CreateCell(cell_start_index + 13).SetCellValue(data[i].curwallproblemother);
|
|
row.CreateCell(cell_start_index + 14).SetCellValue(data[i].wallproblemsfirst);
|
|
row.CreateCell(cell_start_index + 15).SetCellValue(data[i].firstproblemdate);
|
|
row.CreateCell(cell_start_index + 16).SetCellValue(data[i].problemfrequency);
|
|
row.CreateCell(cell_start_index + 17).SetCellValue(data[i].problemseason);
|
|
row.CreateCell(cell_start_index + 18).SetCellValue(data[i].wallproblemtoward);
|
|
row.CreateCell(cell_start_index + 19).SetCellValue(data[i].problemfanwei);
|
|
row.CreateCell(cell_start_index + 20).SetCellValue(data[i].problemheight);
|
|
row.CreateCell(cell_start_index + 21).SetCellValue(data[i].diaoluowu);
|
|
}
|
|
var file = new FileInfo(excelFilePath);
|
|
var savePath = file.DirectoryName + "\\OutPut\\";
|
|
if (!Directory.Exists(savePath))
|
|
Directory.CreateDirectory(savePath);
|
|
outputPath = savePath + DateTime.Now.ToString("yyyyMMddHHmmsss") + "-" + template_name;
|
|
using (var filess = new FileStream(outputPath, FileMode.Create, FileAccess.Write, FileShare.Read))
|
|
{
|
|
workbook.Write(filess, true);
|
|
}
|
|
}
|
|
}
|
|
return outputPath;
|
|
}
|
|
catch (System.IO.IOException ioex)
|
|
{
|
|
throw Oops.Oh("文件被占用,请检查文件模板");
|
|
}
|
|
}
|
|
|
|
|
|
}
|
|
}
|