解决中英文环境时间格式化问题
continuous-integration/drone/push Build is passing

This commit is contained in:
2024-08-26 12:28:09 +09:00
parent e42c7407e3
commit fbc7513f19
5 changed files with 123 additions and 133 deletions
@@ -3,6 +3,7 @@ using DocumentFormat.OpenXml.Spreadsheet;
using DocumentFormat.OpenXml.Wordprocessing;
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;
@@ -14,6 +15,8 @@ using Newtonsoft.Json.Linq;
using NPOI.HSSF.UserModel;
using NPOI.XSSF.UserModel;
using System.Collections;
using System.ComponentModel;
using System.Globalization;
using System.IO;
namespace IRaCIS.Core.Application.Service;
@@ -23,19 +26,13 @@ public static class ExcelExportHelper
//MiniExcel_Export
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))
// .Select(c => new { c.Name, DicParentCode = ((DictionaryTranslateAttribute?)c.GetCustomAttributes(typeof(DictionaryTranslateAttribute), false)[0])?.DicParentCode }).ToList();
//一个值 对应不同的字典翻译
var needTranslatePropertyList = translateType.GetProperties().Where(t => t.IsDefined(typeof(DictionaryTranslateAttribute), true))
.SelectMany(c =>
@@ -62,12 +59,18 @@ public static class ExcelExportHelper
var newObjList = new List<object>();
var no = 1;
foreach (var item in dic[key] as IList )
foreach (var item in dic[key] as IList)
{
//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)
@@ -79,7 +82,7 @@ public static class ExcelExportHelper
{
var beforeValue = itemDic[needTranslateProperty.Name]?.ToString();
itemDic[needTranslateProperty.Name] = translateDataList[needTranslateProperty.DicParentCode].Where(t => t.Code.ToLower() == beforeValue?.ToLower()).Select(t=>data.IsEn_US?t.Value:t.ValueCN).FirstOrDefault() ?? String.Empty;
itemDic[needTranslateProperty.Name] = translateDataList[needTranslateProperty.DicParentCode].Where(t => t.Code.ToLower() == beforeValue?.ToLower()).Select(t => isEn_US ? t.Value : t.ValueCN).FirstOrDefault() ?? String.Empty;
}
}
//普通翻译 或者某一标准翻译
@@ -88,7 +91,7 @@ public static class ExcelExportHelper
var beforeValue = itemDic[needTranslateProperty.Name]?.ToString();
itemDic[needTranslateProperty.Name] = translateDataList[needTranslateProperty.DicParentCode].Where(t => t.Code.ToLower() == beforeValue?.ToLower()).Select(t => data.IsEn_US ? t.Value : t.ValueCN).FirstOrDefault() ?? String.Empty;
itemDic[needTranslateProperty.Name] = translateDataList[needTranslateProperty.DicParentCode].Where(t => t.Code.ToLower() == beforeValue?.ToLower()).Select(t => isEn_US ? t.Value : t.ValueCN).FirstOrDefault() ?? String.Empty;
}
@@ -104,6 +107,7 @@ public static class ExcelExportHelper
dic[key] = newObjList;
}
}
@@ -139,7 +143,7 @@ public static class ExcelExportHelper
if (sheetCount == 2)
{
if (data.IsEn_US)
if (isEn_US)
{
workbook.RemoveSheetAt(0);
}
@@ -149,7 +153,7 @@ public static class ExcelExportHelper
}
var memoryStream2 = new MemoryStream();
workbook.Write(memoryStream2,true);
workbook.Write(memoryStream2, true);
memoryStream2.Seek(0, SeekOrigin.Begin);
@@ -160,14 +164,9 @@ public static class ExcelExportHelper
//fileNmae = workbook.GetSheetName(0);
#endregion
#region MiniExcel
var memoryStream = new MemoryStream();
var memoryStream = new MemoryStream();
var config = new OpenXmlConfiguration()
{
@@ -181,29 +180,18 @@ public static class ExcelExportHelper
return new FileStreamResult(memoryStream, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
{
FileDownloadName = $"{(string.IsNullOrEmpty(exportFileNamePrefix) ? "" : exportFileNamePrefix+ "_") }{Path.GetFileNameWithoutExtension(fileName) }_{DateTime.Now.ToString("yyyyMMddHHmmss")}.xlsx"
FileDownloadName = $"{(string.IsNullOrEmpty(exportFileNamePrefix) ? "" : exportFileNamePrefix + "_")}{Path.GetFileNameWithoutExtension(fileName)}_{DateTime.Now.ToString("yyyyMMddHHmmss")}.xlsx"
};
#endregion
#region Magicodes
////创建Excel导出对象
//IExportFileByTemplate exporter = new ExcelExporter();
////根据模板导出
//var result = await exporter.ExportBytesByTemplate(exportInfo, tplPath);
//return new XlsxFileResult(bytes: result, fileDownloadName: $"{doc.Name}_{DateTime.Now.ToString("yyyy-MM-dd:hh:mm:ss")}.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;
@@ -211,11 +199,6 @@ public static class ExcelExportHelper
if (_dictionaryService != null && translateType != null)
{
////只标注单个的时候
//var needTranslatePropertyList = translateType.GetProperties().Where(t => t.IsDefined(typeof(DictionaryTranslateAttribute), true))
// .Select(c => new { c.Name, DicParentCode = ((DictionaryTranslateAttribute?)c.GetCustomAttributes(typeof(DictionaryTranslateAttribute), false)[0])?.DicParentCode }).ToList();
//一个值 对应不同的字典翻译
var needTranslatePropertyList = translateType.GetProperties().Where(t => t.IsDefined(typeof(DictionaryTranslateAttribute), true))
.SelectMany(c =>
@@ -247,6 +230,15 @@ public static class ExcelExportHelper
//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)
{
//翻译的属性依赖其他属性
@@ -256,7 +248,7 @@ public static class ExcelExportHelper
{
var beforeValue = itemDic[needTranslateProperty.Name]?.ToString();
itemDic[needTranslateProperty.Name] = translateDataList[needTranslateProperty.DicParentCode].Where(t => t.Code.ToLower() == beforeValue?.ToLower()).Select(t => data.IsEn_US ? t.Value : t.ValueCN).FirstOrDefault() ?? String.Empty;
itemDic[needTranslateProperty.Name] = translateDataList[needTranslateProperty.DicParentCode].Where(t => t.Code.ToLower() == beforeValue?.ToLower()).Select(t => isEn_US ? t.Value : t.ValueCN).FirstOrDefault() ?? String.Empty;
}
}
//普通翻译 或者某一标准翻译
@@ -265,7 +257,7 @@ public static class ExcelExportHelper
var beforeValue = itemDic[needTranslateProperty.Name]?.ToString();
itemDic[needTranslateProperty.Name] = translateDataList[needTranslateProperty.DicParentCode].Where(t => t.Code.ToLower() == beforeValue?.ToLower()).Select(t => data.IsEn_US ? t.Value : t.ValueCN).FirstOrDefault() ?? String.Empty;
itemDic[needTranslateProperty.Name] = translateDataList[needTranslateProperty.DicParentCode].Where(t => t.Code.ToLower() == beforeValue?.ToLower()).Select(t => isEn_US ? t.Value : t.ValueCN).FirstOrDefault() ?? String.Empty;
}
@@ -310,7 +302,7 @@ public static class ExcelExportHelper
if (sheetCount == 2)
{
if (data.IsEn_US)
if (isEn_US)
{
workbook.RemoveSheetAt(0);
}
@@ -320,7 +312,7 @@ public static class ExcelExportHelper
}
var memoryStream2 = new MemoryStream();
workbook.Write(memoryStream2,true);
workbook.Write(memoryStream2, true);
memoryStream2.Seek(0, SeekOrigin.Begin);
@@ -355,4 +347,7 @@ public static class ExcelExportHelper
#endregion
}
}