mirror of
https://gitee.com/dotnetchina/MiniExcel.git
synced 2024-11-29 18:38:08 +08:00
[New] SaveAsByTemplate support DateTime custom format #255
This commit is contained in:
parent
dd2ccf0163
commit
ab3f27434b
@ -1182,7 +1182,7 @@ private IEnumerable<Dictionary<int, object>> ConvertToIntIndexRows(IEnumerable<o
|
||||
|
||||
Because MiniExcel uses a logic similar to JSON.NET to dynamically get type from values to simplify API operations, type cannot be knew without data. You can check [issue #133](https://github.com/shps951023/MiniExcel/issues/133) for understanding.
|
||||
|
||||
![image](https://user-images.githubusercontent.com/12729184/122639359-02c28200-d12c-11eb-9bd4-1d2971def927.png)
|
||||
![image](https://user-images.githubusercontent.com/12729184/122639771-546c0c00-d12e-11eb-800c-498db27889ca.png)
|
||||
|
||||
|
||||
|
||||
|
@ -1179,7 +1179,7 @@ private IEnumerable<Dictionary<int, object>> ConvertToIntIndexRows(IEnumerable<o
|
||||
|
||||
因为 MiniExcel 使用类似 JSON.NET 动态从值获取类别机制简化 API 操作,没有数据就无法获取类别。可以查看[ issue #133](https://github.com/shps951023/MiniExcel/issues/133) 了解。
|
||||
|
||||
![image](https://user-images.githubusercontent.com/12729184/122639359-02c28200-d12c-11eb-9bd4-1d2971def927.png)
|
||||
![image](https://user-images.githubusercontent.com/12729184/122639771-546c0c00-d12e-11eb-800c-498db27889ca.png)
|
||||
|
||||
|
||||
|
||||
|
@ -1190,7 +1190,7 @@ private IEnumerable<Dictionary<int, object>> ConvertToIntIndexRows(IEnumerable<o
|
||||
|
||||
因為 MiniExcel 使用類似 JSON.NET 動態從值獲取類別機制簡化 API 操作,沒有數據就無法獲取類別。可以查看[ issue #133](https://github.com/shps951023/MiniExcel/issues/133) 了解。
|
||||
|
||||
![image](https://user-images.githubusercontent.com/12729184/122639359-02c28200-d12c-11eb-9bd4-1d2971def927.png)
|
||||
![image](https://user-images.githubusercontent.com/12729184/122639771-546c0c00-d12e-11eb-800c-498db27889ca.png)
|
||||
|
||||
|
||||
|
||||
|
@ -16,6 +16,9 @@
|
||||
|
||||
---
|
||||
|
||||
### 0.16.1
|
||||
- [New] SaveAsByTemplate support DateTime custom format #255, contributor: [网虫 (landde) - Gitee.com](https://gitee.com/landde)
|
||||
|
||||
### 0.16.0
|
||||
- [New] Query support custom datetime format mapping datetime type #256
|
||||
- [Bug] Fix Query call convertValueByStyleFormat method repeatedly, cause waste of resources #259
|
||||
|
@ -17,6 +17,9 @@
|
||||
|
||||
---
|
||||
|
||||
### 0.16.1
|
||||
- [New] SaveAsByTemplate 支持 DateTime 自定义格式 #255, contributor: [网虫 (landde) - Gitee.com](https://gitee.com/landde)
|
||||
|
||||
### 0.16.0
|
||||
- [New] Query 支持自定义日期格式转成 datetime 型别 #256
|
||||
- [Bug] 修正 Query 重复呼叫 convertValueByStyleFormat 方法造成资源浪费 #259
|
||||
|
@ -17,6 +17,9 @@
|
||||
|
||||
---
|
||||
|
||||
### 0.16.1
|
||||
- [New] SaveAsByTemplate 支持 DateTime 自定義格式 #255, contributor: [网虫 (landde) - Gitee.com](https://gitee.com/landde)
|
||||
|
||||
### 0.16.0
|
||||
- [New] Query 支持自定義日期格式轉成 datetime 型別 #256
|
||||
- [Bug] 修正 Query 重複呼叫 convertValueByStyleFormat 方法造成資源浪費 #259
|
||||
|
BIN
samples/xlsx/TestsIssue255_Template.xlsx
Normal file
BIN
samples/xlsx/TestsIssue255_Template.xlsx
Normal file
Binary file not shown.
@ -1,4 +1,5 @@
|
||||
using MiniExcelLibs.Utils;
|
||||
using MiniExcelLibs.Attributes;
|
||||
using MiniExcelLibs.Utils;
|
||||
using MiniExcelLibs.Zip;
|
||||
using System;
|
||||
using System.Collections;
|
||||
@ -193,8 +194,7 @@ namespace MiniExcelLibs.OpenXml
|
||||
}
|
||||
else if (type == typeof(DateTime))
|
||||
{
|
||||
//c.SetAttribute("t", "d");
|
||||
cellValueStr = ((DateTime)cellValue).ToString("yyyy-MM-dd HH:mm:ss");
|
||||
cellValueStr = ConvertToDateTimeString(propInfo, cellValue);
|
||||
}
|
||||
|
||||
//TODO: ![image](https://user-images.githubusercontent.com/12729184/114848248-17735880-9e11-11eb-8258-63266bda0a1a.png)
|
||||
@ -229,8 +229,7 @@ namespace MiniExcelLibs.OpenXml
|
||||
}
|
||||
else if (type == typeof(DateTime))
|
||||
{
|
||||
//c.SetAttribute("t", "d");
|
||||
cellValueStr = ((DateTime)cellValue).ToString("yyyy-MM-dd HH:mm:ss");
|
||||
cellValueStr = ConvertToDateTimeString(propInfo, cellValue);
|
||||
}
|
||||
|
||||
//TODO: ![image](https://user-images.githubusercontent.com/12729184/114848248-17735880-9e11-11eb-8258-63266bda0a1a.png)
|
||||
@ -266,8 +265,7 @@ namespace MiniExcelLibs.OpenXml
|
||||
}
|
||||
else if (type == typeof(DateTime))
|
||||
{
|
||||
//c.SetAttribute("t", "d");
|
||||
cellValueStr = ((DateTime)cellValue).ToString("yyyy-MM-dd HH:mm:ss");
|
||||
cellValueStr = ConvertToDateTimeString(propInfo, cellValue);
|
||||
}
|
||||
|
||||
//TODO: ![image](https://user-images.githubusercontent.com/12729184/114848248-17735880-9e11-11eb-8258-63266bda0a1a.png)
|
||||
@ -366,6 +364,18 @@ namespace MiniExcelLibs.OpenXml
|
||||
}
|
||||
}
|
||||
|
||||
private static string ConvertToDateTimeString(KeyValuePair<string, PropInfo> propInfo, object cellValue)
|
||||
{
|
||||
string cellValueStr;
|
||||
//TODO:c.SetAttribute("t", "d"); and custom format
|
||||
var efa = propInfo.Value.PropertyInfo.GetCustomAttribute(typeof(ExcelFormatAttribute)) as ExcelFormatAttribute;
|
||||
if (efa == null)
|
||||
cellValueStr = (cellValue as DateTime?)?.ToString("yyyy-MM-dd HH:mm:ss");
|
||||
else
|
||||
cellValueStr = (cellValue as DateTime?)?.ToString(efa.Format);
|
||||
return cellValueStr;
|
||||
}
|
||||
|
||||
private static string CleanXml(string xml, string endPrefix)
|
||||
{
|
||||
//TODO: need to optimize
|
||||
|
@ -26,6 +26,47 @@ namespace MiniExcelLibs.Tests
|
||||
this.output = output;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// [SaveAsByTemplate support DateTime custom format · Issue #255 · shps951023/MiniExcel]
|
||||
/// (https://github.com/shps951023/MiniExcel/issues/255)
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void Issue255()
|
||||
{
|
||||
//tempalte
|
||||
{
|
||||
var templatePath = PathHelper.GetSamplePath("xlsx/TestsIssue255_Template.xlsx");
|
||||
var path = PathHelper.GetTempPath();
|
||||
var value = new
|
||||
{
|
||||
Issue255DTO = new[] {
|
||||
new Issue255DTO { Time = new DateTime(2021, 01, 01) } ,
|
||||
null
|
||||
}
|
||||
};
|
||||
MiniExcel.SaveAsByTemplate(path, templatePath, value);
|
||||
var rows = MiniExcel.Query(path).ToList();
|
||||
Assert.Equal("2021", rows[1].A.ToString());
|
||||
}
|
||||
//saveas
|
||||
{
|
||||
var path = PathHelper.GetTempPath();
|
||||
var value = new[] {
|
||||
new Issue255DTO { Time = new DateTime(2021, 01, 01) } ,
|
||||
null
|
||||
};
|
||||
MiniExcel.SaveAs(path, value);
|
||||
var rows = MiniExcel.Query(path).ToList();
|
||||
Assert.Equal("2021", rows[1].A.ToString());
|
||||
}
|
||||
}
|
||||
|
||||
public class Issue255DTO
|
||||
{
|
||||
[ExcelFormat("yyyy")]
|
||||
public DateTime Time { get; set; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// [Dynamic Query custom format not using mapping format · Issue #256]
|
||||
/// (https://github.com/shps951023/MiniExcel/issues/256)
|
||||
|
Loading…
Reference in New Issue
Block a user