读书人

npoi单元格样式有关问题

发布时间: 2013-11-21 23:38:25 作者: rapoo

npoi单元格样式问题

 ICellStyle cellStyle = workbook.CreateCellStyle();          cellStyle.DataFormat = HSSFDataFormat.GetBuiltinFormat("0.00%");          SumRow.CreateCell(17).CellStyle = cellStyle; 
SumRow.CreateCell(17).SetCellValue(Convert.ToDouble(sumExamineGrossRate));

我是这么加样式的为什么不起作用啊?
[解决办法]

/// <summary>
/// 获取单元格样式
/// </summary>
/// <param name="hssfworkbook">Excel操作类</param>
/// <param name="font">单元格字体</param>
/// <param name="fillForegroundColor">图案的颜色</param>
/// <param name="fillPattern">图案样式</param>
/// <param name="fillBackgroundColor">单元格背景</param>
/// <param name="ha">垂直对齐方式</param>
/// <param name="va">垂直对齐方式</param>
/// <returns></returns>
public static ICellStyle GetCellStyle(HSSFWorkbook hssfworkbook, IFont font, HSSFColor fillForegroundColor, FillPatternType fillPattern, HSSFColor fillBackgroundColor, HorizontalAlignment ha, VerticalAlignment va)
{
ICellStyle cellstyle = hssfworkbook.CreateCellStyle();
cellstyle.FillPattern = fillPattern;
cellstyle.Alignment = ha;
cellstyle.VerticalAlignment = va;
if (fillForegroundColor != null)
{
cellstyle.FillForegroundColor = fillForegroundColor.GetIndex();
}
if (fillBackgroundColor != null)
{
cellstyle.FillBackgroundColor = fillBackgroundColor.GetIndex();
}
if (font != null)
{
cellstyle.SetFont(font);
}
//有边框
cellstyle.BorderBottom = CellBorderType.THIN;
cellstyle.BorderLeft = CellBorderType.THIN;
cellstyle.BorderRight = CellBorderType.THIN;
cellstyle.BorderTop = CellBorderType.THIN;
return cellstyle;
}

[解决办法]
没看到你对cellStyle?设置过样式属性咧!你要设置相关属性啊,如字体、边距、背景等,如:
//设置字体
Font font1 = hssfworkbook.CreateFont();
font1.Color = HSSFColor.RED.index;
font1.IsItalic = false;
font1.FontHeightInPoints = 10;

cellStyle.SetFont(font1);
//设置边框背景等
cellStyle.BorderLeft = CellBorderType.THIN;
cellStyle.LeftBorderColor = HSSFColor.GREY_40_PERCENT.index;
cellStyle.BorderRight = CellBorderType.THIN;
cellStyle.RightBorderColor = HSSFColor.GREY_40_PERCENT.index;
cellStyle.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.GREY_40_PERCENT.index;
cellStyle.FillPattern = FillPatternType.SOLID_FOREGROUND;

读书人网 >asp.net

热点推荐