NPOI导出Excel

HSSFWorkbook workbook = new HSSFWorkbook();
MemoryStream ms = new MemoryStream();
HSSFSheet sheet = workbook.CreateSheet("Sheet") as HSSFSheet;
//默认宽度 和高度
sheet.DefaultColumnWidth = 16;
sheet.DefaultRowHeightInPoints = 30;
sheet.SetColumnWidth(0, 10 * 256); 

#region 样式

//所有行的样式
HSSFCellStyle style = (HSSFCellStyle)workbook.CreateCellStyle();
//设置水平居中和垂直居中
style.VerticalAlignment = VerticalAlignment.Center;
style.Alignment = HorizontalAlignment.Center;
//内容自动换行
//style.WrapText = true;
//设置字体

style.SetFont(_exportService.SetFont(workbook, "微软雅黑", null, 10, HSSFColor.Black.Index));
//设置边框格式
_exportService.SetBorder(style, NPOI.SS.UserModel.BorderStyle.Thin, HSSFColor.Black.Index);

//标题的样式
HSSFCellStyle styleTitle = (HSSFCellStyle)workbook.CreateCellStyle();
//设置单元格的背景颜色
styleTitle.FillPattern = FillPattern.SolidForeground;
styleTitle.FillForegroundColor = _exportService.SetColorByRGB(workbook, 192, 0, 0, (short)10);
//内容自动换行
styleTitle.WrapText = true;
//设置水平居中和垂直居中
styleTitle.VerticalAlignment = VerticalAlignment.Center;
styleTitle.Alignment = HorizontalAlignment.Center;
//设置字体
styleTitle.SetFont(_exportService.SetFont(workbook, "微软雅黑", null, 12, HSSFColor.White.Index));
//设置边框格式
_exportService.SetBorder(styleTitle, NPOI.SS.UserModel.BorderStyle.Thin, HSSFColor.Black.Index);


//第一行的样式
HSSFCellStyle styleFirst = (HSSFCellStyle)workbook.CreateCellStyle();
//设置单元格的背景颜色
styleFirst.FillPattern = FillPattern.SolidForeground;
styleFirst.FillForegroundColor = _exportService.SetColorByRGB(workbook, 192, 0, 0, (short)10);
//内容自动换行
styleFirst.WrapText = true;
//设置水平居中和垂直居中
styleFirst.VerticalAlignment = VerticalAlignment.Center;
styleFirst.Alignment = HorizontalAlignment.Center;
//设置字体
styleFirst.SetFont(_exportService.SetFont(workbook, "微软雅黑", null, 10, HSSFColor.White.Index));
//设置边框格式
_exportService.SetBorder(styleFirst, NPOI.SS.UserModel.BorderStyle.Thin, HSSFColor.Black.Index);

//内容行的样式
HSSFCellStyle styleContent = (HSSFCellStyle)workbook.CreateCellStyle();
//设置水平居中和垂直居中
styleContent.VerticalAlignment = VerticalAlignment.Center;
styleContent.Alignment = HorizontalAlignment.Left;
//内容自动换行
//style.WrapText = true;
//设置字体
styleContent.SetFont(_exportService.SetFont(workbook, "微软雅黑", null, 10, HSSFColor.Black.Index));
//设置边框格式
_exportService.SetBorder(styleContent, NPOI.SS.UserModel.BorderStyle.Thin, HSSFColor.Black.Index);

#endregion

//第一行 标题
HSSFRow rowTitle = sheet.CreateRow(0) as HSSFRow;
rowTitle.CreateCell(0).SetCellValue("清欠回款列表");
sheet.AddMergedRegion(_exportService.SetCellRangeAddress(0, 0, 0, 8));
rowTitle.Cells.ForEach(r => r.CellStyle = styleTitle);
rowTitle.Height = 550;

//第二行表格头表
HSSFRow row = sheet.CreateRow(1) as HSSFRow;
row.CreateCell(0).SetCellValue("序号");
row.CreateCell(1).SetCellValue("收款单位(我方)"); 

// 设置样式
row.Cells.ForEach(c => c.CellStyle = styleFirst);
//将数据逐步写入sheet1各个行
for (int i = 0; i < list.Count; i++)
{
NPOI.SS.UserModel.IRow rowtemp = sheet.CreateRow(i + 2);
rowtemp.CreateCell(0).SetCellValue(i + 1);
rowtemp.CreateCell(1).SetCellValue(123);

//设置样式
//rowtemp.Cells.ForEach(r => r.CellStyle = style);
for (int j = 0; j < rowtemp.Cells.Count; j++)
{
if (j == 1 || j == 8)
{
rowtemp.GetCell(j).CellStyle = styleContent;
}
else
{
rowtemp.GetCell(j).CellStyle = style;
}
}

设置每个单元格的宽度
//for (int j = 0; j < rowtemp.Cells.Count; j++)
//{
// sheet.AutoSizeColumn(j);
//}
}

NPOI.SS.UserModel.IRow rowtempTotal = sheet.CreateRow(list.Count() + 2);
rowtempTotal.CreateCell(0).SetCellValue("合计");
rowtempTotal.CreateCell(1).SetCellValue("");

// 设置样式
rowtempTotal.Cells.ForEach(c => c.CellStyle = style);


workbook.Write(ms);
ms.Flush();
ms.Position = 0;

sheet = null;
list = null;
workbook = null;

return File(ms, "application/vnd.ms-excel", "xxx.xls");

转载于:https://www.cnblogs.com/lbb0214/p/9875245.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值