Comments (13)
请提供下完整的Demo(包含Excel文件)。谢谢。
from magicodes.ie.
TestMagicIE.zip
例子里可以看出Magicodes.IE至少有两个bug,我在注释里写的很详细
from magicodes.ie.
public class SalaryDTO
{
[ImporterHeader(Name = "工资月份")]
[Required]
public DateTime SalaryDate { get; set; }
/// <summary>
/// 员工姓名
/// </summary>
[ImporterHeader(Name = "员工姓名")]
[Required]
[MaxLength(50, ErrorMessage = "员工姓名字数超过最大长度50的限制")]
public string EmpName { get; set; }
/// <summary>
/// 岗级工资
/// </summary>
[ImporterHeader(Name = "岗级工资")]
[Required]
public decimal PostSalary { get; set; }
}
public class Salary
{
[Required]
public DateTime SalaryDate { get; set; }
/// <summary>
/// 员工姓名
/// </summary>
// [ImporterHeader(Name = "员工姓名")]
[Required]
[MaxLength(50, ErrorMessage = "员工姓名字数超过最大长度50的限制")]
public string EmpName { get; set; }
/// <summary>
/// 岗级工资
/// </summary>
// [ImporterHeader(Name = "岗级工资")]
[Required]
public decimal PostSalary { get; set; }
}
导出的代码很简单:
IImporter Importer = new ExcelImporter();
IExporter exporter = new ExcelExporter();
List<SalaryDTO> dtos = new List<SalaryDTO>();
dtos.Add(new SalaryDTO { EmpName = "郑是", PostSalary = 2345.13M, SalaryDate = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd")) });
dtos.Add(new SalaryDTO { EmpName = "邓五年", PostSalary = 4345.06M, SalaryDate = Convert.ToDateTime(DateTime.Now.AddMonths(-1).ToString("yyyy-MM-dd")) });
Importer.GenerateTemplate<SalaryDTO>("salarytemplate.xlsx");//这样导出标题栏已经汉化
exporter.Export("newSalary.xlsx", dtos);//这样导出表头没有汉化
得到两个excel表,其中一个salarytemplate.xlsx表头汉化了,newSalary.xlsx没有汉化。
导出数据后,我还想把它导回来,导入的代码也很简单:
IImporter Importer = new ExcelImporter();
// var result= await Importer.Import<SalaryDTO>("newSalary.xlsx");//如果用这个导入,取不到数据
var result= await Importer.Import<Salary>("newSalary.xlsx");//如果用这个导入,SalaryDate没有值,也是一个bug
bindingSource1.DataSource = result.Data;
dataGridView1.DataSource = bindingSource1;
用注释的那行代码取不到数据,因为导出的数据的类型是Salary类,但如果用Salary做为导入数据源,SalaryDate没有正确识别。
非常感谢开源Magicodes.IE,不过明显的bug请尽快更正,感谢!
from magicodes.ie.
看了下,这个不是Bug。因为导入导出的Dto使用的同一个,您只设置了导入的Header特性,并没有设置导出的Header特性。所以导出的Excel表头使用是的属性名称。
from magicodes.ie.
谢谢,还有个问题,导入的SalaryDate没有数据,请问还要怎么设置。我增加了:
[ImporterHeader(Name = "工资月份")]
[ExporterHeader(DisplayName = "工资月份",Format ="yyyy-MM-dd")]
[Required]
public DateTime SalaryDate { get; set; }
还是不起作用
from magicodes.ie.
@zsanhong 方便将结果截图么?
from magicodes.ie.
@zsanhong 见我们的单元测试:ExcelExporter_Tests 的AttrsExport_Test,相关验证逻辑一直是通过的:
[Fact(DisplayName = "DTO特性导出(测试格式化)")]
public async Task AttrsExport_Test()
{
IExporter exporter = new ExcelExporter();
var filePath = GetTestFilePath($"{nameof(AttrsExport_Test)}.xlsx");
DeleteFile(filePath);
var data = GenFu.GenFu.ListOf<ExportTestDataWithAttrs>(100);
foreach (var item in data)
{
item.LongNo = 45875266524;
}
var result = await exporter.Export(filePath, data);
result.ShouldNotBeNull();
File.Exists(filePath).ShouldBeTrue();
using (var pck = new ExcelPackage(new FileInfo(filePath)))
{
pck.Workbook.Worksheets.Count.ShouldBe(1);
var sheet = pck.Workbook.Worksheets.First();
sheet.Cells[sheet.Dimension.Address].Rows.ShouldBe(101);
sheet.Cells["A2"].Text.ShouldBe(data[0].Text);
//[ExporterHeader(DisplayName = "日期1", Format = "yyyy-MM-dd")]
sheet.Cells["E2"].Text.Equals(DateTime.Parse(sheet.Cells["E2"].Text).ToString("yyyy-MM-dd"));
//[ExporterHeader(DisplayName = "日期2", Format = "yyyy-MM-dd HH:mm:ss")]
sheet.Cells["F2"].Text.Equals(DateTime.Parse(sheet.Cells["F2"].Text).ToString("yyyy-MM-dd HH:mm:ss"));
//默认DateTime
sheet.Cells["G2"].Text.Equals(DateTime.Parse(sheet.Cells["G2"].Text).ToString("yyyy-MM-dd"));
}
}
from magicodes.ie.
麻烦看看我上传的例子为什么导入时取不到SalaryDate的值
from magicodes.ie.
根据你的代码,我这边导出正常(用的最新的2.1.4的包):
导入好像有点问题,有空了我再跟进下
from magicodes.ie.
@zsanhong 已确认日期导入这里存在一个Bug,稍后我们会修复并发布一个版本。
from magicodes.ie.
@zsanhong 该问题已在develop分支修复,对应单元测试为 ExportAndImportUseOneDto_Test 。明天我们将会基于成功的构建进行发包。非常感谢!
from magicodes.ie.
感谢你们的杰出工作,顺便说一下,我也在长沙,有机会向你们学习!
from magicodes.ie.
@zsanhong
你可以使用最新的包【2.2.0-beta2】试试。
多多交流学习。在社区群里没?
希望后续能够多多支持此项目,加入管理委员会。
from magicodes.ie.
Related Issues (20)
- 如何对ExporterAttribute特性做全局设置? HOT 1
- 目前不支持交叉表导出吗?
- magicodes.ie.excel.aspnetcore 2.7.4.5在WindowServier 2012R2下运行The type initializer for 'SkiaSharp.SKFontStyle' threw an exception. HOT 6
- 空指针异常
- 导出设置ExporterHeader的宽度没有效果 HOT 1
- Magicodes.IE.Pdf无法读取本地文件(图片) HOT 1
- Linux Dcoker构建 .NET8 按HTML模版导出PDF 报错找不到libjpeg62,同样的环境.NET7打包运行后导出PDF就正常 HOT 2
- 一个项目用Magicodes.IE跟NPOI HOT 3
- windows service部署导出word异常
- 同一个数据源,Linux 下面速度慢30s没有导出,windows下5s就导出来了。
- Magicodes.IE 和npoi冲突
- Pls remove throw new ArgumentException("Tablename has spaces") at ExcelTableCollection.ValidateTableName,xlsx is allowed worksheet name Contains empty
- Reminder:Method not found: 'System.IO.MemoryStream Microsoft.IO.RecyclableMemoryStreamManager.GetStream(System.String)'. HOT 1
- The npoi package conflicts with the Magicodes.IE package
- 模板导出,支持多层循环渲染吗?
- 动态模板导出支持导出多个sheet吗
- 数值类型导入的问题
- PDF导出报错:The value cannot be an empty string. (Parameter 'path')
- Datatable动态导出Excel,IExporterHeaderFilter设置禁用表头筛选器无效
- 导出数据没有先格式化为模板 HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from magicodes.ie.