需求
将每个xmpp机房的在线/离线用户信息导出到excel表格中(定时任务+网页按钮),并在网页上提供下载按钮进行下载。
效果预览
导出文件效果
代码总览/** ...为公司业务代码,大多为从缓存或者数据库中获取导出数据,不影响导出功能。
前端写法为公司框架,理解大致意思就好。
*/
一、工具类:生成excel对象wb
package com.onewaveinc.utils;import java.util.list;import org.apache.poi.hssf.usermodel.hssfcell;import org.apache.poi.hssf.usermodel.hssfcellstyle;import org.apache.poi.hssf.usermodel.hssfrow;import org.apache.poi.hssf.usermodel.hssfsheet;import org.apache.poi.hssf.usermodel.hssfworkbook;import com.onewaveinc.mip.log.logger;import com.onewaveinc.user.entity.userinfo;/** * 生成excel文件工具类 * @author wxin * */public class excelutil { private static logger logger = logger.getinstance(excelutil.class); /** * 导出excel * @param sheetname sheet名称 * @param title 标题 * @param values 内容 * @param wb hssfworkbook对象 * @return */ public static hssfworkbook gethssfworkbook(string sheetname,string []title,list<userinfo> valuelist, hssfworkbook wb){ // 第一步,创建一个hssfworkbook,对应一个excel文件 if(wb == null){ wb = new hssfworkbook(); } // 第二步,在workbook中添加一个sheet,对应excel文件中的sheet hssfsheet sheet = wb.createsheet(sheetname); // 第三步,在sheet中添加表头第0行,注意老版本poi对excel的行数列数有限制 hssfrow row = sheet.createrow(0); // 第四步,创建单元格,并设置值表头 设置表头居中 hssfcellstyle style = wb.createcellstyle(); // 创建一个居中格式 style.setalignment(hssfcellstyle.align_center); //声明列对象 hssfcell cell = null; //创建标题 for(int i=0;i<title.length;i++){ cell = row.createcell((short) i); cell.setcellvalue(title[i]); cell.setcellstyle(style); } //创建内容 if (null != valuelist && valuelist.size() > 0) { for(int i=0;i
/**前端写法为公司框架,理解大致意思就好。*/...<input id="exportexcel" type="submit" value="导出" />...<script> //导出excel w.$('exportexcel').on('click',function(e){ w.create('userloginservice/exportexcel').done(function(result){ if (result == success) { w.alert(导出所有在线/离线用户成功); } else { w.alert(导出所有在线/离线用户失败); } }); });</script>
以上就是java导出excel文件的方法介绍(代码示例)的详细内容。
