后台导出excel
1.导入jar包
链接:http://pan.baidu.com/s/1c2L70lE 密码:nvhh
2.获取绝对路径
2.源码:
package com.action; import java.io.File; import java.io.FileOutputStream; import java.io.PrintWriter; import java.util.ArrayList; import java.util.List; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import net.sf.json.JSONArray; import net.sf.json.JSONObject; 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 org.apache.struts2.ServletActionContext; import com.bean.AccountByDay; import com.bean.Qcash; import com.opensymphony.xwork2.ActionSupport; import com.util.UrlUtil; /** * 棋牌室提现excel导出 * @author Administrator * */ public class QpsCashExcelAction extends ActionSupport{ private List<Qcash> qcash; public List<Qcash> getQcash() { return qcash; } public void setQcash(List<Qcash> qcash) { this.qcash = qcash; } @Override public String execute() throws Exception { // TODO Auto-generated method stub HttpServletResponse response=null; response=ServletActionContext.getResponse(); HttpServletRequest request=ServletActionContext.getRequest(); response.setContentType("text/html;charset=UTF-8"); response.setCharacterEncoding("UTF-8"); PrintWriter out=response.getWriter(); //创建session对象 HttpSession session=ServletActionContext.getRequest().getSession(); if (session.getAttribute("id")==null) { out.print("<script lanaguage=‘javascript‘>alert(‘请重新登录‘);window.location=‘login.jsp‘;></script>"); out.flush(); out.close(); return null; } //棋牌室申请提现列表 String url="dapail/qps/qpsApplyMoneyList.action?"; JSONObject salerJson=JSONObject.fromObject(UrlUtil.getUrl(url)); String data=salerJson.get("data").toString(); JSONObject jsonData=JSONObject.fromObject(data); JSONArray backItemsjson = JSONArray.fromObject(jsonData.get("qpsMoneyItems").toString()); List<Qcash> qc=new ArrayList<Qcash>(); if(backItemsjson.size()>0){ // 第一步,创建一个webbook,对应一个Excel文件 HSSFWorkbook wb = new HSSFWorkbook(); // 第二步,在webbook中添加一个sheet,对应Excel文件中的sheet HSSFSheet sheet = wb.createSheet("棋牌室申请提现"); // 第三步,在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制short HSSFRow row = sheet.createRow((int) 0); // 第四步,创建单元格,并设置值表头 设置表头居中 HSSFCellStyle style = wb.createCellStyle(); style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式 HSSFCell cell = row.createCell((short) 0); cell.setCellValue("提现id"); cell.setCellStyle(style); cell = row.createCell((short) 1); cell.setCellValue("棋牌室名"); cell.setCellStyle(style); cell = row.createCell((short) 2); cell.setCellValue("棋牌室账号"); cell.setCellStyle(style); cell = row.createCell((short) 3); cell.setCellValue("卡名"); cell.setCellStyle(style); cell = row.createCell((short) 4); cell.setCellValue("账户名"); cell.setCellStyle(style); cell = row.createCell((short) 5); cell.setCellValue("银行卡账号"); cell.setCellStyle(style); cell = row.createCell((short) 6); cell.setCellValue("提现金额"); cell.setCellStyle(style); cell = row.createCell((short) 7); cell.setCellValue("提现状态"); cell.setCellStyle(style); cell = row.createCell((short) 8); cell.setCellValue("提现时间"); cell.setCellStyle(style); // 第五步,写入实体数据 实际应用中这些数据从数据库得到, for(int i=0;i<backItemsjson.size();i++){ String cash_state=""; JSONObject job = backItemsjson.getJSONObject(i); // 遍历 jsonarray 数组,把每一个对象转成 json 对象 if(job.get("cash_state").toString().equals("1")) { cash_state="审核中"; }else if(job.get("cash_state").toString().equals("2")) { cash_state="审核成功"; } else if(job.get("cash_state").toString().equals("3")) { cash_state="审核失败"; } row = sheet.createRow((int) i + 1); row.createCell((short) 0).setCellValue(job.get("cash_id").toString());//得到 每个对象中的属性值 row.createCell((short) 1).setCellValue(job.get("qps_name").toString());//得到 每个对象中的属性值 row.createCell((short) 2).setCellValue(job.get("qps_account").toString());//得到 每个对象中的属性值 row.createCell((short) 3).setCellValue(job.get("card_name").toString());//得到 每个对象中的属性值 row.createCell((short) 4).setCellValue(job.get("user_name").toString());//得到 每个对象中的属性值 row.createCell((short) 5).setCellValue(job.get("card_num").toString());//得到 每个对象中的属性值 row.createCell((short) 6).setCellValue(String.format("%.3f",Double.valueOf(job.get("cash_money").toString())));//得到 每个对象中的属性值 row.createCell((short) 7).setCellValue(cash_state);//得到 每个对象中的属性值 row.createCell((short) 8).setCellValue(job.get("cash_time").toString());//得到 每个对象中的属性值 } // 第六步,将文件存到指定位置 try { //项目真实路径 String upload = request.getRealPath("/"); // upload=upload.replace("webapps/Back/", "webapps/"); String path=upload+"qpsCash"; System.out.println(path); File file = new File(path); //判断上传文件的保存目录是否存在 if (!file.exists() && !file.isDirectory()) { //创建目录 file.mkdir(); } FileOutputStream fout = new FileOutputStream(path+"/qpsCash.xls"); wb.write(fout); fout.close(); } catch (Exception e) { e.printStackTrace(); } } return SUCCESS; } }
时间: 2024-10-23 08:10:39