c# 写出到excel

using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp3
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

    private void button1_Click(object sender, EventArgs e)
    {
        List<Person> list = new List<Person>()
        {
            new Person() { name="cheng",age=112,email="1231@"},
          new Person() { name = "gong", age = 122, email = "1232@" },
          new Person() { name = "aoaoa", age = 132, email = "1233@" },
           new Person() { name = "liu", age = 142, email = "12334@" },
            new Person() { name = "piu", age = 152, email = "12335@" }
        };
        ///1 创建工作浦对象
        IWorkbook workbook = new HSSFWorkbook();
        //workbook.Dispose();
        //2 在工作晡创建工作表对象
        ISheet sheet= workbook.CreateSheet("导出来的数据");
        //2.1 向工作表中插入行和单元格
        for (int i = 0; i < list.Count ; i++)
        {
            //在sheet里面创建一行
         IRow row=   sheet.CreateRow(i);
            //在改行中创建单元格
            ICell cell = row.CreateCell(0);
            cell.SetCellValue(list[i].name);
            ICell cell1 = row.CreateCell(1);
            cell1.SetCellValue(list[i].age);
            ICell cell2 = row.CreateCell(2);
            cell2.SetCellValue(list[i].email);

        }
        //写入到excel
        using (FileStream filelll = File.OpenWrite("ok.xls"))
        {
            workbook.Write(filelll);


        }
        MessageBox.Show("载入成功");
      
     }
}

}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WindowsFormsApp3
{
    class Person
    {
        public string name
        {
            get;
            set;        
        }
        public int  age
        {
            get;
            set;
        }
        public string email
        {
            get;
            set;
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值