SpreadsheetFormatRows 格式颜色 ColdFusion

问题描述 投票:0回答:3

我正在使用 ColdFusion 和 SpreadsheetNew、SpreadsheetAddRows、SpreadsheetFormatRows 等函数创建 Excel 文件。根据我读过的文档,它们是颜色和 fgcolor 的属性。我有点困惑,不知道两者之间有什么区别。一种是文本颜色,另一种是背景颜色?我一直在使用 fgcolor 来设置行的背景颜色。 // HEADER ROW FORMAT formatHeaderRow = StructNew(); formatHeaderRow.fgcolor="royal_blue";

我的主要问题是,根据文档,我可以提供 
org.apache.poi.hssf.util.HSSFColor

颜色类中的任何值作为我的颜色。但是,我确实需要提供十六进制值或 RGB。我知道 Excel 可以处理它,因为您可以在 Excel 的颜色选择器中输入。有没有办法为我的行颜色输入十六进制或 RGB 值?


谢谢你!

更新

<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)#">


coldfusion apache-poi coldfusion-10 cfspreadsheet
3个回答
7
投票
我有点困惑,不知道两者之间有什么区别。

可以理解。属性名称是根据
POI

(底层 Java 库)中使用的约定建模的,在 IMO 中这有点令人困惑。由于 ColdFusion 仅实现 POI 功能的子集,因此名称被断章取义,使其更加混乱。为了回答你的问题,在 POI 中实际上有(3)个相关的颜色属性:

  1. 字体颜色

    - 即Font.setColor()

    
    单元格文本的颜色。在 CF 中,这是由 

    dataFormat.color

    属性控制的。

    
    

  2. 单元格图案前景色

    - 即CellStyle.setFillForegroundColor

    
    尽管有这个名字,但这就是大多数人认为的单元格

    背景

    颜色(下图中的黄色)。在 CF 中,这是由 dataFormat.fgColor 属性控制的。

    
    

  3. 单元格图案背景颜色

    - CellStyle.setFillBackgroundColor

    
    (可选)多色单元格图案中使用的辅助颜色(下图中的红色)。没有 ColdFusion 等效项。

有什么方法可以输入行颜色的十六进制或 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);



1
投票
Apache POI Utility

的更新版本。显然,SOLID_FOREGROUND属性已从

CellStyle
对象中删除并移至
FillPatternType
对象。我只是想对 @Leigh 在 2016 年给出的已接受答案进行更新。顺便说一句,非常感谢 @Leigh 提供了一个运行了好几年的优秀代码示例。希望这个答案能让某人在更新到新版本的 ACF 时免于痛苦。

根据 3.17 版本的文档,该字段已被删除。

Use FillPatternType.SOLID_FOREGROUND instead. From source code of apache-poi 3.15 I can see: /** * Fill Pattern: Solidly filled * @deprecated 3.15 beta 3. Use {@link FillPatternType#SOLID_FOREGROUND} instead. */ @Removal(version="3.17") static final short SOLID_FOREGROUND = 1; //FillPatternType.SOLID_FOREGROUND;

我修改了@Leigh上面的代码并添加了以下行

FillPatternType = createObject("java", "org.apache.poi.ss.usermodel.FillPatternType");

然后我修改了以下两行

backgroundOnlyStyle.setFillPattern( backgroundOnlyStyle.SOLID_FOREGROUND ); ... backgroundAndTextStyle.setFillPattern( backgroundAndTextStyle.SOLID_FOREGROUND );

并将其更改为

backgroundOnlyStyle.setFillPattern( FillPatternType.SOLID_FOREGROUND ); ... backgroundAndTextStyle.setFillPattern( FillPatternType.SOLID_FOREGROUND );

我创建了 @Leigh 代码的工作示例和要点,并验证了它适用于从 ACF10 到 ACF2018 的所有 ACF 版本。
https://trycf.com/gist/cb4edf103a75b60e0d62259b0f9941ff/acf2018?theme=monokai

<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"); FillPatternType = createObject("java", "org.apache.poi.ss.usermodel.FillPatternType"); // Style 1: Cell with background color (only) backgroundOnlyStyle = poiWorkbook.createCellStyle(); backgroundOnlyStyle.setFillPattern( FillPatternType.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( FillPatternType.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 ); </cfscript> <!--- Now that spreadsheet is prepared, initiate download ---> <cfheader name="Content-Disposition" value="attachment;filename=yourfile.xlsx"> <cfcontent variable="#spreadsheetReadBinary(cfSheet)#" type="application/vnd.ms-excel">



0
投票

setColor( XSSFColor.init(Color.decode("##a20932")) );

不再起作用..你必须为ColorMap添加第二个参数...“可以为空”。无法让它工作

tx 托马斯

© www.soinside.com 2019 - 2024. All rights reserved.