使用Apache POI将自定义颜色添加到Excel工作表 [英] Adding Custom colours to Excel sheet using Apache POI
问题描述
有人能解释如何使用Apche poi中的Cellstyle将Excel中的(rgb值或十六进制值)添加到Excelsheet工作表(在前景或背景中)吗?
Can anyone explain how to add Custom colours either using (rgb values or hex values ) to an excelsheet sheet(either in foreground or background) using Cellstyle in Apche poi to a Excelsheet(XSSF Workbook)?
推荐答案
设置自定义颜色取决于Excel
文件的类型(Office Open XML格式*.xlsx
与BIFF格式*.xls
).由于弃用,使用不同版本的apache poi
可能会有所不同.
Setting custom colors depends on the kind of Excel
file (Office Open XML format *.xlsx
vs. BIFF format *.xls
). And it might be different using different versions of apache poi
because of deprecation.
使用Office Open XML格式*.xlsx
,我们可以简单地使用
Using Office Open XML format *.xlsx
we can simply set new colors using constructor of XSSFColor. In apache poi 4.0.0
XSSFColor(byte[] rgb, IndexedColorMap colorMap)
can be used. IndexedColorMap
can be null
if no additional color map shall be used instead of the default one.
使用BIFF格式*.xls
只能使用索引颜色.但是可以暂时覆盖某些索引颜色.
Using BIFF format *.xls
only indexed colors are usable. But temporary overwriting some of the indexed colors is possible.
以下代码显示了两者均用于设置单元格的填充颜色.使用的自定义颜色是RGB(112,134,156).使用HSSF
(BIFF格式*.xls
),索引颜色HSSFColor.HSSFColorPredefined.LIME
将被临时覆盖.
Following code shows both used for setting a cells's fill color. The used custom color is RGB(112,134,156). Using HSSF
(BIFF format *.xls
) the indexed color HSSFColor.HSSFColorPredefined.LIME
will be temporary overwritten.
注意,以下内容已经过测试,可以在apache poi 4.0.0
上使用.不保证使用其他版本.
Note, the following is tested and works using apache poi 4.0.0
. No guarantee using other versions.
import java.io.FileOutputStream;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.*;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.hssf.util.HSSFColor;
public class CreateExcelCustomColor {
public static void main(String[] args) throws Exception {
Workbook workbook = new XSSFWorkbook();
//Workbook workbook = new HSSFWorkbook();
CellStyle cellcolorstyle = workbook.createCellStyle();
cellcolorstyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
byte[] rgb = new byte[]{(byte)112, (byte)134, (byte)156};
if (cellcolorstyle instanceof XSSFCellStyle) {
XSSFCellStyle xssfcellcolorstyle = (XSSFCellStyle)cellcolorstyle;
xssfcellcolorstyle.setFillForegroundColor(new XSSFColor(rgb, null));
} else if (cellcolorstyle instanceof HSSFCellStyle) {
cellcolorstyle.setFillForegroundColor(HSSFColor.HSSFColorPredefined.LIME.getIndex());
HSSFWorkbook hssfworkbook = (HSSFWorkbook)workbook;
HSSFPalette palette = hssfworkbook.getCustomPalette();
palette.setColorAtIndex(HSSFColor.HSSFColorPredefined.LIME.getIndex(), rgb[0], rgb[1], rgb[2]);
}
Sheet sheet = workbook.createSheet();
Cell cell = sheet.createRow(0).createCell(0);
cell.setCellStyle(cellcolorstyle);
FileOutputStream out = null;
if (workbook instanceof XSSFWorkbook) {
out = new FileOutputStream("CreateExcelCustomColor.xlsx");
} else if (workbook instanceof HSSFWorkbook) {
out = new FileOutputStream("CreateExcelCustomColor.xls");
}
workbook.write(out);
out.close();
workbook.close();
}
}
这篇关于使用Apache POI将自定义颜色添加到Excel工作表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!