如何使用ExcelLibrary更改Excel文件中的单元格样式? [英] How can I change cell style in an Excel file with ExcelLibrary?

查看:130
本文介绍了如何使用ExcelLibrary更改Excel文件中的单元格样式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人可以帮助我 ExcelLibrary
我想设置一个单元格背景和字体颜色,但我不知道该怎么做。
我尝试访问一个单元格样式,但是我没有找到它。



有任何想法吗?

解决方案

我已经看过这个图书馆,发现以下内容(警告 - 这是坏消息!):


  1. 没有发布版本的 ExcelLibrary 允许访问单元格颜色。


  2. 在未发布的源代码中,有一个 BackColor 属性新的 CellStyle 类,但是没有表示前景颜色的属性。


  3. BackColor 属性在保存工作簿时不会保留。它仅用于在加载工作簿时设置单元格的背景颜色。


如果使用颜色然后使用 NPOI (根据@jamietre的建议)的要求。然后,您可以设置这样的前景和背景颜色:

  HSSFCellStyle style1 = hssfworkbook.CreateCellStyle(); 

//单元格背景
style1.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.BLUE.index;
style1.FillPattern = HSSFCellStyle.SOLID_FOREGROUND;

//字体颜色
HSSFFont font1 = hssfworkbook.CreateFont();
font1.Color = NPOI.HSSF.Util.HSSFColor.YELLOW.index;
style1.SetFont(font1);

cell.CellStyle = style1;


Can anybody help me with ExcelLibrary? I'd like to set a cell background and font color, but I don't know how can I do it. I try to get access to a cell style, but I didn't found it.

Anybody have any ideas?

解决方案

I've looked into this library for you and found the following (warning - it's bad news!):

  1. There is no released version of ExcelLibrary that allows access to cell colours.

  2. In the unreleased source code there is a BackColor property in the new CellStyle class, however there is no property to represent foreground colour.

  3. The BackColor property is not persisted when the workbook is saved. It is only used to set the background colour of a cell when the workbook is loaded.

If use of colours is a requirement then use NPOI (as recommended by @jamietre). You can then set foreground and background colours like this:

HSSFCellStyle style1 = hssfworkbook.CreateCellStyle();

// cell background
style1.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.BLUE.index;
style1.FillPattern = HSSFCellStyle.SOLID_FOREGROUND;

// font color
HSSFFont font1 = hssfworkbook.CreateFont();
font1.Color = NPOI.HSSF.Util.HSSFColor.YELLOW.index;
style1.SetFont(font1);

cell.CellStyle = style1;

这篇关于如何使用ExcelLibrary更改Excel文件中的单元格样式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆