码迷,mamicode.com
首页 > 其他好文 > 详细

NPOI导出Excel

时间:2018-10-30 11:24:23      阅读:183      评论:0      收藏:0      [点我收藏+]

标签:black   port   bsp   count   nbsp   etc   reg   creat   自动换行   

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

标签:black   port   bsp   count   nbsp   etc   reg   creat   自动换行   

原文地址:https://www.cnblogs.com/lbb0214/p/9875245.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!