Code Monkey home page Code Monkey logo

linqtoexcel.extend's Introduction

LinqToExcel.Extend使用

使用LinqToExcel.Extend到你的项目

NuGet

您可以使用NuGet来快速地将LinqToExcel.Extend添加到您的项目中。只需搜索LinqToExcel.Extend并安装软件包。或者使用指令 install-package LinqToExcel.Extend

LinqToExcel.Extend

LinqToExcel没有包含验证,在无法保证客户提供的Excel数据规范的前提下容易发生错误。所以对其进行扩展。主要扩展方向为验证,验证包含俩点

1.默认验证参数有效性

2.自定是逻辑有效性

本代码在LinqToExcel的基础上扩展了验证功能,实例化验证对象时,传入对应的excel路径,自动完成验证。

自动验证为参数有效性验证,用户可自定义逻辑有效性验证,作为参数实例化时传入

实例:定义验证对象

//自定义工作簿验证
WorkBookValidate workbook = new WorkBookValidate("Default.xlsx");
//验证结束返回Verification对象,对象包含 俩个属性,一个为是否验证成功,一个为验证出错的集合信息
workbook[0].StartValidate<User>();

#验证实体

public class User {

[ExcelColumn("Id")]
public string ID { get; set; }

[ExcelColumn("名称")]
public string Name { get; set; }

[ExcelColumn("年龄")]
public int Age { get; set; }

[ExcelColumn("出生日期")]
public DateTime BirthDay { get; set; }

public override string ToString()
{
    return string.Format("{0}\t{1}\t{2}", ID, Name, Age);
}

}

#示例

    /// <summary>
    /// 基础验证 通过实体特性
    /// </summary>
    public static void BasicValidate()
    {
        WorkBookValidate workbook = new WorkBookValidate("Default.xlsx");
        var errlist = workbook[0].StartValidate<User>();
        Validate(errlist);
    }

    /// <summary>
    /// 基础验证 自定义对应关系
    /// </summary>
    public static void BasicValidate2()
    {
        WorkBookValidate workbook = new WorkBookValidate("Other.xlsx");

        workbook[0].AddMapping("ID", "主键");
        var errlist = workbook[0].StartValidate<User>();
        Validate(errlist);
    }
    
    /// <summary>
    /// 带条件验证
    /// </summary>
    public static void ValidateWithCondition()
    {
        WorkBookValidate workbook = new WorkBookValidate("Default.xlsx");

        List<CellMatching<User>> rowValidate = new List<CellMatching<User>>();
        rowValidate.Add(new CellMatching<User>()
        {
            paramater = u => u.Age,
            matchCondition = u => u.Age > 10,
            errMsg = "请选择年龄大于10的人员"
        });

        var errlist = workbook[0].StartValidate<User>(rowValidate);
        Validate(errlist);
    }
    
    private static void Validate(Verification verifity)
    {
        Console.WriteLine(string.Format("是否通过{0}", verifity.IfPass));
        //出错列
        foreach (var item in verifity.ErrCells)
        {
            Console.WriteLine(item);
        }
    }

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.