Grails:Excel 导出 .xlsx 样式问题,打开文件时出现错误消息
Grails: Excel exporting .xlsx style issues, error message when opening the file
我正在使用 grails-excel-export 插件将数据导出到 Excel(xlsx 格式),但我在使用 Microsoft Office 打开文件时遇到问题(OpenOffice 没有问题) .
当我打开它时,我收到消息 "We found a problem with some of the content in 'exportedFile.xlsx'. Do you want us to try to recover as much as we can? If you trust the source of this workbook, click Yes",与此消息相同:
我已经发现只有在对工作簿应用任何类型的单元格样式时才会出现问题,在我的例子中,我将第一行设置为粗体。
这是代码:
def exportToExcel(results, headers, properties, ByteArrayOutputStream outputStream) {
WebXlsxExporter webXlsxExporter = new WebXlsxExporter()
webXlsxExporter.setWorksheetName("Sheet")
webXlsxExporter.with {
fillHeader(headers)
add(results, properties)
save(outputStream)
}
def wb = webXlsxExporter.getWorkbook()
def row = webXlsxExporter.getSheet().getRow(0)
makeFirstRowBold(wb, row)
wb.write(outputStream)
}
def makeFirstRowBold(Workbook wb, Row row) {
CellStyle style = wb.createCellStyle()
Font font = wb.createFont()
font.setBold(true) //Already tried with font.setBoldweight(Font.BOLDWEIGHT_BOLD)
style.setFont(font)
for (int i = 0; i < row.getLastCellNum(); i++) {
row.getCell(i).setCellStyle(style)
}
}
以防其他人发生这种情况,事实证明,向输出流写入两次会以某种方式破坏文件。
所以,因为我们已经在这一行中写入输出流:
wb.write(outputStream)
我删除了这个:
save(outputStream)
它似乎运行良好。
我正在使用 grails-excel-export 插件将数据导出到 Excel(xlsx 格式),但我在使用 Microsoft Office 打开文件时遇到问题(OpenOffice 没有问题) .
当我打开它时,我收到消息 "We found a problem with some of the content in 'exportedFile.xlsx'. Do you want us to try to recover as much as we can? If you trust the source of this workbook, click Yes",与此消息相同:
我已经发现只有在对工作簿应用任何类型的单元格样式时才会出现问题,在我的例子中,我将第一行设置为粗体。
这是代码:
def exportToExcel(results, headers, properties, ByteArrayOutputStream outputStream) {
WebXlsxExporter webXlsxExporter = new WebXlsxExporter()
webXlsxExporter.setWorksheetName("Sheet")
webXlsxExporter.with {
fillHeader(headers)
add(results, properties)
save(outputStream)
}
def wb = webXlsxExporter.getWorkbook()
def row = webXlsxExporter.getSheet().getRow(0)
makeFirstRowBold(wb, row)
wb.write(outputStream)
}
def makeFirstRowBold(Workbook wb, Row row) {
CellStyle style = wb.createCellStyle()
Font font = wb.createFont()
font.setBold(true) //Already tried with font.setBoldweight(Font.BOLDWEIGHT_BOLD)
style.setFont(font)
for (int i = 0; i < row.getLastCellNum(); i++) {
row.getCell(i).setCellStyle(style)
}
}
以防其他人发生这种情况,事实证明,向输出流写入两次会以某种方式破坏文件。
所以,因为我们已经在这一行中写入输出流:
wb.write(outputStream)
我删除了这个:
save(outputStream)
它似乎运行良好。