using NPOI.HSSF.UserModel;
using NPOI.HSSF.Util;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using System.IO;
XSSFWorkbook workbook = new XSSFWorkbook();
ISheet sheet = workbook.CreateSheet();
sheet.SetColumnWidth(0, (int)((15 + 0.72) * 256)); //*设置宽度(列号,宽度)
sheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(0, 2, 0, 3));//*合并单元格(起始行号,终止行号, 起始列号,终止列号)
IRow row0 = sheet.CreateRow(0);
row0.Height = 20 * 20;//*行高
ICell t0 = (ICell)row0.CreateCell(0); //创建单元格
{
//1.定义属性
ICellStyle cellStyle = workbook.CreateCellStyle();
//2.文字
IFont font = workbook.CreateFont();//文字属性
font.Color = HSSFColor.OliveGreen.Blue.Index;//颜色
font.IsItalic = true;//下划线
font.FontHeightInPoints = 10;
font.FontName = "微软雅黑";
cellStyle.SetFont(font);
//3.边框
cellStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Dotted;//上
cellStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Dotted;//下
cellStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Hair;//左
cellStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Hair;//右
cellStyle.TopBorderColor = HSSFColor.OliveGreen.Blue.Index;//上边框颜色
cellStyle.BottomBorderColor = HSSFColor.OliveGreen.Blue.Index;//下边框颜色
cellStyle.LeftBorderColor = HSSFColor.OliveGreen.Blue.Index;//左边框颜色
cellStyle.RightBorderColor = HSSFColor.OliveGreen.Blue.Index;//右边框颜色
cellStyle.FillForegroundColor = HSSFColor.Blue.Index;//选中后文字背景
cellStyle.FillBackgroundColor = HSSFColor.Red.Index;//选中后单元格背景
cellStyle.FillPattern = FillPattern.AltBars;
cellStyle.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Left;//水平对齐
cellStyle.VerticalAlignment = VerticalAlignment.Center;//垂直对齐
cellStyle.WrapText = true; //自动换行
cellStyle.Indention = 0;//缩进
t0.CellStyle = cellStyle;//*单元格属性
}
t0.SetCellValue("内容");
string FilePath = "Documents/xxx.xlsx";
FilePath = Path.GetFullPath(FilePath);
using (FileStream file = new FileStream(FilePath, FileMode.Create))
{
workbook.Write(file);
}
C# EXCEL表格生成 NPOI.dll应用2(表格样式)
最新推荐文章于 2022-09-16 11:46:04 发布