Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

导出数据的数据头没有汉化 #53

Closed
zsanhong opened this issue Mar 17, 2020 · 13 comments
Closed

导出数据的数据头没有汉化 #53

zsanhong opened this issue Mar 17, 2020 · 13 comments
Assignees
Labels
bug Something isn't working
Milestone

Comments

@zsanhong
Copy link

zsanhong commented Mar 17, 2020

Importer.GenerateTemplate<ImportMonthSalaryDto>("salarytemplate.xlsx");

这样到出模板数据头汉化了

但是如果导出的数据,excel数据头没有汉化

IImporter Importer = new ExcelImporter();
 var result = await exporter.Export("newSalary.xlsx", dtos);

其中 dtos是ImportMonthSalaryDto的集合
image

@xin-lai
Copy link
Collaborator

xin-lai commented Mar 17, 2020

请提供下完整的Demo(包含Excel文件)。谢谢。

@zsanhong
Copy link
Author

TestMagicIE.zip
例子里可以看出Magicodes.IE至少有两个bug,我在注释里写的很详细

@zsanhong
Copy link
Author

  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请尽快更正,感谢!

@xin-lai xin-lai self-assigned this Mar 18, 2020
@xin-lai
Copy link
Collaborator

xin-lai commented Mar 18, 2020

看了下,这个不是Bug。因为导入导出的Dto使用的同一个,您只设置了导入的Header特性,并没有设置导出的Header特性。所以导出的Excel表头使用是的属性名称。

@xin-lai xin-lai added the wontfix This will not be worked on label Mar 18, 2020
@zsanhong
Copy link
Author

谢谢,还有个问题,导入的SalaryDate没有数据,请问还要怎么设置。我增加了:

  [ImporterHeader(Name = "工资月份")]
        [ExporterHeader(DisplayName = "工资月份",Format ="yyyy-MM-dd")]
        [Required]
        public DateTime SalaryDate { get; set; }

还是不起作用

@xin-lai
Copy link
Collaborator

xin-lai commented Mar 19, 2020

@zsanhong 方便将结果截图么?

@xin-lai
Copy link
Collaborator

xin-lai commented Mar 19, 2020

@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"));

            }
        }

@zsanhong
Copy link
Author

image

麻烦看看我上传的例子为什么导入时取不到SalaryDate的值

TestMagicIE.zip

@xin-lai
Copy link
Collaborator

xin-lai commented Mar 19, 2020

根据你的代码,我这边导出正常(用的最新的2.1.4的包):
image
导入好像有点问题,有空了我再跟进下

@xin-lai
Copy link
Collaborator

xin-lai commented Mar 19, 2020

@zsanhong 已确认日期导入这里存在一个Bug,稍后我们会修复并发布一个版本。

@xin-lai
Copy link
Collaborator

xin-lai commented Mar 19, 2020

@zsanhong 该问题已在develop分支修复,对应单元测试为 ExportAndImportUseOneDto_Test 。明天我们将会基于成功的构建进行发包。非常感谢!

@xin-lai xin-lai added this to the 2.2 milestone Mar 19, 2020
xin-lai pushed a commit that referenced this issue Mar 19, 2020
- **【Nuget】版本更新到2.2.0-beta2**
- **【Excel导入】修复日期格式的导入Bug,支持DateTime和DateTimeOffset以及可为空类型,默认支持本地化时间格式(默认根据地区自动使用本地日期时间格式)**
- **【Excel导入导出】添加单元测试ExportAndImportUseOneDto_Test,对使用同一个Dto导出并导入进行测试。Issue见 [#53](#53
@zsanhong
Copy link
Author

感谢你们的杰出工作,顺便说一下,我也在长沙,有机会向你们学习!

@xin-lai
Copy link
Collaborator

xin-lai commented Mar 20, 2020

@zsanhong
你可以使用最新的包【2.2.0-beta2】试试。
多多交流学习。在社区群里没?
希望后续能够多多支持此项目,加入管理委员会。

@xin-lai xin-lai added bug Something isn't working and removed wontfix This will not be worked on labels Mar 20, 2020
@xin-lai xin-lai closed this as completed Mar 22, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants