poi实现从数据库到excel的导出。

来源:互联网 发布:mac yum install 编辑:程序博客网 时间:2024/04/27 14:18

网上下载poi的jar包:http://apache.justdn.org/jakarta/poi/

2.在sturts的Action里的代码如下 :

本例子实现把list中的数据导出到excel

/*
 * Generated by MyEclipse Struts
 * Template path: templates/java/JavaClass.vtl
 */
package sso.admin.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;

import sso.admin.form.DataOutForm;
import sso.admin.recruitinfo.*;
import sso.admin.subject.*;
import sso.admin.department.*;
import sso.common.CommonUtil;
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 java.io.FileOutputStream;
import java.util.*;

/**
 * design by Mr.Chan

 * Creation date: 12-28-2007
 *
 * XDoclet definition:
 * @struts.action path="/dataOut" name="dataOutForm" input="/form/dataOut.jsp" scope="request" validate="true"
 */
public class DataOutAction extends Action {
 /*
  * Generated Methods
  */

 /**
  * Method execute
  * @param mapping
  * @param form
  * @param request
  * @param response
  * @return ActionForward
  */
 public ActionForward execute(ActionMapping mapping, ActionForm form,
   HttpServletRequest request, HttpServletResponse response) {
  DataOutForm dof = (DataOutForm) form;// TODO Auto-generated method stub
  ActionMessages errors = new ActionMessages() ;
  errors.add("error",new ActionMessage("download_file")) ;
  super.saveErrors(request, errors);
  String yxdm="";
  String zydm="";
  if(dof.getYxdm()!=null){
   yxdm=dof.getYxdm();
  }
  if(dof.getZydm()!=null){
   zydm=dof.getZydm();
  }
  String realPath=request.getRealPath("");
  int checkNum=Integer.parseInt(dof.getCheckNum());
  List l=null;
  RecruitInfoOperation rio=new RecruitInfoOperation();
  if(yxdm.equals("")&&zydm.equals("")){
   l=rio.queryAllRegStuInfo(checkNum);
  }else{
   l=rio.queryRegStuInfoBySubject(checkNum,zydm, yxdm);
  }
  //Excel 文件要存放的位置,假定在sso的download目录下
  String outputFile=realPath+"/download/temp.xls";
  SubjectOperation so=new SubjectOperation();
  DepartmentOperation deo=new DepartmentOperation();
  try{
   //创建新的Excel 工作簿
   HSSFWorkbook workbook = new HSSFWorkbook();
   //在Excel工作簿中建一名为"报到新生"的工作表,其语句为:
   //HSSFSheet sheet = workbook.createSheet("新生报到状况表");
   HSSFSheet sheet=null;
   if(checkNum==1){
    sheet = workbook.createSheet("报到新生");
   }else{
    sheet = workbook.createSheet("未报到新生");
   }
   //在索引0的位置创建行(最顶端的行)
   HSSFRow row = sheet.createRow((short)0);
   //在索引0的位置创建单元格(左上端)
   row.createCell((short)0).setCellValue("考生号");
   row.createCell((short)1).setCellValue("姓名");
   row.createCell((short)2).setCellValue("学号");
   row.createCell((short)3).setCellValue("系别");
   row.createCell((short)4).setCellValue("专业");
   row.createCell((short)5).setCellValue("考分");
   row.createCell((short)6).setCellValue("家庭住址");
   for(int i=0;i<l.size();i++){
    HSSFRow row1 = sheet.createRow((short)(i+1));
    Object temp=(Object)l.get(i);
    String ksh=temp.toString();
    YX_RegBean yx_reg=(YX_RegBean)rio.queryXsByKsh(ksh);
    String subject=null;
      String department=null;
    SubjectBean sb=new SubjectBean();
    DepartmentBean db=new DepartmentBean();
    if(yx_reg.getZydm()==null)
    {
     subject="未添加";
    }
    else{
     sb=(SubjectBean)so.queryById(yx_reg.getZydm());
     subject=CommonUtil.isoToGb(sb.getName());
    }
    if(yx_reg.getYxdm()==null)
    {
     department="未添加";
    }
    else{
     db=(DepartmentBean)deo.queryById(yx_reg.getYxdm());
     department=CommonUtil.isoToGb(db.getGroupName());
    } 
    row1.createCell((short)0).setCellValue(yx_reg.getKsh());
    row1.createCell((short)1).setCellValue(CommonUtil.isoToGb(yx_reg.getXm()));
    row1.createCell((short)2).setCellValue(yx_reg.getXh());
    row1.createCell((short)3).setCellValue(department);
    row1.createCell((short)4).setCellValue(subject);
    row1.createCell((short)5).setCellValue(yx_reg.getZf());
    row1.createCell((short)6).setCellValue(CommonUtil.isoToGb(yx_reg.getJtzz()));
    subject=null;
    department=null;
    System.gc();
    yx_reg=null;
    System.gc();
   }
   //新建一输出文件流
   FileOutputStream fOut = new FileOutputStream(outputFile);
   //把相应的Excel 工作簿存盘
   workbook.write(fOut);
   fOut.flush();
   //操作结束,关闭文件
   fOut.close();
   return mapping.getInputForward();
  }catch(Exception e) {
   System.out.println(e+"出错拉");
   return null;
  }
  
 }
}