问题描述
我有一个包含信息的列表,我想将其导出到Excel。我该怎么做?
导出插件是否有什么好处?我想我之前看到一个文件导出到Excel,但我找不到它。
如果你想实际 Excel文档(而不仅仅是CSV文件),我使用了 JExcel库有一些成功。这是一个很快写的例子,可能是Groovy-fied一点点。
编辑:更新我的例子,在控制器中执行此操作。在建筑上这样做会更有意义,但这仅仅是为了例如。
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
}
}
私有文件createReport(def list){
WorkbookSettings workbookSettings = new WorkbookSettings()
workbookSettings.locale = Locale.default
def file = File.createTempFile('myExcelDocument','.xls')
file.deleteOnExit()
WritableWorkbook工作簿= Workbook.createWorkbook(文件,工作簿设置)
WritableFont font = new WritableFont(WritableFont.ARIAL,12)
WritableCellFormat格式= new WritableCellFormat(font)
def row = 0
WritableSheet sheet = workbook.createSheet('MySheet',0)
list.each {
//如果列表包含具有'foo'和'bar'属性的对象,那么
//每列表项输出一行,列A包含foo和列
// B包含条
sheet.addCell(new Label(0,row,it.foo,format))
sheet.addCell(new Label(1,row ++,it.bar,format))
}
}
}
使用此库可以让您执行格式化,使用多个工作表,等等。
I have a list with information and I want to export it to Excel.How do I do it?
Is the "Export Plugin" any good? I think I saw one a while ago to export files to Excel but I can't find it anymore.
If you want actual Excel documents (rather than just CSV files), I've used the JExcel library with some success. Here's a quickly-written example that could probably be Groovy-fied a little bit.
Edit: Updated my example to do this in a controller. Architecturally it would make more sense to split this up a bit, but this is just for example's sake.
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))
}
}
}
Using this library lets you do things like formatting, using multiple worksheets, etc.
这篇关于Grails:如何将grails列表导出到Microsoft Excel?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!