如何在Grails的excel-export / apache-POI插件中设置背景颜色?
我们正在使用Grails的excel-export插件 ,它使用apache-poi。
我们正在尝试设置单元格(或行)的背景颜色(以及后面的字体)。
我们试过这个:
def rgb = [50,50,50] as byte[] XSSFColor colour = new XSSFColor(rgb) headers.eachWithIndex() {item, i -> xlsxExporter.putCellValue(row, i, item.value.toString()) def style = xlsxExporter.getCellAt(row, i).getCellStyle() xlsxExporter.getCellAt(row, i).setCellStyle(style.setFillBackgroundColor(colour)) };
但没有任何变化。 我们尝试在plutCellValue之前放置setCellStyle,但是这会导致空指针exception。
我们不能使用模板,因为我们正在开发一个插件,不幸的是,只有应用程序可以在WEB-INF中有可访问的文件,而不是插件。
– 更新1
试过这个:
def style = xlsxExporter.getCellAt(row, i).getCellStyle() style.setFillBackgroundColor(colour) style.setFillPattern(CellStyle.ALIGN_FILL) xlsxExporter.getCellAt(row, i).setCellStyle(style)
没有帮助。
– 更新2
尝试使用模板xlsx。 在模板中,我改变了整个行有一个背景颜色。 所得到的输出具有写入数据的所有单元都没有背景,其余单元具有背景。 也就是说,如果您编写任何数据,它将覆盖模板样式,这与文档所述相反。 我把它作为一个插件网站的bug。
// convert the headers into a list of strings for the exporter. headers = response.rows[0].keySet() List<String> headersList = [] headers.eachWithIndex() { item, i -> headersList.add(item.value.toString()) } // copy the raw row data into a list of lists for Exporter def exports = [] response.rows.each { exports << it } xlsxExporter = new WebXlsxExporter(servletContext.getRealPath("/reporting/report_template.xlsx")).with { fillHeader(headersList) add(exports, headersList) save(outputStream) }
代替 :
style.setFillBackgroundColor(colour)
尝试这个:
style.setFillForegroundColor(color) style.setFillPattern(style.SOLID_FOREGROUND)
检查是否有帮助