function exportExcel(){
if(confirm('确认导出数据')){
$("#form1").attr("action","export-excel.action").submit();
}else{
return false;
}
}
在ExportExcel.java类里
@Action(results={@Result(name="success",type="stream",params={
"contentType","application/octet-stream",
"inputName","fileInputStream",
"contentDisposition","attachment;filename=${fileName}.xls",
"bufferSize","1024"
}
)
}
)
@Override
public String execute() throws Exception{
System.out.println("aaaaaaaaaaaaaa");
System.out.println(lotteryId);
LotteryPeriod lotteryPeriod = this.lotteryPeriodService.findById(lotteryId);
DetachedCriteria dc = this.lotteryPrizeService.createCriteria();
dc.add(Restrictions.eq("lotteryPeriod", lotteryPeriod));
List<LotteryPrize> lotPrize = this.lotteryPrizeService.findByDetachedCriteria(dc);
DetachedCriteria dc2 = this.prizeListService.createCriteria();
dc2.add(Restrictions.in("lotteryPrize", lotPrize));
if(!StringUtils.isEmpty(user_num)){
dc2.add(Restrictions.ilike("userNum",user_num.trim(),MatchMode.ANYWHERE ));
}
if(!StringUtils.isEmpty(user_name)){
dc2.add(Restrictions.ilike("userName",user_name.trim(),MatchMode.ANYWHERE ));
}
if(!StringUtils.isEmpty(weibo_url)){
dc2.add(Restrictions.ilike("weiboUrl",weibo_url.trim(),MatchMode.ANYWHERE ));
}
if(!StringUtils.isEmpty(contact_phone)){
dc2.add(Restrictions.ilike("contactPhone",contact_phone.trim(),MatchMode.ANYWHERE ));
}
dc.addOrder(Order.desc("createTime"));
List<PrizeList> priList=this.prizeListService.findByDetachedCriteria(dc2);
Sheet sheet = workBook.createSheet();
HSSFCellStyle style=workBook.createCellStyle();
style.setFillForegroundColor(HSSFColor.BLACK.index);
style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
Row metaRow = sheet.createRow(0);
//Date nowDate = new Date();
//创建表头
metaRow.createCell(0).setCellValue("编号");
metaRow.createCell(1).setCellValue("姓名");
metaRow.createCell(2).setCellValue("微博链接");
metaRow.createCell(0).setCellValue("联系号码");
metaRow.createCell(1).setCellValue("抽奖号");
metaRow.createCell(2).setCellValue("是否已经抽奖");
metaRow.createCell(0).setCellValue("所中奖项");
metaRow.createCell(1).setCellValue("抽奖日期");
//把查询出的数据编辑到excel中
if(priList!=null&&priList.size()>0){
int rowNum=1;
for(int i=0;i<priList.size();i++){
PrizeList obj = (PrizeList) priList.get(i);
Row row = sheet.createRow(rowNum);
row.createCell(0).setCellValue(rowNum);
row.createCell(1).setCellValue(obj.getLotteryNo());
row.createCell(2).setCellValue(obj.getUserNum());
row.createCell(3).setCellValue(obj.getUserName());
row.createCell(4).setCellValue(obj.getWeiboUrl());
row.createCell(5).setCellValue(obj.getContactPhone());
rowNum++;
}
}else{
System.out.println("错误");
}
return super.execute();
}
/**
* 定义"contentDisposition","attachment;filename=${fileName}.xls",
* 中${fileName}属性来源,需要转成8850编码
* @return
* @throws ParseException
*/
public String getFileName() throws ParseException {
try {
DateFormat formatSuffix = new SimpleDateFormat(
"yyyy-MM-dd");
this.fileNameSuffix =
formatSuffix.format(new Date());
return new String(("中奖名单列表").getBytes("GBK"), "ISO8859-1" ); //实际环境要GBK
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return "export";
}
public InputStream getFileInputStream(){
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] result = null;
try {
workBook.write(baos);
baos.flush();
result = baos.toByteArray();
baos.close();
} catch (IOException e) {
e.printStackTrace();
}
return new ByteArrayInputStream(result, 0, result.length);
}