Grails: How to export Grails list to Microsoft Excel?

I have a list with information and I want to export it to Excel. How to do it?

Is the "Export plugin" good? I think some time ago I saw how to export files to Excel, but I can no longer find them.

+3
source share
4 answers

If you need actual Excel documents (and not just CSV files), I have successfully used the JExcel library . Here's a quick-written example, which Groovy could probably be, is a bit limited.

: , . , , , .

import jxl.*
import jxl.write.*

class SomeController {

    def report = {
        def file = createReport(MyDomain.list())

        response.setHeader('Content-disposition', 'attachment;filename=Report.xls')
        response.setHeader('Content-length', "${file.size()}")

        OutputStream out = new BufferedOutputStream(response.outputStream)

        try {
            out.write(file.bytes)

        } finally {
            out.close()
            return false
        }
    }

    private File createReport(def list) {
        WorkbookSettings workbookSettings = new WorkbookSettings()
        workbookSettings.locale = Locale.default

        def file = File.createTempFile('myExcelDocument', '.xls')
        file.deleteOnExit()

        WritableWorkbook workbook = Workbook.createWorkbook(file, workbookSettings)

        WritableFont font = new WritableFont(WritableFont.ARIAL, 12)
        WritableCellFormat format = new WritableCellFormat(font)

        def row = 0
        WritableSheet sheet = workbook.createSheet('MySheet', 0)

        list.each {
            // if list contains objects with 'foo' and 'bar' properties, this will
            // output one row per list item, with column A containing foo and column
            // B containing bar
            sheet.addCell(new Label(0, row, it.foo, format))
            sheet.addCell(new Label(1, row++, it.bar, format))
        }
    }
}

, , ..

+6

" Grails" , : ( ) ( ), - Excel. , , - .

if( params.format && params.format != "html"){
    response.contentType = grailsApplication.config.grails.mime.types[params.format]
    response.setHeader("Content-disposition", "attachment; filename=Docs_${new Date().format('yyyy-MM-dd')}.${params.extension}")

    List fields = ["documentNo", "modifiedBy", "modifiedDate"]
    Map labels = ["documentNo": 'Document No', "modifiedBy":'Modified by', "modifiedDate":'Last modified']

    def fullDocId = { domain, value ->
        return domain.fullDocId()
    }
    def formatDate = { domain, value ->
        return value.format('yyyy-MM-dd HH-mm')
    }

    Map formatters = [documentNo:fullDocId, modifiedDate:formatDate]
    Map parameters = [title: "${query}", "column.widths": [20, 15, 15]]

    exportService.export(params.format, response.outputStream, Document.list(), fields, labels, formatters, parameters)
}

, JExcel, , - , . .: -)

+1

excel . , .

0

JXL Grails , .

Excel , , REST.

: http://grails.org/plugin/jxl

, :

new ExcelBuilder().workbook('/path/to/test.xls') {
    sheet('SheetName') {
        cell(0,0,'DEF')
    }
}

.

0

Source: https://habr.com/ru/post/1766089/


All Articles