gpt4 book ai didi

coldfusion - SpreadsheetFormatRows 格式颜色 ColdFusion

转载 作者:行者123 更新时间:2023-12-03 12:28:34 33 4
gpt4 key购买 nike

我正在使用 ColdFusion 和 SpreadsheetNew、SpreadsheetAddRows、SpreadsheetFormatRows 等函数创建 Excel 文件。根据我读过的文档,位于 here它们是颜色和 fgcolor 的属性。我有点困惑,这两者之间的区别是什么。一种是文本颜色,另一种是背景颜色吗?我一直在使用 fgcolor 来设置行的背景颜色。

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

我的主要问题是,根据文档,我可以提供 org.apache.poi.hssf.util.HSSFColor 颜色类中的任何值作为我的颜色。但是,我真的需要提供 HEX 值或 RGB。我知道 Excel 可以处理它,因为您可以在 excel 的颜色选择器中输入任何一个。有没有办法为我的行颜色输入 HEX 或 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)#">

最佳答案

I'm a bit confused as to what the difference between the two are.

可以理解。属性名称是根据 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

Is there ANY way to enter a HEX or RGB value for my row colors?

最后我检查了核心 CF 功能不支持它。但是,您可以利用 does support it 的底层 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);

关于coldfusion - SpreadsheetFormatRows 格式颜色 ColdFusion,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37727645/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com