当前位置: 移动技术网 > IT编程>开发语言>c# > 基于.net EF6 MVC5+WEB Api 的Web系统框架总结(4)-Excel文件读、写操作

基于.net EF6 MVC5+WEB Api 的Web系统框架总结(4)-Excel文件读、写操作

2019年09月22日  | 移动技术网IT编程  | 我要评论
Excel文件读、写可以使用Office自带的库(Microsoft.Office.Interop.Excel),前提是本机须安装office才能运行,且不同的office版本之间可能会有兼容问题。还可以使用NPOI,在不安装office的时候也是可以读写的,速度很快。当然,还有支持Excel200 ...

  excel文件读、写可以使用office自带的库(microsoft.office.interop.excel),前提是本机须安装office才能运行,且不同的office版本之间可能会有兼容问题。
还可以使用npoi,在不安装office的时候也是可以读写的,速度很快。当然,还有支持excel2007以上版本的openxml。以及其他一些开源项目,由于本人不太熟悉,在此不做过多介绍。
  为了解决烦人office版本兼容问题,本项目-shiquan.offices 决定采用基于npoi,进行excel文件读、写操作。
  本项目目前实现内容包括:
  1、直接根据datatable,生成excel文件内容。
  2、根据列表设置和datatable,生成excel文件内容。
  3、根据excel模板,使用模板设置,生成excel文件内容。
  4、读取excel内容,返回datatable。

  本项目基于lgpl3.0开源,供大家参考、使用,欢迎提供宝贵意见!
  项目源码:https://gitee.com/shiquansoft/shiquan.offices

 

  下面介绍分享如何使用该项目-shiquan.offices。
  首先,创建consoleapp测试项目,添加程序包安装:

     

    1、先我们试下,普通的datatable生成excel文件是怎样的?

try
            {
                //取出数据源
                datatable dtdata = new datatable();
                dtdata.columns.add("account");
                dtdata.columns.add("realname");
                dtdata.columns.add("birthday");
                dtdata.columns.add("description");
                dtdata.columns.add("remark");

                datarow dr = dtdata.newrow();
                dr["account"] = "账户";
                dr["realname"] = "姓名";
                dr["birthday"] = "2000-05-25";
                dr["description"] = "说明";
                dr["remark"] = "说明";/*多余字段,不会数据导出*/
                dtdata.rows.add(dr);
                console.writeline("生成2007以上版本excel...");
                npoihelper.savetoexcel(dtdata, "xssfworkbook.xlsx");
                console.writeline("生成2003版本excel...");
                npoihelper.savetoexcel(dtdata, "hssfworkbook.xls");
            }
            catch (exception ex)
            {
                console.writeline("导出异常:" + ex.message);
            }

  运行效果:

     

  2、一般从系统中导出excel,我们都将采用中文标题,设置相应的数据列格式,对齐方式等信息?

 

try
            {
                //取出数据源
                datatable dtdata = new datatable();
                dtdata.columns.add("account");
                dtdata.columns.add("realname");
                dtdata.columns.add("gender");
                dtdata.columns.add("birthday");
                dtdata.columns.add("enabled");
                dtdata.columns.add("price");
                dtdata.columns.add("percent");
                dtdata.columns.add("money");
                dtdata.columns.add("remark");

                datarow dr = dtdata.newrow();
                dr["account"] = "账户";
                dr["realname"] = "姓名";
                dr["gender"] = "0";/*0:女*/
                dr["birthday"] = "2000-05-25";
                dr["price"] = "10.5656";
                dr["percent"] = "0.56";
                dr["money"] = "1000.56";
                dr["enabled"] = "1";/*是否启用:1:是*/
                dr["remark"] = "说明";/*多余字段,不会数据导出*/
                dtdata.rows.add(dr);
                /*设置导出格式*/
                columncollection colmodel = new columncollection();
                colmodel.add("account", "账户");
                colmodel.add("realname", "姓名");
                colmodel.add("birthday", "出生日期", typeof(datetime), "yyyy年mm月dd日");
                colmodel.add("price", "保留两位",typeof(double), "#0.00");
                colmodel.add("percent", "百分比", typeof(double), "0.00%");
                colmodel.add(new columnmodel() { name = "money", desc = "货币格式", type = typeof(double), format = "¥#,##0", textalign = columnalign.right });
                colmodel.add("gender", "性别", new dataoptions("0:女,1:男".split(',')));
                colmodel.add(new columnmodel[]
                {
                    new columnmodel() { name = "enabled", desc = "是否启用", options = new dataoptions("0:否,1:是".split(',')), textalign = columnalign.center }
                });

                console.writeline("保存excel...");
                //调用导出方法
                npoihelper.savetoexcel(dtdata,colmodel, "用户信息.xls");
                console.writeline("excel保存成功!");
            }
            catch (exception ex)
            {
                console.writeline("导出异常:" + ex.message);
            }

 

  运行效果:

       

  3、当我们使用普通的设置数据列格式,还不能满足要求时,此时,只能采用预定的excel模板,使用数据填充方式,生成excel文件?

var filename = "根据模板生成excel.xlsx";
            try
            {
                var templatefile = "templatefile.xlsx";
                system.io.file.writeallbytes(templatefile, consoleapp.properties.resources.template);

                list<templatemodel> list = new list<templatemodel>();
                list.add(new templatemodel() { rowindex = 1, colindex = 0, value = "账号" });
                list.add(new templatemodel() { rowindex = 1, colindex = 1, value = "名称" });
                list.add(new templatemodel() { rowindex = 1, colindex = 2, value = "2000-09-01" });
                list.add(new templatemodel() { rowindex = 1, colindex = 3, value = "备注" });

                list.add(new templatemodel() { rowindex = 2, colindex = 0, value = "账号" });
                list.add(new templatemodel() { rowindex = 3, colindex = 1, value = "名称" });
                list.add(new templatemodel() { rowindex = 4, colindex = 2, value = "2000-09-01" });
                list.add(new templatemodel() { rowindex = 5, colindex = 3, value = "备注" });
                console.writeline("根据模板生成excel...");
                npoihelper.savetoexcel(templatefile,"sheet1", list, filename);
                console.writeline("excel保存成功!");
            }
            catch (exception ex)
            {
                console.writeline("生成异常:" + ex.message);
            }

  定义的模板:

  

       运行效果:

  

  4、生成excel文件方法介绍完毕,下面我们看下怎么读取excel文件内容,我们以直接读取模板生成的excel文件为例?

try
            {
                
                console.writeline("读取excel内容...");
                datatable dtdata = npoihelper.readtotable(filename);
                stringbuilder filecontent = new stringbuilder();
                //标题
                foreach (datacolumn dc in dtdata.columns)
                {
                    filecontent.append(dc.columnname + "\t");
                }
                filecontent.appendline();

                foreach (datarow dr in dtdata.rows)
                {
                    foreach (datacolumn dc in dtdata.columns)
                    {
                        if (dr[dc.columnname] == dbnull.value)
                            filecontent.append("\t");
                        else
                            filecontent.append(dr[dc.columnname].tostring() + "\t");
                    }
                    filecontent.appendline();
                }
                console.writeline("文件内容:");
                console.writeline(filecontent.tostring());
            }
            catch (exception ex)
            {
                console.writeline("生成异常:" + ex.message);
            }

 

  运行效果:

  
  

至此 shiquan.offices excel文件读、写操作项目介绍分享完毕,欲听更多精彩,且听下回分解!

 

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网