zoukankan      html  css  js  c++  java
  • c# .Net :Excel NPOI导入导出操作教程之List集合的数据写到一个Excel文件并导出

    将List集合的数据写到一个Excel文件并导出示例:


    using NPOI.HSSF.UserModel;
    using NPOI.SS.UserModel;
    using System;
    using System.Collections.Generic;
    using System.IO;

      List<UserInfo> listUser = new List<UserInfo>()
               {
                   new UserInfo { name="1", id="1", phone="1r" },
                   new UserInfo { name="2", id="2", phone="2r" },
                   new UserInfo { name="3", id="3", phone="3r" },
                   new UserInfo { name="4", id="4", phone="4r" },
                   new UserInfo { name="5", id="5", phone="5r" },
               };
            1、//创建工作簿对象
           IWorkbook workbook = new HSSFWorkbook();
            2、//创建工作表
            ISheet sheet = workbook.CreateSheet("onesheet");
            IRow row0 = sheet.CreateRow(0);
            row0.CreateCell(0).SetCellValue("用户Id");
            row0.CreateCell(1).SetCellValue("用户名称");
            row0.CreateCell(2).SetCellValue("用户备注信息");
            for (int r = 1; r < listUser.Count; r++)
            {
                3、//创建行row
                IRow row = sheet.CreateRow(r);
                row.CreateCell(0).SetCellValue(listUser[r].id);
                row.CreateCell(1).SetCellValue(listUser[r].name);
                row.CreateCell(2).SetCellValue(listUser[r].phone);
                row.CreateCell(3).SetCellValue(listUser[r].pwd);
            }

        //创建流对象并设置存储Excel文件的路径
            using (FileStream url = File.OpenWrite(@"C:UsersAdministratorDesktop写入excel.xls"))
            {

        //导出Excel文件
                workbook.Write(url);
                Response.Write("<script>alert('写入成功!')</script>");
            };

    ——————————分享End——————————

  • 相关阅读:
    extern C的作用详解
    UIWindow in iOS
    iOS会议和组织
    Fantageek翻译系列之《使用Autolayout显示变化高度的UITableViewCell》
    KVO的概述的使用
    Struts2 基于XML校验(易百教程)
    Maven项目中添加JDBC驱动
    org.dom4j.DocumentException: null Nested exception: null解决方法
    struts2中的数据类型自动转换
    struts2的拦截器
  • 原文地址:https://www.cnblogs.com/jicheng/p/5961257.html
Copyright © 2011-2022 走看看