SpreadsheetFormatRows格式为ColdFusion

Phi*_*hil 2 coldfusion coldfusion-10 cfspreadsheet

我正在使用ColdFusion和SpreadsheetNew,SpreadsheetAddRows,SpreadsheetFormatRows等功能创建Excel文件.根据我在这里阅读的文档,他们是颜色和fgcolor的推荐.我对两者之间的区别有点困惑.一个是文字颜色而另一个是背景颜色?我一直在使用fgcolor来设置行的背景颜色.

// HEADER ROW FORMAT
formatHeaderRow = StructNew();
formatHeaderRow.fgcolor="royal_blue";
Run Code Online (Sandbox Code Playgroud)

我的主要问题是,根据文档,我可以在org.apache.poi.hssf.util.HSSFColor颜色类中提供任何值作为我的颜色.但是,我真的需要提供HEX值或RGB.我知道Excel可以处理它,因为你可以在excel的colorpicker中输入.有没有办法为我的行颜色输入HEX或RGB值?

谢谢!

UPDATE

<cfscript>
// create XLSX workbook with a few cells
// and grab underlying POI objects
cfSheet = Spreadsheetnew("Sheet1", true);
poiWorkbook = cfSheet.getWorkBook();
poiSheet = poiWorkbook.getSheet("Sheet1");


// Create reusuable style objects 
// NOTE: Excel limits the maximum number of styles allowed. So do not create a new
// style for every cell. Create distinct styles once, and apply to multiple cells/rows.
Color = createObject("java", "java.awt.Color");

// Style 1: Cell with background color (only)
backgroundOnlyStyle = poiWorkbook.createCellStyle();
backgroundOnlyStyle.setFillPattern( backgroundOnlyStyle.SOLID_FOREGROUND );
XSSFColor = createObject("java", "org.apache.poi.xssf.usermodel.XSSFColor");
backgroundOnlyStyle.setFillForegroundColor( XSSFColor.init(Color.decode("##055910")) );

// Apply styles to cell A1. Note: POI indexes are 0-based
SpreadSheetSetCellValue(cfSheet, "background color only", 1, 1);
poiSheet.getRow( 0 ).setRowStyle( backgroundOnlyStyle );


</cfscript>

<!--- stream it to the browser --->
<cfheader name="Content-Disposition" value="inline; filename=reportName.xlsx">
<cfcontent type="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" variable="#SpreadSheetReadBinary(cfSheet)#">
Run Code Online (Sandbox Code Playgroud)

Lei*_*igh 6

我对两者之间的区别有点困惑.

可以理解.属性名称是在POI(底层java库)中使用的约定之后建模的,这些约定从IMO开始有点令人困惑.由于ColdFusion仅实现了POI功能的一个子集,因此名称将脱离上下文,使其更加混乱.要回答您的问题,在POI中实际上有(3)个相关的颜色属性:

  1. 字体颜色 - 即Font.setColor()

    单元格文本的颜色.在CF,这是由dataFormat.color财产控制.

  2. 细胞模式前景色 - 即CellStyle.setFillForegroundColor

    尽管名称,这是大多数人认为的细胞背景颜色(下图中的黄色).在CF中,这是由dataFormat.fgColor财产控制.

  3. 细胞图案背景颜色 -CellStyle.setFillBackgroundColor

    (可选)多色单元格图案中使用的辅助颜色(下图中为红色).没有ColdFusion等价物.

Excel单元格填充属性

有没有办法为我的行颜色输入HEX或RGB值?

最后我检查核心CF功能不支持它.但是,您可以使用支持它的底层POI库.假设您使用的是较新的.XLSX格式,可以通过创建CellStyle并应用所需的XSSFColor来完成.

以下是如何通过POI设置字体和/或单元格背景颜色的示例(使用CF11测试).虽然在实际代码中,我建议在可重用的函数中包含基本逻辑.

例:

// create XLSX workbook with a few cells
// and grab underlying POI objects
cfSheet = Spreadsheetnew("Sheet1", true);
poiWorkbook = cfSheet.getWorkBook();
poiSheet = poiWorkbook.getSheet("Sheet1");


// Create reusuable style objects 
// NOTE: Excel limits the maximum number of styles allowed. So do not create a new
// style for every cell. Create distinct styles once, and apply to multiple cells/rows.
Color = createObject("java", "java.awt.Color");

// Style 1: Cell with background color (only)
backgroundOnlyStyle = poiWorkbook.createCellStyle();
backgroundOnlyStyle.setFillPattern( backgroundOnlyStyle.SOLID_FOREGROUND );
XSSFColor = createObject("java", "org.apache.poi.xssf.usermodel.XSSFColor");
backgroundOnlyStyle.setFillForegroundColor( XSSFColor.init(Color.decode("##055910")) );

// Style 2: Cell with font color (only)
textOnlyStyle = poiWorkbook.createCellStyle();
textFont = poiWorkbook.createFont();
XSSFColor = createObject("java", "org.apache.poi.xssf.usermodel.XSSFColor");
textFont.setColor( XSSFColor.init(Color.decode("##bd13be")) );
textOnlyStyle.setFont( textFont );

// Style 3: Cell with both backgound and Text color
backgroundAndTextStyle = poiWorkbook.createCellStyle();
textFont = poiWorkbook.createFont();
XSSFColor = createObject("java", "org.apache.poi.xssf.usermodel.XSSFColor");
textFont.setColor( XSSFColor.init(Color.decode("##a20932")) );
backgroundAndTextStyle.setFont( textFont );
XSSFColor = createObject("java", "org.apache.poi.xssf.usermodel.XSSFColor");
backgroundAndTextStyle.setFillPattern( backgroundAndTextStyle.SOLID_FOREGROUND );
backgroundAndTextStyle.setFillForegroundColor( XSSFColor.init(Color.decode("##192fda")) );

// Apply styles to cell A1. Note: POI indexes are 0-based
SpreadSheetSetCellValue(cfSheet, "background color only", 1, 1);
poiSheet.getRow( 0 ).getCell( 0 ).setCellStyle( backgroundOnlyStyle );

// Apply styles to cell A2
SpreadSheetSetCellValue(cfSheet, "text color only", 2, 1);
poiSheet.getRow( 1 ).getCell( 0 ).setCellStyle( textOnlyStyle );

// Apply styles to cell A3
SpreadSheetSetCellValue(cfSheet, "background AND text color", 3, 1);
poiSheet.getRow( 2 ).getCell( 0 ).setCellStyle( backgroundAndTextStyle );

// Save to file
SpreadSheetWrite(cfSheet, "c:/path/to/yourFile.xlsx", true);
Run Code Online (Sandbox Code Playgroud)