上周末,我们的组织从 ACF11 升级到 ACF2018,我发现给出的已接受答案不再有效,因为代码已被弃用并且不再起作用。ACF2018 使用来自 ACF11的Apache POI 实用程序的更新版本。显然,该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">