Struts 导出 Excel 文档

本文介绍了一个基于JSP和Struts框架的人力资源档案查询系统,该系统实现了档案信息的查询、排序、分页及导出Excel等功能。系统通过下拉菜单的方式让用户选择查询条件,如一级机构、二级机构等,并提供了按姓名、职位等字段进行搜索的功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

JSP页面

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"
 contentType="text/html; charset=UTF-8"%>
<%@ taglib uri="/WEB-INF/jb-common.tld" prefix="jb"%>
<%
 String path = request.getContextPath();
 String basePath = request.getScheme() + "://"
   + request.getServerName() + ":" + request.getServerPort()
   + path + "/";
%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<!-- 日期格式 -->
<s:head theme="ajax" />
<script type='text/javascript' src='/HRSystem/dwr/interface/JCascade.js'></script>
<script type='text/javascript' src='/HRSystem/dwr/engine.js'></script>
<script type='text/javascript' src='/HRSystem/dwr/util.js'></script>
<script src="<%=basePath%>/js/validate.js"></script>
<script src="<%=basePath%>/js/icommon.js"></script>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "
http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="
http://www.w3.org/1999/xhtml">
 <head>
  <base href="<%=basePath%>">
  <meta http-equiv="pragma" content="no-cache">
  <meta http-equiv="cache-control" content="no-cache">
  <meta http-equiv="expires" content="0">
  <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
  <meta http-equiv="description" content="This is my page">
  <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
  <title>档案查询</title>
  <style type="text/css">
<!--
.STYLE5 {
 font-size: 12px
}
.STYLE7 {color: #FF0000}
.td {
 font-size: 12px;
 border-top-width: 1px;
 border-right-width: 1px;
 border-bottom-width: 1px;
 border-left-width: 1px;
 border-top-style: solid;
 border-bottom-style: none;
 border-top-color: #68B4FF;
 border-right-color: #68B4FF;
 border-bottom-color: #68B4FF;
 border-left-color: #68B4FF;
 border-right-style: none;
 border-left-style: solid;
}
.table {
 border-top-width: 1px;
 border-right-width: 1px;
 border-bottom-width: 1px;
 border-left-width: 1px;
 border-top-style: none;
 border-right-style: solid;
 border-bottom-style: solid;
 border-left-style: none;
 border-top-color: #478D8D;
 border-right-color: #478D8D;
 border-bottom-color: #478D8D;
 border-left-color: #478D8D;
}
-->
        </style>
  <script type="text/javascript">
 
  //获得所有一级机构类型
  function getAllfirstSetup()
  {
     //调用CascadeMenuBizImp中获得一级机构,回调方法
     JCascade.getAllFirstSetup(showFirstSetup);
     function showFirstSetup(data)
     {
     //将原来的选项清除,并设置初始选项
     DWRUtil.removeAllOptions("manFile.firstSetupId");
     //如果列表中的选项为空
     DWRUtil.addOptions("manFile.firstSetupId",{0:'请选择所在集团'});
      //将二级机构原来的选项清除,并设置初始选项
     DWRUtil.removeAllOptions("manFile.secondSetupId");
     DWRUtil.addOptions("manFile.secondSetupId",{0:'请选择所在公司'});
       //将三级机构原来的选项清除,并设置初始选项
       DWRUtil.removeAllOptions("manFile.thirdSetupId");
     DWRUtil.addOptions("manFile.thirdSetupId",{0:'请选择所在部门'});
     //添加到下拉列表中
     DWRUtil.addOptions("manFile.firstSetupId",data);
     }
  
  }
   //设置二级机构类型,根据一级机构ID获得所有的二级机构
   function getAllSecondSetup(firstSetupId)
       {
      //调用CascadeMenuBizImp中获得二级机构,回调方法
      JCascade.getAllSecondSetupByFirst(firstSetupId,showSecondSetup);
      function showSecondSetup(data)
      {
       //将二级机构原来的选项清除,并设置初始选项
      DWRUtil.removeAllOptions("manFile.secondSetupId");
      DWRUtil.addOptions("manFile.secondSetupId",{0:'请选择所在公司'});
        //将三级机构原来的选项清除,并设置初始选项
        DWRUtil.removeAllOptions("manFile.thirdSetupId");
      DWRUtil.addOptions("manFile.thirdSetupId",{0:'请选择所在部门'});
      //添加到下拉列表中
      DWRUtil.addOptions("manFile.secondSetupId",data);
      }
  
    }
     //设置三级级机构类型,根据一级机构ID和二级机构ID获得所有的三级机构
   function getAllthirdSetup(secondSetupId)
       {
       //获得一级机构ID
       var firstSetupId=document.forms[0].elements["manFile.firstSetupId"].value;
      //调用CascadeMenuBizImp中获得二级机构,回调方法
      JCascade.getAllThirdSetupByFC(firstSetupId,secondSetupId,showThirdSetup);
      function showThirdSetup(data)
      {
        //将三级机构原来的选项清除,并设置初始选项
        DWRUtil.removeAllOptions("manFile.thirdSetupId");
      DWRUtil.addOptions("manFile.thirdSetupId",{0:'请选择所在部门'});
      //添加到下拉列表中
      DWRUtil.addOptions("manFile.thirdSetupId",data);
      }
  
    }
   
    //设置二级机构类型,根据一级机构ID获得所有的二级机构
   function getAllSecondSetup(firstSetupId)
       {
      //调用CascadeMenuBizImp中获得二级机构,回调方法
      JCascade.getAllSecondSetupByFirst(firstSetupId,showSecondSetup);
      function showSecondSetup(data)
      {
       //将二级机构原来的选项清除,并设置初始选项
      DWRUtil.removeAllOptions("manFile.secondSetupId");
      DWRUtil.addOptions("manFile.secondSetupId",{0:'请选择所在公司'});
        //将三级机构原来的选项清除,并设置初始选项
        DWRUtil.removeAllOptions("manFile.thirdSetupId");
      DWRUtil.addOptions("manFile.thirdSetupId",{0:'请选择所在部门'});
      //添加到下拉列表中
      DWRUtil.addOptions("manFile.secondSetupId",data);
      }
  
    }
   //设置职位类别
   function getAllJobType()
       {
      //调用CascadeMenuBizImp中获得职位类别,回调方法
      JCascade.getAllJobType(showJobType);
      function showJobType(data)
      {
      DWRUtil.removeAllOptions("manFile.manJobTypeId");
      DWRUtil.addOptions("manFile.manJobTypeId",{0:'请选择职位类别'});
    
        DWRUtil.removeAllOptions("manFile.manJobId");
      DWRUtil.addOptions("manFile.manJobId",{0:'请选择职位名称'});
      //添加到下拉列表中
      DWRUtil.addOptions("manFile.manJobTypeId",data);
      }
  
    }
     //设置职位名称
   function getAllJobName(jobTypeId)
       {
      //调用CascadeMenuBizImp中获得职位名称,回调方法
      JCascade.getAllJobByType(jobTypeId,showJobName);
      function showJobName(data)
      {
        DWRUtil.removeAllOptions("manFile.manJobId");
      DWRUtil.addOptions("manFile.manJobId",{0:'请选择职位名称'});
      //添加到下拉列表中
      DWRUtil.addOptions("manFile.manJobId",data);
      }
  
    }
  
     //将下拉列表选择的值做为隐藏域外的值一起提交
      function doSubmit()
  {
  //相关机构
    document.forms[0].elements["manFile.firstSetupName"].value=getText("manFile.firstSetupId")
          document.forms[0].elements["manFile.secondSetupName"].value=getText("manFile.secondSetupId");
          document.forms[0].elements["manFile.thirdSetupName"].value=getText("manFile.thirdSetupId");
         
          //职位
           document.forms[0].elements["manFile.manJobTypeName"].value=getText("manFile.manJobTypeId");
          document.forms[0].elements["manFile.manJobName"].value=getText("manFile.manJobId");
          //获得下拉列表所有的文本值
          function getText(name)
          {
            //获得下拉列表对象
            var object =document.forms[0].elements[name];
            var index=object.selectedIndex;
                //获得行择列表的文本
            return object.options[index].text;
         
          }
  }
 </script>
 </head>
 <body onLoad="getAllfirstSetup(),getAllJobType()">

  <form action="manFile/ManFile_toList.action" method="post"
   onsubmit="doSubmit()">
   <table width="1010" border="0" cellpadding="0" cellspacing="0">
    <tr valign="top">
     <td height="24" colspan="4" align="left"
      background="<%=basePath%>/manFileManager/image/image/tipBg.jpg"
      bgcolor="#CDE0FC" class="td"
      style="background-repeat: repeat-x">
      <img src="<%=basePath%>/manFileManager/image/img/globe.gif"
       width="19" height="18" align="absmiddle" />
      你正在做的业务是:人力资源-人力资源档案-人力资源档案查询     </td>
    </tr>
    <tr valign="top">
     <td height="18" colspan="4" bgcolor="#CDE0FC" class="td">
      <label>
       请输入查询条件:
      </label>
      </td>
    </tr>
    <tr>
     <td width="122" height="30" align="left" valign="middle"
      bgcolor="#CDE0FC" class="td">
      一级机构:     </td>
     <td width="204" height="30" colspan="-4" align="left"
      bgcolor="#CDE0FC" class="td">
      <select name="manFile.firstSetupId" class="STYLE5"
       onchange="getAllSecondSetup(this.value)">
       <option value="0">
        请选择所在集团
       </option>
      </select>

      <input type="hidden" name="manFile.firstSetupName" />
      </td>
     <td width="318" height="30" colspan="-4" align="left"
      bgcolor="#CDE0FC" class="td">
      二级机构:
        <select name="manFile.secondSetupId" class="STYLE5"
       onchange="getAllthirdSetup(this.value)">
       <option value="0">
        请选择所在公司
       </option>
       <option value="株洲北大青鸟">
        株洲北大青鸟
       </option>
       <option value="长沙北大青鸟">
        长沙北大青鸟
       </option>
       <option value="四川分公司">
        四川分公司
       </option>
       <option value="长沙分公司">
        长沙分公司
       </option>
       <option value="湖北分公司">
        湖北分公司
       </option>
       <option value="江苏分公司">
        江苏分公司
       </option>
       <option value="长沙奶粉公司">
        长沙奶粉公司
       </option>
       <option value="湘潭奶粉公司">
        湘潭奶粉公司
       </option>
       <option value="株洲奶粉公司">
        株洲奶粉公司
       </option>
       </select>
      <input type="hidden" name="manFile.secondSetupName" />
      </td>
     <td width="453" height="30" colspan="-4" align="left"
      bgcolor="#CDE0FC" class="td">
      三级机构:&nbsp;
      <select name="manFile.thirdSetupId" class="STYLE5">
       <option value="0">
        请选择所在部门
       </option>
       <option value="市场部">
        市场部
       </option>
       <option value="销售部">
        销售部
       </option>
       <option value="就业部">
        教质部
       </option>
       <option value="行政部">
        行政部
       </option>
       <option value="教质部">
        教质部
       </option>

 package util.servlet;


       </select>
      <input type="hidden" name="manFile.thirdSetupName" />
      </td>
    </tr>
    <tr>
     <td height="30" align="left" valign="middle" bgcolor="#CDE0FC"
      class="td">
      职位分类:     </td>
     <td height="30" colspan="-4" align="left" bgcolor="#CDE0FC"
      class="td">
      <select name="manFile.manJobTypeId" class="STYLE5"
       onchange="getAllJobName(this.value)">
       <option value="0">
        请选择职位类别
       </option>
       </select>
      <input type="hidden" name="manFile.manJobTypeName" />
      </td>
     <td height="30" colspan="-4" align="left" bgcolor="#CDE0FC"
      class="td">
      职位名称: 
        <select name="manFile.manJobId" class="STYLE5">
       <option value="0">
        请选择职位名称
       </option>
       <option value="总经理">
        总经理
       </option>
       <option value="区域经理">
        区域经理
       </option>
       <option value="总管">
        总管
       </option>
       <option value="生产组长">
        生产组长
       </option>
       <option value="项目经理">
        项目经理
       </option>
       <option value="项目组长">
        项目组长
       </option>
       <option value="产品服务经理">
        产品服务经理
       </option>
       <option value="产品服务人员">
        产品服务人员
       </option>
       </select>
      <input type="hidden" name="manFile.manJobName" />
      </td>
     <td height="30" colspan="-4" align="left" bgcolor="#CDE0FC"
      class="td">
      关键字:&nbsp;&nbsp;&nbsp;
      <label>
       <input name="manFile.manName" type="text" class="STYLE5" size="15" />
       
      </label>
      </td>
    </tr>
    <tr>
     <td height="30" colspan="3" align="left" valign="middle"
      bgcolor="#CDE0FC" class="td">
      <label></label>
      建档时间:&nbsp;&nbsp;&nbsp;&nbsp;

       &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<s:datetimepicker name="manFile.fromDateTime" theme="simple"
       toggleType="explode"  />
      
      &nbsp;<img src="<%=basePath%>/jobChangeManager/image/image/right_Butt.jpg"
       style="background-repeat: repeat-x" align="absmiddle" />&nbsp;
      <s:datetimepicker name="manFile.toDateTime" theme="simple"
       toggleType="explode" value="today" />      </td>
     <td height="30" colspan="4" align="left" bgcolor="#CDE0FC"
      class="td">
      <input name="Submit" type="submit" class="STYLE5" value="  查  询  " />
      </td>
    </tr>
    <tr valign="top">
     <td height="21" colspan="4" align="left" bgcolor="#CDE0FC" class="td">&nbsp;     </td>
    </tr>
    <tr valign="top">
      <td height="26" colspan="4" align="left" bgcolor="#CDE0FC" class="td">
      <img src="<%=basePath%>/jobChangeManager/image/image/icon-edit.gif"
       width="18" height="18" align="absmiddle" />
      复核条件的人力资源档案总数:
      
        <span class="STYLE7">
         <s:property value="baseForm.pageResult.recTotal"/>       </span>例 </td>
    </tr>
    <tr valign="top">
     <td height="57" colspan="4" align="left" bgcolor="#CDE0FC" class="td">
      <table width="1010" border="0" cellspacing="0" cellpadding="0">
       <tr>
        <td height="27" align="center"
         background="<%=basePath%>/manFileManager/image/image/tipBg.jpg"
         class="td" style="background-repeat: repeat-x">
         序号        </td>
        <td align="center"
         background="<%=basePath%>/manFileManager/image/image/tipBg.jpg"
         class="td" onClick="order_by('manId');">
         档案编号        </td>
        <td align="center"
         background="<%=basePath%>/manFileManager/image/image/tipBg.jpg"
         class="td" onClick="order_by('manName');">
         姓名        </td>
        <td align="center"
         background="<%=basePath%>/manFileManager/image/image/tipBg.jpg"
         class="td" onClick="order_by('manSex');">
         性别        </td>
        <td align="center"
         background="<%=basePath%>/manFileManager/image/image/tipBg.jpg"
         class="td" onClick="order_by('firstSetupName');">
         一级机构        </td>
        <td align="center"
         background="<%=basePath%>/manFileManager/image/image/tipBg.jpg"
         class="td" onClick="order_by('secondSetupName');">
         二级机构        </td>
        <td align="center"
         background="<%=basePath%>/manFileManager/image/image/tipBg.jpg"
         class="td" onClick="order_by('thirdSetupName');">
         三级机构        </td>
        <td align="center"
         background="<%=basePath%>/manFileManager/image/image/tipBg.jpg"
         class="td" onClick="order_by('manJobTypeName');">
         职位分类        </td>
        <td align="center"
         background="<%=basePath%>/manFileManager/image/image/tipBg.jpg"
         class="td">
         操作        </td>
       </tr>
       
       <s:if test="%{baseForm.pageResult.list!=null}">
        <s:iterator value="baseForm.pageResult.list" id="manFile"
         status="satus">
         <s:if test="%{#satus.count!=0}">
         <!-- 存储查询出了好多条记录 -->
         <s:set name="size" value="#satus.count"></s:set>
         
          <tr>
           <td align="center"
            background="<%=basePath%>/manFileManager/image/image/topBg.jpg"
            class="td">
            <s:property value="#satus.index+1" />
            </td>
           <td height="27" align="center"
            background="<%=basePath%>/manFileManager/image/image/topBg.jpg"
            class="td">
            <s:property value="#manFile.manId" />
            </td>
           <td align="center"
            background="<%=basePath%>/manFileManager/image/image/topBg.jpg"
            class="td">
            <s:property value="#manFile.manName" />
            </td>
           <td align="center"
            background="<%=basePath%>/manFileManager/image/image/topBg.jpg"
            class="td">
            <s:property value="#manFile.manSex" />
            </td>
           <td align="center"
            background="<%=basePath%>/manFileManager/image/image/topBg.jpg"
            class="td">
            <s:property value="#manFile.firstSetupName" />
            </td>
           <td align="center"
            background="<%=basePath%>/manFileManager/image/image/topBg.jpg"
            class="td">
            <s:property value="#manFile.secondSetupName" />
            </td>
           <td align="center"
            background="<%=basePath%>/manFileManager/image/image/topBg.jpg"
            class="td">
            <s:property value="#manFile.thirdSetupName" />
            </td>
           <td align="center"
            background="<%=basePath%>/manFileManager/image/image/topBg.jpg"
            class="td">
            <s:property value="#manFile.manJobTypeName" />
            </td>

           <td align="center"
            background="<%=basePath%>/manFileManager/image/image/topBg.jpg"
            class="td">
            <label>
             <input name="Submit2" type="button" class="STYLE5" onClick="manFile/generateExcel.action" value="EXCEL" />
             <a href="manFile/generateExcel.action">EXCEL</a>
            </label>
            </td>
          </tr>
         </s:if>
        </s:iterator>
       </s:if>
          <s:if test="%{baseForm.pageResult.recTotal==0}">
        <tr>
         <td height="27" colspan="9" align="center"
          background="<%=basePath%>/manFileManager/image/image/topBg.jpg"
          class="td">
          <img src="<%=basePath%>/manFileManager/image/img/question.gif"
           width="18" height="18" align="absmiddle" />
          <span class="STYLE7">未能查找到记录,请重新查询!         </span></td>
        </tr>
       </s:if>
       </table>
     </td>
    </tr>

    <tr valign="top">
     <td height="29" colspan="4" bgcolor="#CDE0FC" class="td">
      <jb:pager form="baseForm" />
      </td>
    </tr>
    <tr valign="top">
     <td height="383" colspan="4" align="left" bgcolor="#CDE0FC" class="td">&nbsp;     </td>
    </tr>
    <tr valign="top">
     <td height="383" colspan="4" align="left" bgcolor="#CDE0FC">&nbsp;
      

     </td>
    </tr>
    <tr valign="top">
     <td height="383" colspan="4" align="left" bgcolor="#CDE0FC">&nbsp;
      

     </td>
    </tr>
    </table>
   <p>&nbsp;
    


   </p>
  </form>

 </body>
</html>

业务逻辑层

 

package biz.impl;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import comm.BaseBiz;

import dao.ManFileDao;

import util.PageResult;
import entity.ManFile;
import entity.User;
import biz.ManFileBiz;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

/**
 * Description:档案信息管理业务逻辑处理实现类 Program Name:ManFileBizImpl
 *
 * @author (☆_☆)Date:2009-12-24 12:23:31
 *
 */
public class ManFileBizImpl extends BaseBiz implements ManFileBiz {
 // 档案信息管理数据访问层接口对象
 private ManFileDao manFileDao;

 public ManFileDao getManFileDao() {
  return manFileDao;
 }

 public void setManFileDao(ManFileDao manFileDao) {
  this.manFileDao = manFileDao;
 }

 // 档案信息添加
 public void add(ManFile manFile) {
  this.manFileDao.add(manFile);

 }

 // 根据档案信息ID获得ManFile对象
 public ManFile get(int manId) {
  return this.manFileDao.get(manId);
 }

 // 根据查询语句获得数据总数
 public int getfindcount(String hql) {
  return this.manFileDao.getfindcount(hql);

 }

 // 根据查询语句,查询数据列表
 public List<ManFile> list(String sql) {
  return this.manFileDao.list(sql);

 }

 // 分页查询
 public void listPage(String hql, PageResult<ManFile> pageResult) {
  this.manFileDao.listPage(hql, pageResult);

 }

 // 修改档案信息
 public void update(ManFile manFile) {
  this.manFileDao.update(manFile);

 }

 // 档案信息ID删除
 public void del(int mfId) {
  this.del(mfId);

 }

 public List<ManFile> search(ManFile manFile) {
  return this.manFileDao.search(manFile);
 }

 // 获得为复核的档案编号
 public List<ManFile> getNcheckMFId() {
  String status = "未复核";
  return this.manFileDao.list("from ManFile m where m.manStatus='"
    + status + "'");
 }

 /**
  * 此方法用于查询档案信息列表
  *
  * @param manFile
  *            封装查询条件的用户对象
  * @param pageResult
  *            封装分页查询的条件
  */
 @SuppressWarnings("unchecked")
 public void getManFileList(ManFile manFile, PageResult pageResult) {

  // hql语句、查询所有的档案信息
  String hql = "select m from ManFile m where 1=1";
  if (manFile != null) {
   // 姓名不为空
   if (super.isNotNullOrEmpty(manFile.getManName())) {
    hql += "and m.manName like '%" + manFile.getManName() + "%'";

   }
   // 如果状态不为空
   if (super.isNotNullOrEmpty(manFile.getManStatus())) {
    hql += "and m.manStatus='" + manFile.getManStatus() + "'";
   }
   // 一级机构不为空
   if (super.isNotNullOrEmpty(manFile.getFirstSetupName())
     && !manFile.getFirstSetupName().equals("请选择所在集团")) {

    hql += "and m.firstSetupName='" + manFile.getFirstSetupName()
      + "'";

   }
   // 二级机构不为空
   if (super.isNotNullOrEmpty(manFile.getSecondSetupName())
     && !manFile.getSecondSetupName().equals("请选择所在公司")) {

    hql += "and m.secondSetupName='" + manFile.getSecondSetupName()
      + "'";
   }
   // 三级机构不为空
   if (super.isNotNullOrEmpty(manFile.getThirdSetupName())
     && !manFile.getThirdSetupName().equals("请选择所在部门")) {

    hql += "and m.thirdSetupName='" + manFile.getThirdSetupName()
      + "'";
   }
   // 职位类型不为空
   if (super.isNotNullOrEmpty(manFile.getManJobTypeName())
     && !manFile.getManJobTypeName().equals("请选择职位类别")) {

    hql += "and m.manJobTypeName='" + manFile.getManJobTypeName()
      + "'";
   }
   // 职位名称不为空
   if (super.isNotNullOrEmpty(manFile.getManJobName())
     && !manFile.getManJobName().equals("请选择职位名称")) {

    hql += "and m.manJobName='" + manFile.getManJobName() + "'";
   }
   // //建档起始日期不为空
   if (super.isNotNullOrEmpty(manFile.getFromDateTime())) {

    hql += "and m.registerTime >= '" + manFile.getFromDateTime()
      + "'";
   }

   // 建档终止日期不为空
   if (super.isNotNullOrEmpty(manFile.getToDateTime())) {

    hql += "and m.registerTime <= '" + manFile.getToDateTime()
      + "'";
   }
   // 其实日期和终止日期都不为空
   if (super.isNotNullOrEmpty(manFile.getFromDateTime())
     && super.isNotNullOrEmpty(manFile.getToDateTime())) {
    hql += "and m.registerTime between ' "
      + manFile.getFromDateTime() + "' and '"
      + manFile.getToDateTime() + "'";
   }
  }
  // 如果orderBy不为空,则要进行列表的排序查询
  if (isNotNullOrEmpty(pageResult.getOrderBy())) {

   // 判断进行升序还是降序
   String sort = pageResult.getSort();
   // 如果传过来的升序,就设为将降序
   if ("asc".equals(sort)) {
    pageResult.setSort("desc");
   } else {
    pageResult.setSort("asc");
   }
   hql += "order by " + pageResult.getOrderBy() + " " + sort;
  } else {
   // 根据档案编号降序
   hql += "order by manId desc";
  }
  this.manFileDao.listPage(hql, pageResult);
 }

 // 修改档案状态
 public void udpateStatus() {

 }

 public List<ManFile> getManFile(String manId) {
  return this.manFileDao.list("from ManFile m where m.manId= '" + manId
    + "'");
 }

 // 获得档案编号
 public String getManId() {
  // 自动设置档案编号
  List<ManFile> list = this.manFileDao
    .list("from ManFile m order by m.mfId desc");
  // HttpServletRequest request = ServletActionContext.getRequest();
  // request.setAttribute("manFiles", manFile);

  String manId = "MF00";
  Integer mfId = list.get(0).getMfId() + 1;
  if (list.get(0).getMfId() < 10) {
   manId = "MF000";
  }
  return manId + mfId.toString();
 }

 // 生成Excel
 public InputStream getInputStream(ManFile eManFile) {
  
  HSSFWorkbook wb = new HSSFWorkbook();
  HSSFSheet sheet = wb.createSheet("sheet1");

  HSSFRow row = sheet.createRow(0);

  HSSFCell cell = row.createCell((short) 0);
  cell.setEncoding(HSSFCell.ENCODING_UTF_16);
  cell.setCellValue("序号");

  cell = row.createCell((short) 1);
  cell.setEncoding(HSSFCell.ENCODING_UTF_16);
  cell.setCellValue("档案编号");

  cell = row.createCell((short) 2);
  cell.setEncoding(HSSFCell.ENCODING_UTF_16);
  cell.setCellValue("员工姓名");

  cell = row.createCell((short) 3);
  cell.setEncoding(HSSFCell.ENCODING_UTF_16);
  cell.setCellValue("性别");

  cell = row.createCell((short) 4);
  cell.setEncoding(HSSFCell.ENCODING_UTF_16);
  cell.setCellValue("年龄");

  cell = row.createCell((short) 5);
  cell.setEncoding(HSSFCell.ENCODING_UTF_16);
  cell.setCellValue("联系方式");

  cell = row.createCell((short) 6);
  cell.setEncoding(HSSFCell.ENCODING_UTF_16);
  cell.setCellValue("所属集团");

  cell = row.createCell((short) 7);
  cell.setEncoding(HSSFCell.ENCODING_UTF_16);
  cell.setCellValue("所属公司");

  cell = row.createCell((short) 8);
  cell.setEncoding(HSSFCell.ENCODING_UTF_16);
  cell.setCellValue("所属部门");

  cell = row.createCell((short) 9);
  cell.setEncoding(HSSFCell.ENCODING_UTF_16);
  cell.setCellValue("职位类别");

  cell = row.createCell((short) 10);
  cell.setEncoding(HSSFCell.ENCODING_UTF_16);
  cell.setCellValue("职位名称");
  
  String hql = "select m from ManFile m where 1=1 and m.manStatus='正常'";
  if (eManFile != null) {
   // 姓名不为空
   if (super.isNotNullOrEmpty(eManFile.getManName())) {
    hql += "and m.manName like '%" + eManFile.getManName() + "%'";

   }
   // 如果状态不为空
   if (super.isNotNullOrEmpty(eManFile.getManStatus())) {
    hql += "and m.manStatus='" + eManFile.getManStatus() + "'";
   }
   // 一级机构不为空
   if (super.isNotNullOrEmpty(eManFile.getFirstSetupName())
     && !eManFile.getFirstSetupName().equals("请选择所在集团")) {

    hql += "and m.firstSetupName='" + eManFile.getFirstSetupName()
      + "'";

   }
   // 二级机构不为空
   if (super.isNotNullOrEmpty(eManFile.getSecondSetupName())
     && !eManFile.getSecondSetupName().equals("请选择所在公司")) {

    hql += "and m.secondSetupName='" + eManFile.getSecondSetupName()
      + "'";
   }
   // 三级机构不为空
   if (super.isNotNullOrEmpty(eManFile.getThirdSetupName())
     && !eManFile.getThirdSetupName().equals("请选择所在部门")) {

    hql += "and m.thirdSetupName='" + eManFile.getThirdSetupName()
      + "'";
   }
   // 职位类型不为空
   if (super.isNotNullOrEmpty(eManFile.getManJobTypeName())
     && !eManFile.getManJobTypeName().equals("请选择职位类别")) {

    hql += "and m.manJobTypeName='" + eManFile.getManJobTypeName()
      + "'";
   }
   // 职位名称不为空
   if (super.isNotNullOrEmpty(eManFile.getManJobName())
     && !eManFile.getManJobName().equals("请选择职位名称")) {

    hql += "and m.manJobName='" + eManFile.getManJobName() + "'";
   }
   // //建档起始日期不为空
   if (super.isNotNullOrEmpty(eManFile.getFromDateTime())) {

    hql += "and m.registerTime >= '" + eManFile.getFromDateTime()
      + "'";
   }

   // 建档终止日期不为空
   if (super.isNotNullOrEmpty(eManFile.getToDateTime())) {

    hql += "and m.registerTime <= '" + eManFile.getToDateTime()
      + "'";
   }
   // 其实日期和终止日期都不为空
   if (super.isNotNullOrEmpty(eManFile.getFromDateTime())
     && super.isNotNullOrEmpty(eManFile.getToDateTime())) {
    hql += "and m.registerTime between ' "
      + eManFile.getFromDateTime() + "' and '"
      + eManFile.getToDateTime() + "'";
   }
  }
       
  List<ManFile> list = this.manFileDao.list(hql);

  for (int i = 0; i < list.size(); ++i) {
   ManFile manFile = list.get(i);

   row = sheet.createRow(i + 1);

   cell = row.createCell((short) 0);
   cell.setEncoding(HSSFCell.ENCODING_UTF_16);
   cell.setCellValue(i + 1);
   // 档案编号
   cell = row.createCell((short) 1);
   cell.setEncoding(HSSFCell.ENCODING_UTF_16);
   cell.setCellValue(manFile.getManId());
   // 姓名
   cell = row.createCell((short) 2);
   cell.setEncoding(HSSFCell.ENCODING_UTF_16);
   cell.setCellValue(manFile.getManName());
   // 性别
   cell = row.createCell((short) 3);
   cell.setEncoding(HSSFCell.ENCODING_UTF_16);
   cell.setCellValue(manFile.getManSex());
   // age
   cell = row.createCell((short) 4);
   cell.setEncoding(HSSFCell.ENCODING_UTF_16);
   cell.setCellValue(manFile.getManAge());

   // 联系方式
   cell = row.createCell((short) 5);
   cell.setEncoding(HSSFCell.ENCODING_UTF_16);
   cell.setCellValue(manFile.getManAddress());
   // 集团
   cell = row.createCell((short) 6);
   cell.setEncoding(HSSFCell.ENCODING_UTF_16);
   cell.setCellValue(manFile.getFirstSetupName());
   // 公司
   cell = row.createCell((short) 7);
   cell.setEncoding(HSSFCell.ENCODING_UTF_16);
   cell.setCellValue(manFile.getSecondSetupName());

   // 部门
   cell = row.createCell((short) 8);
   cell.setEncoding(HSSFCell.ENCODING_UTF_16);
   cell.setCellValue(manFile.getThirdSetupName());
   // 职位分类
   cell = row.createCell((short) 9);
   cell.setEncoding(HSSFCell.ENCODING_UTF_16);
   cell.setCellValue(manFile.getManJobTypeName());
   // 职位名称
   cell = row.createCell((short) 10);
   cell.setEncoding(HSSFCell.ENCODING_UTF_16);
   cell.setCellValue(manFile.getManJobName());

  }

  ByteArrayOutputStream os = new ByteArrayOutputStream();

  try {
   wb.write(os);
  } catch (IOException e) {
   e.printStackTrace();
  }

  byte[] content = os.toByteArray();

  InputStream is = new ByteArrayInputStream(content);

  return is;

 }
}
Action

package web.manfile;

import java.io.InputStream;

import biz.ManFileBiz;

import com.opensymphony.xwork2.ActionSupport;

import entity.ManFile;

/**
 * 生成EXCel Action
 *
 * @author (☆_☆) Date:2010-01-12
 */
@SuppressWarnings("serial")
public class GenerateExcelAction extends ActionSupport {
 ManFileAction manFileAction=new ManFileAction();
 private ManFileBiz manFileBiz;
 
 public ManFileBiz getManFileBiz() {
  return manFileBiz;
 }

 public void setManFileBiz(ManFileBiz manFileBiz) {
  this.manFileBiz = manFileBiz;
 }

 public InputStream getDownloadFile() {
  System.out.println(this.manFileAction.getManFile().getManId());
  return this.manFileBiz.getInputStream(manFileAction.getManFile());
 }

 @Override
 public String execute() throws Exception {
  return SUCCESS;
 }

}

struts.xml

<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
        "
http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
 <constant name="struts.devMode" value="true" />
 <!-- <constant name="struts.enable.DynamicMethodInvocation" value="true" />
  <constant name="struts.i18n.reload" value="true" />
  <constant name="struts.ui.theme" value="simple" />
  <constant name="struts.locale" value="zh_CN" /> -->
 <constant name="struts.i18n.encoding" value="GBK" />
 <!-- 通用的action包括 -->
 <package name="manFile" namespace="/manFile"
  extends="struts-default">
  <global-results>
   <result name="input">ManFile_toAdd.action</result>
  </global-results>
  <action name="*_*" class="{1}Action" method="{2}">
   <result>/manFileManager/{1}_{2}.jsp</result>
   <!--  <result name="toUpdate" type="redirect">
    Manfile_toUpdate.action
    </result> -->
   <!-- 添加成功后 -->
   <result name="toList" type="redirect">
                 ManFile_toList.action
   </result>
   <!-- 进入复核页面 -->
   <result name="toCheck" type="redirect">
    ManFile_toCheck.action
   </result>
   <!-- 如果没有复核 -->
   <result name="toNoCheck">
    /manFileManager/ManFile_noCheckList.jsp
   </result>
   <!-- 修改之后返回修改列表页面 -->
   <result name="toUpdateList" type="redirect">
    ManFile_toUpdateList.action
   </result>
   <!-- 删除之后返回删除列表 -->
   <result name="toDeleteList" type="redirect">
       ManFile_toDeleteList.action
   </result>
  </action>
  <!-- 文件上传 -->
  <action name="upload" class="web.UploadAction">
  <result>
    /MyJsp1.jsp
  </result>
  <result name="input">
  /MyJsp.jsp
  </result>
  </action>


  <!-- 通过ID进入复核页面 -->
  <action name="ManFile_toCheckById" class="ManFileAction"
   method="toCheckByMfId">
   <result>/manFileManager/ManFile_toCheck.jsp</result>
  </action>
  <!-- Excel -->
  <action name="generateExcel" class="generateExcelAction">
   <result name="success" type="stream">
    <param name="contentType">application/vnd.ms-excel</param>
    <!-- attachment不管是什么文件都会弹出一个下载对话框 -->
    <param name="contentDisposition">attachment;filename="AllManFile.xls"</param>
    <param name="inputName">downloadFile</param>
   </result>
  </action>


 </package>
</struts>
s
ervelet 启动服务器时删除 临时文件

import java.io.File;
import java.io.FileFilter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;

public class DeleteFilesServlet extends HttpServlet {

 /**
  * Destruction of the servlet. <br>
  */
 public void destroy() {

 }

 /**
  * 服务 启动时 把tomcat下的bin下的Excel.xls临时文件删除
  *
  * @throws ServletException
  *             if an error occurs
  */
 public void init() throws ServletException {
  // 第一种方法
  // File file=new File(".");
  // File[] subFiles=file.listFiles();
  // for(File f:subFiles)
  // {
  // if(f.getName().endsWith("xls"))
  // {
  //    
  // f.delete();
  // }
  //   
  // }
  // }
  // 第二种方法
  File file = new File(".");
  File[] subFiles = file.listFiles(new FileFilter() {
   public boolean accept(File pathname) {
    if (pathname.getName().endsWith("xls")) {

     return true;
    }
    return false;
   }

  });
  for (File f : subFiles) {

   f.delete();
  }
 }
}
相关链接 
http://www.cui243062529.51.com

 

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值