MVC web NPOI 组件导出EXCLE设置其背景色方法
来源:互联网 发布:青牛软件电话 编辑:程序博客网 时间:2024/05/18 18:54
把数据导出到EXCLE表格方法:
public EmptyResult SaveExcelList()
{
NPOI.XSSF.UserModel.XSSFWorkbook workbook = new NPOI.XSSF.UserModel.XSSFWorkbook();
//HSSFWorkbook workbook = new HSSFWorkbook();
MemoryStream ms = new MemoryStream();
// 新增页。
ISheet sheet = workbook.CreateSheet(string.Format("检验时间维护"));
List<ViewColorDesign> datalist = GetNewColorList2();
//设置表头。
IRow headerRow = sheet.CreateRow(0);
headerRow.CreateCell(0).SetCellValue("大类");
headerRow.GetCell(0).CellStyle = GetCellsStyle(workbook, "gray");
headerRow.CreateCell(1).SetCellValue("中类");
headerRow.GetCell(1).CellStyle = GetCellsStyle(workbook, "gray");
headerRow.CreateCell(2).SetCellValue("小类");
headerRow.GetCell(2).CellStyle = GetCellsStyle(workbook, "gray");
headerRow.CreateCell(3).SetCellValue("2H");
headerRow.GetCell(3).CellStyle = GetCellsStyle(workbook, "gray");
headerRow.CreateCell(4).SetCellValue("4H");
headerRow.GetCell(4).CellStyle = GetCellsStyle(workbook, "gray");
headerRow.CreateCell(5).SetCellValue("4H+");
headerRow.GetCell(5).CellStyle = GetCellsStyle(workbook, "gray");
int z = 0;
foreach (var row in datalist)
{
z++;
IRow dataRow = sheet.CreateRow(z);
dataRow.CreateCell(0).SetCellValue(row.bigclass);
dataRow.CreateCell(1).SetCellValue(row.secclass);
dataRow.CreateCell(2).SetCellValue(row.thirdclass);
dataRow.CreateCell(3).SetCellValue(row.h2color);
dataRow.GetCell(3).CellStyle = GetCellsStyle(workbook,row.h2color);
dataRow.CreateCell(4).SetCellValue(row.h4color);
dataRow.GetCell(4).CellStyle = GetCellsStyle(workbook, row.h4color);
dataRow.CreateCell(5).SetCellValue(row.h5color);
dataRow.GetCell(5).CellStyle = GetCellsStyle(workbook, row.h5color);
}
workbook.Write(ms);
Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode("检验时间维护" + DateTime.Now.ToString("yyyyMMddhhmmss") + ".xlsx", System.Text.Encoding.UTF8));
Response.BinaryWrite(ms.ToArray());
return new EmptyResult();
}
设置背景色方法:
private static ICellStyle GetCellsStyle(NPOI.XSSF.UserModel.XSSFWorkbook workbook,string color)
{
ICellStyle CellsStyle = workbook.CreateCellStyle();
CellsStyle.Alignment = HorizontalAlignment.Center;
//边框
CellsStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
CellsStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
CellsStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
CellsStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
CellsStyle.FillPattern = FillPattern.SolidForeground;
switch(color)
{
case "white":
CellsStyle.FillForegroundColor = IndexedColors.White.Index;
break;
case "red":
CellsStyle.FillForegroundColor = IndexedColors.Red.Index;
break;
case "yellow":
CellsStyle.FillForegroundColor = IndexedColors.Yellow.Index;
break;
case "lime":
CellsStyle.FillForegroundColor = IndexedColors.BrightGreen.Index;
break;
case "gray":
CellsStyle.FillForegroundColor = IndexedColors.Grey40Percent.Index;
break;
default:
CellsStyle.FillForegroundColor = IndexedColors.Grey40Percent.Index;
break;
}
//CellsStyle.FillBackgroundColor = IndexedColors.BrightGreen.Index;
return CellsStyle;
}
附截图
- MVC web NPOI 组件导出EXCLE设置其背景色方法
- NPOI导出方法不依赖Office COM组件
- 不同控件设置背景色方法
- excle表格导出-MVC
- poi之excle----占用多行,添加背景色,设置样式
- 统一设置Swing组件的背景色和字体
- 统一设置Swing组件的背景色和字体
- qt 4.7组件设置背景色,并填充
- 设置标题栏背景色,背景色是自定义背景色
- 设置背景色
- 设置对话框背景色
- 设置状态栏背景色
- OPENGL 设置背景色
- 设置Eclipse背景色
- notepad++设置背景色
- SecureCRT设置背景色
- 设置对话框背景色
- 网页背景色设置
- 飞鸽使用的通信协议是什么(未完待续)
- android谷歌官方自带SwipeRefreshLayout实现下拉刷新
- 八大排序方法解析与巧记(三)选择排序
- 将androidVNC项目导入ADT
- Parse Too complex in xxxx.cpp --------source insight
- MVC web NPOI 组件导出EXCLE设置其背景色方法
- 关于pinyin4j
- red hat 安装 mongodb
- addSubView会使内存计数+1
- JAVA程序员最常犯的10个错误
- Java去除掉HTML里面所有标签,
- UNIX编程——互斥锁和条件变量
- 11688 uva 二叉树的旋转+dp Rotate to root
- map解析的几种方式