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;
}
}
}