简体   繁体   English

Apache POI XSSF从RGB设置fillForegroundColor

[英]Apache POI XSSF setting fillForegroundColor from RGB

I'm trying to set custom fill colors in an XSSF spreadsheet, but so far have only been able to add colors based on the default colors available in either java.awt.Color or org.apache.poi.ss.usermodel.IndexedColors. 我正在尝试在XSSF电子表格中设置自定义填充颜色,但到目前为止只能根据java.awt.Color或org.apache.poi.ss.usermodel.IndexedColors中可用的默认颜色添加颜色。 Here's what I have that works: 这就是我的工作原理:

// setup
var workbook= CreateObject( 
    "java",
    "org.apache.poi.xssf.usermodel.XSSFWorkbook"
).Init();

var sheet = workbook.CreateSheet('test');
var row = sheet.CreateRow( 0 );

// first cell, using IndexedColors
var cell = row.createCell( 0 );
cell.setCellValue('test');

var IC = CreateObject( 
    "java",
    "org.apache.poi.ss.usermodel.IndexedColors"
);
var style = workbook.createCellStyle();
style.setFillPattern(style.SOLID_FOREGROUND);
style.setFillForegroundColor(IC.SKY_BLUE.getIndex());
cell.setCellStyle(style);

// second cell, using java.awt.Color
var cell2 = row.createCell( 1 );
cell2.setCellValue('test two');

var C = CreateObject( 
    "java",
    "java.awt.Color"
);
var XSSFColor = createObject("java", "org.apache.poi.xssf.usermodel.XSSFColor");
var myColor = XSSFColor.init(C.BLUE);   
var style2 = workbook.createCellStyle();
style2.setFillPattern(style2.SOLID_FOREGROUND);
style2.setFillForegroundColor(myColor);
cell2.setCellStyle(style2);

var FileOutputStream = CreateObject(
    "java",
    "java.io.FileOutputStream"
).Init( myFileName );

// Write the workout data to the file stream.
workbook.Write( 
    FileOutputStream 
);

// Close the file output stream. 
FileOutputStream.Close();

So that all works; 这一切都有效; however, I'm still no closer to getting colors using RGB values. 但是,我仍然没有接近使用RGB值获取颜色。 Assuming that I want to create an XSSFColor using a java.awt.Color, I should be able to create a Color object using any number of constructors. 假设我想使用java.awt.Color创建一个XSSFColor,我应该能够使用任意数量的构造函数创建一个Color对象。 From https://docs.oracle.com/javase/7/docs/api/java/awt/Color.html , here's what I've tried; 来自https://docs.oracle.com/javase/7/docs/api/java/awt/Color.html ,这是我尝试过的; all of these throw "the init method was not found": 所有这些抛出“未找到init方法”:

var C = CreateObject( 
    "java",
    "java.awt.Color"
);
// constructor Color(int r, int g, int b)
// var c1 = C.init(197,217,241); 
// constructor Color(int r, int g, int b, int a)
// var c1 = C.init(197,217,241,255);
// constructor Color (float r, float g, float b);
// var c1 = C.init(0.0, 0.5, 1.0);

Now, XSSFColor has a constructor that allow you to pass a byte array; 现在,XSSFColor有一个构造函数,允许您传递一个字节数组; that brings me to my next problem. 这让我想到了下一个问题。 Putting aside for the moment the fact that Java bytes are signed, if I try to create a byte array and pass that to the constructor like so, an exception is thrown: 暂时搁置Java字节被签名的事实,如果我尝试创建一个字节数组并将其传递给构造函数,则抛出异常:

var bytes = javaCast(
    "byte[]",
    [
        javaCast("byte", 50), 
        javaCast("byte", 50), 
        javaCast("byte", 50)
    ]
);
var XSSFColor = createObject("java", "org.apache.poi.xssf.usermodel.XSSFColor");
var c1 = XSSFColor.init(bytes); // throws "Unable to find a constructor for class org.apache.poi.xssf.usermodel.XSSFColor that accepts parameters of type ( [B )."

Finally, java.awt.Color has a Constructor defined as 最后,java.awt.Color有一个定义为的构造函数

Color(int rgb) Creates an opaque sRGB color with the specified combined RGB value consisting of the red component in bits 16-23, the green component in bits 8-15, and the blue component in bits 0-7. Color(int rgb)使用指定的组合RGB值创建不透明的sRGB颜色,该值由位16-23中的红色分量,位8-15中的绿色分量和位0-7中的蓝色分量组成。

I'm afraid I have no idea how to create the combined RGB value so am not able to try :-( 我恐怕我不知道如何创建组合的RGB值所以我不能尝试:-(

"the init method was not found" “找不到init方法”

Actually the full error message says (emphasis mine): 实际上完整的错误消息说(强调我的):

Either there are no methods with the specified method name and argument types or the init method is overloaded with argument types that ColdFusion cannot decipher reliably . 要么没有指定方法名称和参数类型的方法,要么init方法会使用ColdFusion无法可靠解密的参数类型进行重载 ColdFusion found 2 methods that match the provided arguments. ColdFusion找到了两个与提供的参数匹配的方法。 If this is a Java object and you verified that the method exists, use the javacast function to reduce ambiguity. 如果这是一个Java对象并且您验证了该方法存在,请使用javacast函数来减少歧义。

Unlike CF, java is strongly typed. 与CF不同,java是强类型的。 So classes like java.awt.Color can contain multiple constructors with the same number of parameters, but different data types. 因此,像java.awt.Color这样的类可以包含多个构造函数,这些构造函数具有相同数量的参数,但具有不同的数据类型。 For example: 例如:

  • Color(float r, float g, float b)
  • Color(int r, int g, int b)

Since CF is weakly typed, it is not sure which of those constructors you want to invoke. 由于CF是弱类型的,因此不确定要调用哪些构造函数。 To resolve the ambiguity, you need to use javacast() on the parameters, ie: 要解决歧义,您需要在参数上使用javacast() ,即:

var color = CreateObject( "java","java.awt.Color").init(
         javacast("int", redValue)
        , javacast("int", greenValue)
        , javacast("int", blueValue)
   );

Side note, you can also create java.awt.Color objects from a hexadecimal string using the static Color.decode() method: 另外,您还可以使用静态Color.decode()方法从十六进制字符串创建java.awt.Color对象:

 var color = CreateObject( "java","java.awt.Color").decode("##323232"); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM