WeihanLi/WeihanLi.Npoi

通过Attributes绑定excel中的列,出现与excel中列对应不上

Closed this issue · 15 comments

在1.3.6版本上可以对应,1.3.8版本上出现问题
` [SheetAttribute(SheetIndex = 0, StartRowIndex = 1)]
public class QuestionModel
{

    [Column("题目")]
    public string title { set; get; }

    [Column("难易程度")]
    public string difficult { get; set; } = "简单";

    [Column("题型")]
    public string type { get; set; } = "单选";

    [Column("分类")]
    public string category_name { get; set; }

    [Column("选项A")]
    public string option_a { get; set; }

    [Column("选项B")]
    public string option_b { get; set; }

    [Column("选项C")]
    public string option_c { get; set; }

    [Column("选项D")]
    public string option_d { get; set; }

    [Column("选项E")]
    public string option_e { get; set; }

    [Column("选项F")]
    public string option_f { get; set; }


    [Column("正确答案")]
    public string ans_right { get; set; }

    [Column("解析")]
    public string analysis { get; set; }
}`

Issue-Label Bot is automatically applying the label bug to this issue, with a confidence of 0.66. Please mark this comment with 👍 or 👎 to give our bot feedback!

Links: app homepage, dashboard and code for this bot.

可以提供一个示例吗,我的示例好像没有办法复现你的问题

你是 1.3.8 导出的 excel 吗,如果是 1.3.6 导出的文档用 1.3.8 导入可能会有问题

我是从excel导入的(数据自己在excel加的),明天我把代码整理上传下

import_question_test.zip
这是测试代码,谢谢,excel文件在bin\debug目录下

好的,谢谢,我看一下,大概知道是哪里的问题了

是我写的代码有问题吗

不是,是之前我改的问题,之前会根据 Excel 内容去调整 ColumnIndex,后来去掉了,因为可能在导出 csv 的时候,没有自动调整,在导入 excel 之后如果自动调整 ColumnIndex 的话,可能会导致导出的 csv 不能再导入了,所以把导入excel自动调整 ColumnIndex 给去掉了,最好的话可以指定 ColumnIndex,或者你感觉有别的方便处理的方式吗

修改参考:
https://github.com/WeihanLi/WeihanLi.Npoi/pull/27/files#diff-94081a94ac3ffeb1200c19563b633416L52

我使用FluentApi 来配置吧,如果excel中的标题多了个空格,指定了ColumnIndex,能匹配吗?

1.3.8 版本不会根据导入的标题来设置 ColumnIndex ,所以没有影响的,使用 Attribute 也可以 Attribute 也可以指定 ColumnIndex, 不过还是比较推荐用 FluentApi 的方式,因为不需要修改model 类,对代码没有侵入性,而且 fluentAPI 对导出有更多的支持

public class QuestionModel
    {
        [Column(IsIgnored = true)]
        public int cop_id { get; set; } = 1;

        [Column("题目", Index = 0)]
        public string title { set; get; }

        [Column("难易程度", Index = 11)]
        public string difficult { get; set; } = "简单";

        [Column("题型", Index = 1)]
        public string type { get; set; } = "单选";

        [Column("分 类", Index = 10)]
        public string category_name { get; set; }

        [Column("选项A", Index = 2)]
        public string option_a { get; set; }

        [Column("选项B", Index = 3)]
        public string option_b { get; set; }

        [Column("选项C", Index = 4)]
        public string option_c { get; set; }

        [Column("选项D", Index = 5)]
        public string option_d { get; set; }

        [Column("选项E", Index = 6)]
        public string option_e { get; set; }

        [Column("选项F", Index = 7)]
        public string option_f { get; set; }

        //[Column(IsIgnored = true)]
        //public string option_g { get; set; }

        //[Column(IsIgnored = true)]
        //public string option_h { get; set; }

        [Column("正确答案", Index = 8)]
        public string ans_right { get; set; }

        [Column("解析", Index = 9)]
        public string analysis { get; set; }
    }

@zhujinhu21 现在可以了吗,还有问题吗?

可以了,谢谢

好的

@zhujinhu21 你好,刚刚发布了 1.4.5 版本,可以不设置 ColumnIndex 也可以正常导入,重新加入了导入 excel 时根据excel文件头自动调整 ColumnIndex,调整的是一个局部变量,不会影响已有的配置,导出的时候还是按照已有的配置(没有配置的话就会用内部自动调整的配置),有需要的话可以试一下

@WeihanLi 好的,谢谢