当前位置: 移动技术网 > IT编程>开发语言>.net > .Net Core下基于NPOI对Excel、Word操作封装

.Net Core下基于NPOI对Excel、Word操作封装

2018年11月21日  | 移动技术网IT编程  | 我要评论

工程师职称挂靠,陈佩斯小品赶场,谁有无毒h网

框架与依赖

  • 框架:.net standard 2.0
  • 依赖:dotnetcore.npoi
    • https://github.com/dotnetcore/npoi
    • http://www.cnblogs.com/savorboard/p/netcore-npoi.html

excel导入(excelimportservice)

  • 导出模板
  • 校验数据
    • 正则表达式校验
      • 性别
      • 邮箱
      • 身份证号
      • 手机号
      • 车牌号
      • 非空
    • 数据库存在校验
    • 数值区间
    • 字符串长度
    • 日期
    • 重复数据
  • 数据转换

demo

public class importcar 
{
    [colname("车牌号")]
    [regex(regexconstant.not_empty_regex,errormsg ="必填")]
    [databaseexist("cartable","carcode")]
    [regex(regexconstant.car_code_regex)]
    [duplication]
    public string carcode { get; set; }

    [colname("手机号")]
    [regex(regexenum.国内手机号)]
    public string mobile { get; set; }

    [colname("身份证号")]
    [regex(regexconstant.identity_number_regex)]
    public string identitynumber { get; set; }

    [colname("姓名")]
    [maxlength(10)]
    public string name { get; set; }

    [colname("性别")]
    [regex(regexconstant.gender_regex)]
    public genderenum gender { get; set; }

    [colname("注册日期")]
    [datetime]
    public datetime registerdate { get; set; }

    [colname("年龄")]
    [range(0, 150)]
    public int age { get; set; }
}
//校验excel数据
list<exceldatarow> rows = excelimportservice.import<importcar>(fileurl, delegatenotexistindatabase);

//错误信息入库
rows.where(r => !r.isvalid).tolist().foreach(
    r =>
        {
            inserterrorlog($"第{r.rowindex}行,{r.errormsg}");
        }
    );

//正确数据转换为指定类型
list<importcar> importcars  = rows.where(r => r.isvalid).fastconvert<importcar>().tolist();

//正确数据入库
insertcorrectdata(importcars);

excel导出(excelexportservice)

  • 导出excel
  • 标记样式
    • 表头(字体、加粗、字号、颜色)
    • 列合并单元格
    • 自适应宽高
    • 自动换行

demo

 [wraptext]
    [header(color =colorenum.red,fontname ="微软雅黑",fontsize =12,isbold =true)]
    public class exportcar 
    {
        [mergecols]
        [colname("车牌号")]
        public string carcode { get; set; }

        [colname("姓名")]
        public string name { get; set; }

        [colname("性别")]
        public genderenum gender { get; set; }

        [colname("注册日期")]
        public datetime registerdate { get; set; }

        [colname("年龄")]
        public int age { get; set; }
    }
list<exportcar> list;

//业务操作,为list 赋值...
...

//导出
iworkbook wk = excelexportservice.export(list);

//为iworkbook提供了转换为byte数组的扩展方法
file.writeallbytes(@"c:\test\test.xls", wk.tobytes());

导出效果

word生成(wordexportservice)

  • 根据模板生成word
    • 插入文本
    • 插入图片

demo

制作word模板

定义类

public class wordcar
{
    [placeholder(placeholderenum.a)]
    public string ownername { get; set; }

    [placeholder(placeholderenum.b)]
    public string cartype { get; set; }

    //图片占位的属性类型必须为list<string>,存放图片的绝对全地址
    [pictureplaceholder(placeholderenum.c,"车辆照片")]
    public list<string> carpictures { get; set; }

    [pictureplaceholder(placeholderenum.d,"车辆证件")]
    public list<string> carlicense { get; set; }
}

导出

string curdir = environment.currentdirectory;
    string pic1 = path.combine(curdir, "files", "1.jpg");
    string pic2 = path.combine(curdir, "files", "2.jpg");
    string pic3 = path.combine(curdir, "files", "3.jpg");
    string templateurl = path.combine(curdir, "files", "carword.docx");

    wordcar car = new wordcar()
    {
        ownername = "张三丰",
        cartype = "豪华型宾利",
        carpictures = new list<string> { pic1, pic2 },
        carlicense = new list<string> { pic3 }
    };

    xwpfdocument doc = wordexportservice.exportfromtemplate(templateurl, car);

    ///为xwpfdocument提供了转换为byte数组的扩展方法
    file.writeallbytes(@"c:\test\test.docx", doc.tobytes());

导出效果

性能测试

excel导入

  • 5000条数据读取和校验(首次缓存之后,700毫秒左右,校验使用了缓存提高性能)
  • 4992条有效数据的转换(80毫秒,数据转换使用了表达式树生成委托,达到了接近硬编码的性能)

github地址:
https://github.com/holdengong/ade.officeservice

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网