2016-06-09 74 views
2

我使用ColdFusion和SpreadsheetNew,SpreadsheetAddRows,SpreadsheetFormatRows等函数创建Excel文件。根据我已阅读的文档位于here他们是颜色和fgcolor propery。对于两者之间的区别我有点困惑。一个是文字颜色,另一个是背景颜色?我一直在使用fgcolor设置行的背景颜色。SpreadsheetFormatRows格式的颜色ColdFusion

// HEADER ROW FORMAT 
formatHeaderRow = StructNew(); 
formatHeaderRow.fgcolor="royal_blue"; 

我的主要问题是,根据我可以在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)#"> 
+1

的ColdFusion的任何特定版本。文档链接去CF 9 –

+0

嗯...好问题。 [我的答案](http://stackoverflow.com/a/37734884/104223)假定CF11。 CF9使用旧版本的POI,所以我不确定这些方法是否存在。 – Leigh

+0

@JamesAMohler - 我使用CF 10,我相信Leigh的答案会奏效。谢谢! – Phil

回答

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 Cell Fill Properties

有没有进入我行的颜色十六进制或RGB值什么办法?

最后我检查了它不支持的核心CF函数。但是,您可以利用底层的POI库does support it。假设你正在使用新的。XLSX格式,可以通过创建CellStyle并应用所需的XSSFColor来完成。

下面是一个例子(用CF11测试)如何通过POI设置字体和/或单元格背景颜色。尽管在真实代码中,我会建议在可重用函数中包含基本逻辑。

实施例:

// 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); 
+0

太棒了,谢谢! – Phil

+0

我将如何使用您的示例来格式化整行? – Phil

+0

[poiSheet.getRow(2).setRowStyle(backgroundAndTextStyle);](https://poi.apache.org/apidocs/org/apache/poi/hssf/usermodel/HSSFRow.html#setRowStyle(org.apache.poi。 ss.usermodel.CellStyle)) – Leigh