grails excel-export 如何从页面导出excel
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了grails excel-export 如何从页面导出excel相关的知识,希望对你有一定的参考价值。
1.如果没有Grails 的 Export插件, 需要事先安装和配置:2.安装插件命令:grails install-plugin export
然后在Project下面的config里面配置,
在grails.mime.types=[]内加入信息如下:
/**
* 构建excel
* @param block 闭包,封装构建逻辑
*/
def build(dataList,titleList,out,Closure block)
def wb = new HSSFWorkbook();
def sheet = wb.createSheet("new sheet");
def row
def cell
def style = wb.createCellStyle()
def font = wb.createFont()
font.setFontHeightInPoints((short)12)
font.setFontName("宋体")
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD)
style.setFont(font)
style.setAlignment(HSSFCellStyle.ALIGN_CENTER)
style.setFillBackgroundColor(HSSFColor.ORANGE.index)
row = sheet.createRow((short) 0);
titleList.eachWithIndextitle, i ->
cell = row.createCell((short) (i));
cell.setCellStyle(style)
cell.setCellValue(title);
//生成逻辑
block.call(sheet,row)
titleList.size().times i ->
sheet.autoSizeColumn((short)i)
wb.write(out);
out.close()
/**
* 生成excel
* @param dataList 数据列表,其中的元素是放入每行cell的数据
* @param titleList 标题列表
* @param out
*/
def genForList(List<List> dataList,List titleList,out)
def block = sheet,row ->
dataList.eachWithIndex dataRow,i ->
row = sheet.createRow((short) (i+1));
dataRow.eachWithIndex data,j ->
row.createCell((short) (j)).setCellValue(data.toString())
build(dataList,titleList,out,block)
/**
* 生成excel
* @param dataList 数据列表,其中的元素是放入每行cell的数据
* @param titleList 标题列表
* @param out OutputStream
*/
def genForMap(List<Map> dataList,List titleList,out)
def block = sheet,row ->
dataList.eachWithIndex dataRow,i ->
row = sheet.createRow((short) (i+1));
dataRow.eachWithIndex k,v,j ->
row.createCell((short) (j)).setCellValue(v.toString())
build(dataList,titleList,out,block)
参考技术A 用 poi 来导出 excel 文件
Java代码
import org.codehaus.groovy.grails.commons.*
import org.apache.poi.hssf.usermodel.HSSFWorkbook
import org.apache.poi.hssf.usermodel.HSSFSheet
import org.apache.poi.hssf.usermodel.HSSFRow
import org.apache.poi.hssf.usermodel.HSSFCell
import org.codehaus.groovy.grails.commons.GrailsDomainClass
import org.codehaus.groovy.grails.web.converters.ConverterUtil
import org.springframework.web.servlet.support.RequestContextUtils as RCU
import org.codehaus.groovy.grails.scaffolding.DomainClassPropertyComparator;
import org.springframework.context.MessageSource
import org.apache.poi.hssf.usermodel.HSSFCellStyle
import org.apache.poi.hssf.usermodel.HSSFFont
import org.apache.poi.hssf.util.HSSFColor;
class XlsExportService
// def messageSource
MessageSource messageSource
boolean transactional = true
// def config = ConfigurationHolder.config
// def domainName
def xlsExport(out, request, domain, datas)
def excludedProps = ['id', 'version']
def column = []
def titles = []
def outProperties
def locale = RCU.getLocale(request)
// def text
def args
def domainName = domain.toLowerCase()
GrailsDomainClass domainClass = ConverterUtil.getDomainClass(domain)
if (domainClass != null)
outProperties = domainClass.properties.findAll !excludedProps.contains(it.name)
Collections.sort(outProperties, new DomainClassPropertyComparator(domainClass))
outProperties.each
column << "$it.name"
def text = messageSource.getMessage(
"$domainName.$it.name".toString(),
args == null ? null : args.toArray(),
"$domainName.$it.name".toString(),
locale)
titles << text
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("new sheet");
HSSFRow row
HSSFCell cell
HSSFCellStyle style = wb.createCellStyle()
HSSFFont font = wb.createFont()
font.setFontHeightInPoints((short)12)
font.setFontName("宋体")
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD)
style.setFont(font)
style.setAlignment(HSSFCellStyle.ALIGN_CENTER)
style.setFillBackgroundColor(HSSFColor.ORANGE.index)
// style.setFillPattern(HSSFCellStyle.)
// set the title
row = sheet.createRow((short) 0);
titles.eachWithIndex title, i ->
cell = row.createCell((short) (i));
cell.setCellStyle(style)
cell.setCellValue(title);
// set the data
datas.eachWithIndex data, i ->
row = sheet.createRow((short) (i+1));
column.eachWithIndex p, j ->
row.createCell((short) (j)).setCellValue(data."$p");
// Write the output to a file
wb.write(out);
out.close()
在你的 domaincontroller 中加入
Java代码
def exportXls =
// def excludedProps = ['id', 'version']
// def column = []
// def titles = []
// def outProperties = []
// GrailsDomainClass domainClass = ConverterUtil.getDomainClass("Person");
// if (domainClass != null)
// domainClass.persistentProperties.each p ->
// outProperties << p.name
// ;
//
//
// outProperties.each
// column << "$it"
// titles << message(code: "person.$it")
//
response.setHeader("Content-disposition", "attachment; filename=person.xls")
response.setContentType("application/vnd.ms-excel")
// ServletOutputStream f = response.getOutputStream();
xlsExportService.xlsExport(response.outputStream, request,"Person", Person.list())
// render(contextType:"application/vnd.ms-excel")
这个好处就是导出的表头信息通过 properties来获取!本回答被提问者和网友采纳
Excel-Export导出批注报错
在Grails里面使用Excel-Export插件时,导出批注时会报如下异常:org.openxmlformats.schemas.spreadsheetml.x2006.main.CTPhoneticRun
解决方法:
需引入 compile(group:'org.apache.poi',name:'ooxml-schemas',version:'1.1')
以上是关于grails excel-export 如何从页面导出excel的主要内容,如果未能解决你的问题,请参考以下文章