1.导入NPOI
2.引用如下内容
using NPOI.XSSF.UserModel;
using NPOI.SS.UserModel;
using NPOI.HSSF.UserModel;
using System.IO;
3.List数据
1).如果是Json类型数据从前台传递到后台,用如下方式获取前台数据;
注:数据类是自定义的类;Key是前台传到后台的键名
List<数据类> list =
Newtonsoft.Json.JsonConvert.DeserializeObject<List<数据类>>(context.Request["Key"]);
2).不是Json类型,C#后台直接写方法如下
List<数据类> list = GetList();
4.导出并保存Excel
//创建HSSFWorkbook
HSSFWorkbook excelBook = new HSSFWorkbook();
//声明style1对象,设置Excel表格的样式
ICellStyle style1 = excelBook.CreateCellStyle();
ICellStyle style2 = excelBook.CreateCellStyle();
ICellStyle style3 = excelBook.CreateCellStyle();
//设置字体颜色及将字体颜色写入相应的样式里
IFont font = excelBook.CreateFont();
font.Color = IndexedColors.Red.Index;
style3.SetFont(font);
//设置样式的对齐方式
//两端自动对齐(自动换行)
style1.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Justify;
style1.VerticalAlignment = VerticalAlignment.Center;
style2.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Center;
style2.VerticalAlignment = VerticalAlignment.Center;
style3.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Justify;
style3.VerticalAlignment = VerticalAlignment.Center;
//创建Excel工作表Sheet
ISheet sheet1 = excelBook.CreateSheet("XXX信息");
//给Sheet添加第一行的头部标题
IRow row1 = sheet1.CreateRow(0);
row1.CreateCell(0).SetCellValue("0");
row1.CreateCell(1).SetCellValue("1");
row1.CreateCell(2).SetCellValue("2");
row1.CreateCell(3).SetCellValue("3");
row1.CreateCell(4).SetCellValue("4");
row1.CreateCell(5).SetCellValue("5");
row1.CreateCell(6).SetCellValue("6");
//注:row1.CreateCell(0).SetCellValue("0") SetCellValue里面的数字就是自定义的标题名
//标题初始化设置样式(可选)
row1.GetCell(0).CellStyle = style2;
row1.GetCell(1).CellStyle = style2;
row1.GetCell(2).CellStyle = style2;
row1.GetCell(3).CellStyle = style2;
row1.GetCell(4).CellStyle = style2;
row1.GetCell(5).CellStyle = style2;
row1.GetCell(6).CellStyle = style2;
//初始化设置宽度(可选)
sheet1.SetColumnWidth(0, 10 * 256);
sheet1.SetColumnWidth(0, 10 * 256);
sheet1.SetColumnWidth(1, 10 * 256);
sheet1.SetColumnWidth(2, 17 * 256);
sheet1.SetColumnWidth(3, 30 * 256);
sheet1.SetColumnWidth(4, 34 * 256);
sheet1.SetColumnWidth(5, 20 * 256);
sheet1.SetColumnWidth(6, 26 * 256);
//添加数据行:给每一个单元格赋值
for (int i = 0; i < list.Count; i++)
{
//创建行
IRow rowTemp = sheet1.CreateRow(i + 1);
rowTemp.Height = 62 * 20;
//SetCellValue(list[i].xxx)里面的XXX是List里面的字段
rowTemp.CreateCell(0).SetCellValue(list[i].xxx);
rowTemp.CreateCell(1).SetCellValue(list[i].xxx);
rowTemp.CreateCell(2).SetCellValue(list[i].xxx);
rowTemp.CreateCell(3).SetCellValue(list[i].xxx);
rowTemp.CreateCell(4).SetCellValue(list[i].xxx);
rowTemp.CreateCell(5).SetCellValue(list[i].xxx);
rowTemp.CreateCell(6).SetCellValue(Convert.ToDouble(list[i].xxx));
//添加单元格样式(可选)
rowTemp.GetCell(0).CellStyle = style3;
rowTemp.GetCell(1).CellStyle = style3;
rowTemp.GetCell(2).CellStyle = style2;
rowTemp.GetCell(3).CellStyle = style2;
rowTemp.GetCell(4).CellStyle = style1;
rowTemp.GetCell(5).CellStyle = style1;
rowTemp.GetCell(6).CellStyle = style1;
}
//保存的文件名
string fileName = "信息" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".xls";
//文件保存路径(可自定义)
FileStream bookStream = new FileStream(@"E:\xxx\xxx\" + fileName, FileMode.OpenOrCreate);
//将工作薄写入文件流
excelBook.Write(bookStream);
bookStream.Close();
标签:row1,CellStyle,C#,Excel,List,rowTemp,CreateCell,GetCell,SetCellValue From: https://blog.51cto.com/u_14463418/7396358