我们正在使用Grails excel-export plugin,它在引擎盖下使用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))
};
但是什么都没有改变。我们尝试将setCellStyle放在plutCellValue之前,但这会导致null指针异常。
我们无法使用模板,因为我们正在开发一个插件,不幸的是,只有应用程序可以在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进行了尝试。在模板中,我将整行更改为具有背景色。所得的输出具有所有写入的数据没有背景的单元,其余的单元具有背景。即如果您写入任何数据,它将覆盖模板样式,这与文档中所说的相反。我将其作为插件网站的错误提出。
// 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)
检查是否有帮助
关于excel - 如何在Grails excel-export/apache-POI插件中设置背景颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30640710/