excel - 如何在Grails excel-export/apache-POI插件中设置背景颜色?

标签 excel grails export apache-poi

我们正在使用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/

相关文章:

javascript - 用Grails设置React

javascript - 类型错误:require(...) 不是一个函数

java - 从 eclipse java ee 导出 java web 服务

javascript - 将 HTML Canvas 捕获为 gif/jpg/png/pdf?

grails - 手动安装插件?

javascript - 有什么办法可以在从js生成的excel文件上设置安全内容吗?

events - 如何使用Excel VBA制作外部日志?

vba - 查找列中的重复项并使用替代颜色突出显示行背景色

EXCEL 宏将 XML 文件作为 XML 表而不是只读工作簿打开

java - 从 grails 应用程序中的自定义 groovy 文件加载 spring bean