private XSSFCellStyle PhaseCellStyle(CellStyle paramCellStyle) { XSSFCellStyle style = null; //try //{ style = (XSSFCellStyle)_workbook.CreateCellStyle(); XSSFFont font = (XSSFFont)_workbook.CreateFont(); font.FontName = paramCellStyle.FontName.ToString(); font.FontHeightInPoints = paramCellStyle.FontHeightInPoints; if (paramCellStyle.FontBoldFlag) { font.Boldweight = 700; } style.SetFont(font); //设置水平位置 switch (paramCellStyle.HorizontalAlignment.ToString()) { case "Center": style.Alignment = HorizontalAlignment.Center; break; case "Left": style.Alignment = HorizontalAlignment.Left; break; case "Right": style.Alignment = HorizontalAlignment.Right; break; default: style.Alignment = HorizontalAlignment.Center; break; } //设置垂直位置 switch (paramCellStyle.VerticalAlignment.ToString()) { case "Center": style.VerticalAlignment = VerticalAlignment.Center; break; case "Top": style.VerticalAlignment = VerticalAlignment.Top; break; case "Botton": style.VerticalAlignment = VerticalAlignment.Bottom; break; default: style.VerticalAlignment = VerticalAlignment.Center; break; } //设置表格线样式宽度 switch (paramCellStyle.BorderStyle.ToString()) { case "Thin": style.BorderBottom = BorderStyle.Thin; style.BorderLeft = BorderStyle.Thin; style.BorderRight = BorderStyle.Thin; style.BorderTop = BorderStyle.Thin; break; default: break; } //设置表格线颜色 switch (paramCellStyle.BorderStyle.ToString()) { case "Black": style.BottomBorderColor = NPOI.HSSF.Util.HSSFColor.Black.Index; style.TopBorderColor = NPOI.HSSF.Util.HSSFColor.Black.Index; style.LeftBorderColor = NPOI.HSSF.Util.HSSFColor.Black.Index; style.RightBorderColor = NPOI.HSSF.Util.HSSFColor.Black.Index; break; default: break; } //设置单元格是否换行 #region 设置为文本 style.WrapText = paramCellStyle.WrapText; XSSFDataFormat format = (XSSFDataFormat)_workbook.GetCreationHelper().CreateDataFormat(); short index = format.GetFormat("@"); style.DataFormat = index; #endregion //} //catch(Exception ex) //{ // string ss = ex.Message; // return null; //} return(style); }
/// <summary> /// 解析单元格样式 /// </summary> /// <param name="paramCellStyle">单元格样式</param> /// <returns>NPOI单元格样式</returns> private XSSFCellStyle PhaseCellStyle(CellStyle paramCellStyle) { XSSFCellStyle style = (XSSFCellStyle)_workbook.CreateCellStyle(); XSSFFont font = (XSSFFont)_workbook.CreateFont(); font.FontName = paramCellStyle.FontName.ToString(); font.FontHeightInPoints = paramCellStyle.FontHeightInPoints; if (paramCellStyle.FontBoldFlag) { font.Boldweight = 700; } style.SetFont(font); //设置水平位置 switch (paramCellStyle.HorizontalAlignment.ToString()) { case "Center": style.Alignment = HorizontalAlignment.Center; break; case "Left": style.Alignment = HorizontalAlignment.Left; break; case "Right": style.Alignment = HorizontalAlignment.Right; break; default: style.Alignment = HorizontalAlignment.Center; break; } //设置垂直位置 switch (paramCellStyle.VerticalAlignment.ToString()) { case "Center": style.VerticalAlignment = VerticalAlignment.Center; break; case "Top": style.VerticalAlignment = VerticalAlignment.Top; break; case "Botton": style.VerticalAlignment = VerticalAlignment.Bottom; break; default: style.VerticalAlignment = VerticalAlignment.Center; break; } //设置表格线样式宽度 switch (paramCellStyle.BorderStyle.ToString()) { case "Thin": style.BorderBottom = BorderStyle.Thin; style.BorderLeft = BorderStyle.Thin; style.BorderRight = BorderStyle.Thin; style.BorderTop = BorderStyle.Thin; break; default: break; } //设置表格线颜色 switch (paramCellStyle.BorderStyle.ToString()) { case "Black": style.BottomBorderColor = NPOI.HSSF.Util.HSSFColor.Black.Index; style.TopBorderColor = NPOI.HSSF.Util.HSSFColor.Black.Index; style.LeftBorderColor = NPOI.HSSF.Util.HSSFColor.Black.Index; style.RightBorderColor = NPOI.HSSF.Util.HSSFColor.Black.Index; break; default: break; } //设置单元格是否换行 style.WrapText = paramCellStyle.WrapText; return(style); }