java - Grails函数生成的空Csv

标签 java grails apache-commons

我正在尝试在grails项目中使用apache commons csv生成一个csv文件。
那是代码:

def createAndDownloadExcelTuttiCampiRichiesta( ArrayList result ) {
        def response = WebUtils.retrieveGrailsWebRequest().getCurrentResponse()
        response.setContentType('application/CSV')
        response.setHeader('Content-Disposition', 'Attachment;Filename="report.csv"')
        try {
            FileWriter fileWriter = new FileWriter(response.outputStream)
            CSVPrinter csvPrinter = new CSVPrinter(fileWriter, CSVFormat.DEFAULT.withHeader("ID","Name","Designation","Company"))
            csvPrinter.printRecord("1", "Test", "Test", "Test")
            csvPrinter.printRecord("2", "Test", "Test", "Test")
            csvPrinter.printRecord("3", "Test", "Test", "Test")
            csvPrinter.printRecord("4", "Test", "Test", "Test")
            csvPrinter.flush()
        } catch(Exception e) {
            log.error("Error parsing csv")
        }
        return
}

但它会生成一个空的csv文件。
为什么?

谢谢

最佳答案

具体细节可能会有所变化,具体取决于您喜欢的Grails版本,但是https://github.com/jeffbrown/francodecsv/tree/master/grails-app中的代码有效。启动应用程序,然后向http://localhost:8080/demo发送请求,该请求应下载如下所示的CSV文件...

$ cat ~/Downloads/report.csv
ID,Name,Designation,Company
1,Test,Test,Test
2,Test,Test,Test
3,Test,Test,Test
4,Test,Test,Test

https://github.com/jeffbrown/francodecsv/blob/master/grails-app/controllers/francodecsv/DemoController.groovy
package francodecsv

class DemoController {

    DemoService demoService

    def index() {
        demoService.createAndDownloadExcelTuttiCampiRichiesta([])
    }
}

https://github.com/jeffbrown/francodecsv/blob/master/grails-app/services/francodecsv/DemoService.groovy
package francodecsv

import grails.web.api.ServletAttributes
import org.apache.commons.csv.CSVFormat
import org.apache.commons.csv.CSVPrinter

class DemoService implements ServletAttributes {

    // result is ignored in this particular example... unclear from
    // the StackOverflow question what that is supposed to be used for
    void createAndDownloadExcelTuttiCampiRichiesta(ArrayList result) {
        response.setContentType('application/CSV')
        response.setHeader('Content-Disposition', 'Attachment;Filename="report.csv"')
        CharArrayWriter writer = new CharArrayWriter()
        CSVPrinter csvPrinter = new CSVPrinter(writer, CSVFormat.DEFAULT.withHeader("ID", "Name", "Designation", "Company"))
        csvPrinter.printRecord("1", "Test", "Test", "Test")
        csvPrinter.printRecord("2", "Test", "Test", "Test")
        csvPrinter.printRecord("3", "Test", "Test", "Test")
        csvPrinter.printRecord("4", "Test", "Test", "Test")
        csvPrinter.flush()
        response.outputStream << writer.toCharArray()
    }
}

希望对您有所帮助。

关于java - Grails函数生成的空Csv,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54674914/

相关文章:

java - org.apache.http.impl.execchain.RequestAbortedException : Request aborted

json - 从控制台到导入Grails转换器的挣扎

Grails:spring security 插件 - 错误 springsecurity.GormPersistentTokenRepository

java - Commons CLI 所需的组

java - 如何将 "Share button"添加到 CardView?

java - Swig 一个 DLL 到 Java

java - Tomcat 7 生产服务器上的 VerifyError 可能由 Apache Commons Logging 1.0.4 引起

java - apache.commons.lang3.DateUtils.setMonths 十二月

java - 将 ObservableList 和整数保存/加载为 XML

grails - 如何管理发送到我的 grails 的文件