当前位置: 移动技术网 > IT编程>开发语言>.net > NET npoi 保存文件

NET npoi 保存文件

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

最新97ai蜜桃,梵林伽晴天团队娟子,guodexiu

npoi完整代码:net npoi帮助类

 

        public static void datatabletoexcel(list<datatable> datatables, string filepath)
        {
            if (datatables == null || !datatables.any())
                throw new exception("datatables不能为null");
            bool isoldthan2007 = path.getextension(filepath)?.tolower() == ".xls";
            iworkbook book = datatables.toworkbook(isoldthan2007);
            using (filestream fs = new filestream(filepath, filemode.create, fileaccess.write))
            {
                book.write(fs);
            }
        }

 

        public static iworkbook toworkbook(this list<datatable> datatables, bool isoldthan2007)
        {
            iworkbook book = isoldthan2007 ? new hssfworkbook() : (iworkbook)new xssfworkbook();
            foreach (var datatable in datatables)
            {
                if (datatable == null)
                    continue;
                isheet sheet = book.createsheet(datatable.tablename);
                irow headerrow = sheet.createrow(0);
                foreach (datacolumn column in datatable.columns)
                {
                    headerrow.createcell(column.ordinal).setcellvalue(column.columnname);
                }

                for (int i = 0; i < datatable.rows.count; i++)
                {
                    datarow row = datatable.rows[i];
                    irow datarow = sheet.createrow(i + 1);
                    for (int j = 0; j < datatable.columns.count; j++)
                    {
                        datarow.createcell(j).setcellvalue(row[j]?.tostring());
                    }
                }
            }
            return book;
        }

 

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

相关文章:

验证码:
移动技术网