NPOI导出Excel
Posted lbb0214
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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");
以上是关于NPOI导出Excel的主要内容,如果未能解决你的问题,请参考以下文章