2016-06-09 79 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