427 lines
15 KiB
C#
427 lines
15 KiB
C#
using IRaCIS.Application.Contracts;
|
|
using IRaCIS.Application.Interfaces;
|
|
using IRaCIS.Core.API._ServiceExtensions.NewtonsoftJson;
|
|
using IRaCIS.Core.Application.Helper;
|
|
using IRaCIS.Core.Domain.Share;
|
|
using Microsoft.AspNetCore.Hosting;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.Extensions.Hosting;
|
|
using MiniExcelLibs;
|
|
using MiniExcelLibs.OpenXml;
|
|
using NPOI.SS.Formula.Functions;
|
|
using NPOI.XSSF.UserModel;
|
|
using System.Collections;
|
|
using System.Globalization;
|
|
|
|
namespace IRaCIS.Core.Application.Service;
|
|
|
|
public static class ExcelExportHelper
|
|
{
|
|
//MiniExcel_Export
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="code"></param>
|
|
/// <param name="data"></param>
|
|
/// <param name="exportFileNamePrefix">文件名前缀</param>
|
|
/// <param name="_commonDocumentRepository"></param>
|
|
/// <param name="_hostEnvironment"></param>
|
|
/// <param name="_dictionaryService"></param>
|
|
/// <param name="translateType"></param>
|
|
/// <param name="criterionType"></param>
|
|
/// <returns></returns>
|
|
public static async Task<IActionResult> DataExportAsync(string code, ExcelExportInfo data, string exportFileNamePrefix, IRepository<CommonDocument> _commonDocumentRepository, IWebHostEnvironment _hostEnvironment, IDictionaryService? _dictionaryService = null, Type? translateType = null, CriterionType? criterionType = null)
|
|
{
|
|
var isEn_US = CultureInfo.CurrentCulture.Name == StaticData.CultureInfo.en_US;
|
|
|
|
//判断是否有字典翻译
|
|
object translateData = data;
|
|
|
|
if (_dictionaryService != null && translateType != null)
|
|
{
|
|
//一个值 对应不同的字典翻译
|
|
var needTranslatePropertyList = translateType.GetProperties().Where(t => t.IsDefined(typeof(DictionaryTranslateAttribute), true))
|
|
.SelectMany(c =>
|
|
c.GetCustomAttributes(typeof(DictionaryTranslateAttribute), false).Select(f => (DictionaryTranslateAttribute?)f).Where(t => t?.CriterionType == criterionType || t?.CriterionType == null)
|
|
.Select(k => new { c.Name, k.DicParentCode, k.IsTranslateDenpendOtherProperty, k.DependPropertyName, k.DependPropertyValueStr })
|
|
).ToList();
|
|
|
|
|
|
|
|
//字典表查询出所有需要翻译的数据
|
|
|
|
var translateDataList = await _dictionaryService.GetBasicDataSelect(needTranslatePropertyList.Select(t => t.DicParentCode).Distinct().ToArray());
|
|
|
|
|
|
var dic = data.ConvertToDictionary();
|
|
|
|
|
|
foreach (var key in dic.Keys)
|
|
{
|
|
//是数组 那么找到对应的属性 进行翻译
|
|
if (dic[key].GetType().GetInterfaces().Any(i => i.IsGenericType && i.GetGenericTypeDefinition() == typeof(IList<>)))
|
|
{
|
|
|
|
var newObjList = new List<object>();
|
|
var no = 1;
|
|
|
|
foreach (var item in dic[key] as IList)
|
|
{
|
|
var itemDic = item.ConvertToDictionary();
|
|
|
|
//处理集合里面时间类型,根据当前语言将时间转变为字符串
|
|
foreach (var itemValuePair in itemDic)
|
|
{
|
|
if (DateTime.TryParse(itemValuePair.Value?.ToString(), out DateTime result))
|
|
{
|
|
itemDic[itemValuePair.Key] = ExportExcelConverterDate.DateTimeInternationalToString(result);
|
|
}
|
|
}
|
|
|
|
|
|
foreach (var needTranslateProperty in needTranslatePropertyList)
|
|
{
|
|
//翻译的属性依赖其他属性
|
|
if (needTranslateProperty.IsTranslateDenpendOtherProperty)
|
|
{
|
|
if (itemDic[needTranslateProperty.DependPropertyName]?.ToString().ToLower() == needTranslateProperty.DependPropertyValueStr.ToLower())
|
|
{
|
|
var beforeValue = itemDic[needTranslateProperty.Name]?.ToString();
|
|
|
|
itemDic[needTranslateProperty.Name] = translateDataList[needTranslateProperty.DicParentCode].Where(t => t.Code.ToLower() == beforeValue?.ToLower()).Select(t => isEn_US ? t.Value : t.ValueCN).FirstOrDefault() ?? String.Empty;
|
|
}
|
|
}
|
|
//普通翻译 或者某一标准翻译
|
|
else
|
|
{
|
|
var beforeValue = itemDic[needTranslateProperty.Name]?.ToString();
|
|
|
|
|
|
itemDic[needTranslateProperty.Name] = translateDataList[needTranslateProperty.DicParentCode].Where(t => t.Code.ToLower() == beforeValue?.ToLower()).Select(t => isEn_US ? t.Value : t.ValueCN).FirstOrDefault() ?? String.Empty;
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
itemDic.Add("No", no++);
|
|
|
|
|
|
newObjList.Add(itemDic);
|
|
}
|
|
|
|
dic[key] = newObjList;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
//data = dic;
|
|
|
|
translateData = dic;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var (physicalPath, fileName) = await FileStoreHelper.GetCommonDocPhysicalFilePathAsync(_hostEnvironment, _commonDocumentRepository, code);
|
|
|
|
|
|
//模板路径
|
|
var tplPath = physicalPath;
|
|
|
|
#region 根据中英文 删除模板sheet
|
|
|
|
// 打开模板文件
|
|
var templateFile = new FileStream(tplPath, FileMode.Open, FileAccess.Read);
|
|
|
|
// 获取文件流
|
|
var templateStream = new MemoryStream();
|
|
templateFile.CopyTo(templateStream);
|
|
templateStream.Seek(0, SeekOrigin.Begin);
|
|
|
|
var workbook = new XSSFWorkbook(templateStream);
|
|
|
|
int sheetCount = workbook.NumberOfSheets;
|
|
|
|
if (sheetCount == 2)
|
|
{
|
|
if (isEn_US)
|
|
{
|
|
workbook.RemoveSheetAt(0);
|
|
}
|
|
else
|
|
{
|
|
workbook.RemoveSheetAt(1);
|
|
}
|
|
|
|
var memoryStream2 = new MemoryStream();
|
|
workbook.Write(memoryStream2, true);
|
|
|
|
memoryStream2.Seek(0, SeekOrigin.Begin);
|
|
|
|
templateStream = memoryStream2;
|
|
}
|
|
|
|
// 文件名称 从sheet里面取
|
|
//fileNmae = workbook.GetSheetName(0);
|
|
#endregion
|
|
|
|
#region MiniExcel
|
|
|
|
var memoryStream = new MemoryStream();
|
|
|
|
var config = new OpenXmlConfiguration()
|
|
{
|
|
IgnoreTemplateParameterMissing = true,
|
|
};
|
|
|
|
await MiniExcel.SaveAsByTemplateAsync(memoryStream, templateStream.ToArray(), translateData, config);
|
|
|
|
memoryStream.Seek(0, SeekOrigin.Begin);
|
|
|
|
|
|
return new FileStreamResult(memoryStream, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
|
|
{
|
|
FileDownloadName = $"{(string.IsNullOrEmpty(exportFileNamePrefix) ? "" : exportFileNamePrefix + "_")}{Path.GetFileNameWithoutExtension(fileName)}_{DateTime.Now.ToString("yyyyMMddHHmmss")}.xlsx"
|
|
};
|
|
|
|
#endregion
|
|
|
|
}
|
|
|
|
|
|
|
|
public static async Task<(MemoryStream, string)> DataExport_NpoiTestAsync(string code, ExcelExportInfo data, IRepository<CommonDocument> _commonDocumentRepository, IWebHostEnvironment _hostEnvironment, IDictionaryService? _dictionaryService = null, Type? translateType = null, CriterionType? criterionType = null)
|
|
{
|
|
var isEn_US = CultureInfo.CurrentCulture.Name == StaticData.CultureInfo.en_US;
|
|
//判断是否有字典翻译
|
|
|
|
object translateData = data;
|
|
|
|
if (_dictionaryService != null && translateType != null)
|
|
{
|
|
|
|
//一个值 对应不同的字典翻译
|
|
var needTranslatePropertyList = translateType.GetProperties().Where(t => t.IsDefined(typeof(DictionaryTranslateAttribute), true))
|
|
.SelectMany(c =>
|
|
c.GetCustomAttributes(typeof(DictionaryTranslateAttribute), false).Select(f => (DictionaryTranslateAttribute?)f).Where(t => t.CriterionType == criterionType || t.CriterionType == null)
|
|
.Select(k => new { c.Name, k.DicParentCode, k.IsTranslateDenpendOtherProperty, k.DependPropertyName, k.DependPropertyValueStr })
|
|
).ToList();
|
|
|
|
|
|
|
|
//字典表查询出所有需要翻译的数据
|
|
|
|
var translateDataList = await _dictionaryService.GetBasicDataSelect(needTranslatePropertyList.Select(t => t.DicParentCode).Distinct().ToArray());
|
|
|
|
var dic = data.ConvertToDictionary();
|
|
//var dic = (JsonConvert.DeserializeObject<IDictionary<string, object>>(data.ToJsonNotIgnoreNull())).IfNullThrowException();
|
|
|
|
foreach (var key in dic.Keys)
|
|
{
|
|
//是数组 那么找到对应的属性 进行翻译
|
|
if (dic[key].GetType().GetInterfaces().Any(i => i.IsGenericType && i.GetGenericTypeDefinition() == typeof(IList<>)))
|
|
//if (dic[key].GetType().IsAssignableFrom(typeof(JArray)))
|
|
{
|
|
|
|
var newObjList = new List<object>();
|
|
var no = 1;
|
|
foreach (var item in dic[key] as IList)
|
|
//foreach (var item in dic[key] as JArray)
|
|
{
|
|
//var itemDic = JsonConvert.DeserializeObject<IDictionary<string, object>>(item.ToJsonNotIgnoreNull());
|
|
var itemDic = item.ConvertToDictionary();
|
|
|
|
//处理集合里面时间类型,根据当前语言将时间转变为字符串
|
|
foreach (var itemValuePair in itemDic)
|
|
{
|
|
if (DateTime.TryParse(itemValuePair.Value?.ToString(), out DateTime result))
|
|
{
|
|
itemDic[itemValuePair.Key] = ExportExcelConverterDate.DateTimeInternationalToString(result);
|
|
}
|
|
}
|
|
|
|
foreach (var needTranslateProperty in needTranslatePropertyList)
|
|
{
|
|
//翻译的属性依赖其他属性
|
|
if (needTranslateProperty.IsTranslateDenpendOtherProperty)
|
|
{
|
|
if (itemDic[needTranslateProperty.DependPropertyName]?.ToString().ToLower() == needTranslateProperty.DependPropertyValueStr.ToLower())
|
|
{
|
|
var beforeValue = itemDic[needTranslateProperty.Name]?.ToString();
|
|
|
|
itemDic[needTranslateProperty.Name] = translateDataList[needTranslateProperty.DicParentCode].Where(t => t.Code.ToLower() == beforeValue?.ToLower()).Select(t => isEn_US ? t.Value : t.ValueCN).FirstOrDefault() ?? String.Empty;
|
|
}
|
|
}
|
|
//普通翻译 或者某一标准翻译
|
|
else
|
|
{
|
|
var beforeValue = itemDic[needTranslateProperty.Name]?.ToString();
|
|
|
|
|
|
itemDic[needTranslateProperty.Name] = translateDataList[needTranslateProperty.DicParentCode].Where(t => t.Code.ToLower() == beforeValue?.ToLower()).Select(t => isEn_US ? t.Value : t.ValueCN).FirstOrDefault() ?? String.Empty;
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
itemDic.Add("No", no++);
|
|
newObjList.Add(itemDic);
|
|
}
|
|
|
|
dic[key] = newObjList;
|
|
|
|
}
|
|
}
|
|
|
|
|
|
//data = dic;
|
|
translateData = dic;
|
|
|
|
}
|
|
|
|
|
|
var (physicalPath, fileName) = await FileStoreHelper.GetCommonDocPhysicalFilePathAsync(_hostEnvironment, _commonDocumentRepository, code);
|
|
|
|
|
|
//模板路径
|
|
var tplPath = physicalPath;
|
|
|
|
#region 根据中英文 删除模板sheet
|
|
|
|
// 打开模板文件
|
|
var templateFile = new FileStream(tplPath, FileMode.Open, FileAccess.Read);
|
|
|
|
// 获取文件流
|
|
var templateStream = new MemoryStream();
|
|
templateFile.CopyTo(templateStream);
|
|
templateStream.Seek(0, SeekOrigin.Begin);
|
|
|
|
var workbook = new XSSFWorkbook(templateStream);
|
|
|
|
int sheetCount = workbook.NumberOfSheets;
|
|
|
|
if (sheetCount == 2)
|
|
{
|
|
if (isEn_US)
|
|
{
|
|
workbook.RemoveSheetAt(0);
|
|
}
|
|
else
|
|
{
|
|
workbook.RemoveSheetAt(1);
|
|
}
|
|
|
|
var memoryStream2 = new MemoryStream();
|
|
workbook.Write(memoryStream2, true);
|
|
|
|
memoryStream2.Seek(0, SeekOrigin.Begin);
|
|
|
|
templateStream = memoryStream2;
|
|
}
|
|
|
|
// 文件名称 从sheet里面取
|
|
//fileName = workbook.GetSheetName(0);
|
|
#endregion
|
|
|
|
#region MiniExcel
|
|
|
|
var memoryStream = new MemoryStream();
|
|
|
|
var config = new OpenXmlConfiguration()
|
|
{
|
|
IgnoreTemplateParameterMissing = true,
|
|
};
|
|
|
|
await MiniExcel.SaveAsByTemplateAsync(memoryStream, templateStream.ToArray(), translateData, config);
|
|
|
|
memoryStream.Seek(0, SeekOrigin.Begin);
|
|
|
|
|
|
return (memoryStream, fileName);
|
|
|
|
//var wb = new HSSFWorkbook(memoryStream);
|
|
|
|
//var sheet = wb.GetSheetAt(0);
|
|
|
|
|
|
#endregion
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// 导出文件模板
|
|
/// </summary>
|
|
/// <param name="inDto"></param>
|
|
/// <returns></returns>
|
|
public static async Task<FileResult> ExportTemplateAsync(ExportTemplateServiceDto inDto)
|
|
{
|
|
var (physicalPath, fileName) = await FileStoreHelper.GetCommonDocPhysicalFilePathAsync(inDto.hostEnvironment, inDto.commonDocumentRepository, inDto.TemplateCode);
|
|
|
|
|
|
//模板路径
|
|
var tplPath = physicalPath;
|
|
|
|
#region 根据中英文 删除模板sheet
|
|
|
|
// 打开模板文件
|
|
var templateFile = new FileStream(tplPath, FileMode.Open, FileAccess.Read);
|
|
|
|
// 获取文件流
|
|
var templateStream = new MemoryStream();
|
|
templateFile.CopyTo(templateStream);
|
|
templateStream.Seek(0, SeekOrigin.Begin);
|
|
|
|
var workbook = new XSSFWorkbook(templateStream);
|
|
|
|
int sheetCount = workbook.NumberOfSheets;
|
|
|
|
if (sheetCount == 2)
|
|
{
|
|
if (inDto.IsEnglish)
|
|
{
|
|
workbook.RemoveSheetAt(0);
|
|
}
|
|
else
|
|
{
|
|
workbook.RemoveSheetAt(1);
|
|
}
|
|
|
|
var memoryStream2 = new MemoryStream();
|
|
workbook.Write(memoryStream2, true);
|
|
|
|
memoryStream2.Seek(0, SeekOrigin.Begin);
|
|
|
|
templateStream = memoryStream2;
|
|
}
|
|
|
|
// 文件名称 从sheet里面取
|
|
//fileNmae = workbook.GetSheetName(0);
|
|
#endregion
|
|
|
|
|
|
|
|
var memoryStream = new MemoryStream();
|
|
|
|
var config = new OpenXmlConfiguration()
|
|
{
|
|
IgnoreTemplateParameterMissing = true,
|
|
};
|
|
|
|
await MiniExcel.SaveAsByTemplateAsync(memoryStream, templateStream.ToArray(), inDto.Data, config);
|
|
|
|
memoryStream.Seek(0, SeekOrigin.Begin);
|
|
|
|
|
|
return new FileStreamResult(memoryStream, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
|
|
{
|
|
FileDownloadName = $"{(string.IsNullOrEmpty(inDto.ExportFileName) ? "" : inDto.ExportFileName + "_")}{Path.GetFileNameWithoutExtension(fileName)}_{DateTime.Now.ToString("yyyyMMddHHmmss")}.xlsx"
|
|
};
|
|
}
|
|
|
|
} |