星空网 > 软件开发 > 数据库

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

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

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

public partial class Excel操作 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void insert_Click(object sender, EventArgs e)
    {

  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:\Users\Administrator\Desktop\写入excel.xls"))
        {

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

}

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




原标题:c# .Net :Excel NPOI导入导出操作教程之List集合的数据写到一个Excel文件并导出

关键词:.NET

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

如何制作你的亚马逊“反链”:https://www.ikjzd.com/articles/136002
Wish平台黑五盘点:车载小冰箱 摄像头产品销量涨60倍:https://www.ikjzd.com/articles/136003
卖家零距离:深度对话马来本土品牌大卖郑总:https://www.ikjzd.com/articles/136004
出售老Faceboook账号的国外网站分享:https://www.ikjzd.com/articles/136005
Wish平台黑五盘点:这些产品销量能涨60倍!:https://www.ikjzd.com/articles/136007
产品图常犯的5大错误,亚马逊卖家千万别踩雷!:https://www.ikjzd.com/articles/136008
德国有那些品牌公司:https://www.vstour.cn/a/408236.html
黔南瓮安AAA以上旅游景点 黔南瓮安aaa以上旅游景点:https://www.vstour.cn/a/408237.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流