1.  创建操作Excel的类,设置Excel的头尾信息.

 

package com.njdt_oa.common.util;

/**

 * 后台拼装表格,并输出到客户端的Excel信息.

 *

 * @author PF

 */

public class ExcelOprateNew {

 

    private ExcelOprateNew(){}

   

   

    public static ExcelOprateNew getExcelOprateNew(){

       return new ExcelOprateNew();

    }

   

    /**

     * 设置Excel头部信息

     * @return

     */

    public final String setExcelHeader(){

       StringBuilder sb = new StringBuilder();

       sb.append("<html xmlns:x=\"urn:schemas-microsoft-com:office:excel\">");

        sb.append(" <head>");

        sb.append(" <!--[if gte mso 9]><xml>");

        sb.append("<x:ExcelWorkbook>");

        sb.append("<x:ExcelWorksheets>");

        sb.append("<x:ExcelWorksheet>");

        sb.append("<x:Name></x:Name>");

        sb.append("<x:WorksheetOptions>");

        sb.append("<x:Print>");           

        sb.append("<x:ValidPrinterInfo />");

        sb.append(" </x:Print>");

        sb.append("</x:WorksheetOptions>");

        sb.append("</x:ExcelWorksheet>");

        sb.append("</x:ExcelWorksheets>");

        sb.append("</x:ExcelWorkbook>");

        sb.append("</xml>");

        sb.append("<![endif]-->");

        sb.append(" </head>");

        sb.append("<body>");

        return sb.toString();

    }

   

    /**

     * 设置Excel尾部信息

     * @return

     */

    public final String setExcelFoot(){

       StringBuilder sb = new StringBuilder();

       sb.append("</body>");

       sb.append("</html>");

       return sb.toString();

    }

   

    /**

     * 设置Excel内容中的标题(第一行)

     * @return 返回多个单元格的数据(包括<td>标签和</td>标签)

     */

    public String setTitle(String[] titles){

       StringBuilder sb = new StringBuilder("<td>");

       for (int i = 0; i < titles.length; i++) {

           String title = titles[i];

           sb.append(title);

           sb.append("</td><td>");

       }

       return sb.toString();

    }

}

 

2.  创建操作缓存类  CacheMgr.java

package com.njdt_oa.common.util;

 

import java.util.*;

 

/**

 * 操作缓存类

 *

 * @author PF 2011-01-10

 */

public class CacheMgr {

    private static Map cacheMap = new HashMap();

 

    private static Map cacheConfMap = new HashMap();

 

    private CacheMgr() {}

 

    private static CacheMgr cm = null;

 

    public static CacheMgr getInstance() {

       if (cm == null) {

           cm = new CacheMgr();

           Thread t = new ClearCache();

           t.start();

       }

       return cm;

    }

 

    /**

     * 增加缓存

     *

     * @param key

     * @param value

     * @param ccm

     *            缓存对象

     * @return

     */

    @SuppressWarnings("unchecked")

    public boolean addCache(Object key, Object value, CacheConfModel ccm) {

       //boolean flag = false;

       cacheMap.put(key, value);

       cacheConfMap.put(key, ccm);

 

       System.out.println("now addcache==" + cacheMap.size());

       return true;

    }

   

   

    /**

     * 获得缓存中的数据

     * @param key

     * @return

     */

    public Object getObjInCache(Object key){

       Object obj=cacheMap.get(key);

       return obj;

    }

 

    /**

     * 删除缓存

     *

     * @param key

     * @return

     */

    public boolean removeCache(Object key) {

       cacheMap.remove(key);

       cacheConfMap.remove(key);

 

       System.out.println("now removeCache==" + cacheMap.size());

 

       return true;

    }

 

    /**

     * 清除缓存的类

     *

     * @author PF 继承Thread线程类

     */

    @SuppressWarnings("unchecked")

    private static class ClearCache extends Thread {

       public void run() {

           while (true) {

              Set tempSet = new HashSet();

              Set set = cacheConfMap.keySet();

              Iterator it = set.iterator();

              while (it.hasNext()) {

                  Object key = it.next();

                  CacheConfModel ccm = (CacheConfModel) cacheConfMap.get(key);

                  // 比较是否需要清除

                  if (!ccm.isForever()) {

                     if ((new Date().getTime() - ccm.getBeginTime()) >= ccm

                            .getDurableTime() * 60 * 1000) {

                         // 可以清除,先记录下来

                         tempSet.add(key);

                     }

                  }

              }

              // 真正清除

              Iterator tempIt = tempSet.iterator();

              while (tempIt.hasNext()) {

                  Object key = tempIt.next();

                  cacheMap.remove(key);

                  cacheConfMap.remove(key);

 

              }

              System.out.println("now thread================>"

                     + cacheMap.size());

              // 休息

              try {

                  Thread.sleep(60 * 1000L);

              } catch (InterruptedException e) {

                  e.printStackTrace();

              }

           }

       }

    }

}

 

3.   创建缓存配置项类  CacheConfModel.java

package com.njdt_oa.common.util;

 

/**

 *

 * 缓存配置项

 *

 * @author PF 2011-01-10

 */

@SuppressWarnings("serial")

public class CacheConfModel implements java.io.Serializable{

     public CacheConfModel(){}

     

     public CacheConfModel(long beginTime,boolean isForever,int durableTime){

        this.beginTime=beginTime;

        this.isForever=isForever;

        this.durableTime=durableTime;

     }

     

     private long beginTime; //开始时间

     private boolean isForever = false; //是否永远缓存

     private int durableTime;       //持续时间

     

     public long getBeginTime() {

      return beginTime;

     }

     public void setBeginTime(long beginTime) {

      this.beginTime = beginTime;

     }

     public boolean isForever() {

      return isForever;

     }

     public void setForever(boolean isForever) {

      this.isForever = isForever;

     }

     public int getDurableTime() {

      return durableTime;

     }

     public void setDurableTime(int durableTime) {

      this.durableTime = durableTime;

     }

}

 

//获取需要的数据,放入缓存类中.

List testList=new TestService().getList();

CacheMgr.getInstance().addCache("test", shouwenListNoPager,null);

 

4.   通过action来对数据进行操作

package com.njdt_oa.common.web.action;

 

import java.text.SimpleDateFormat;

import java.util.List;

 

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

 

import org.apache.struts.action.ActionForm;

import org.apache.struts.action.ActionForward;

import org.apache.struts.action.ActionMapping;

 

import com.matrixflow.demo.common.web.action.BaseDispatchAction;

import com.njdt_oa.common.util.CacheMgr;

import com.njdt_oa.common.util.ExcelOprateNew;

 

/**

 * 文件另存为Excel

 *

 * @author PF

 */

public class OtherSaveAction extends BaseDispatchAction {

 

    @SuppressWarnings("unchecked")

    public ActionForward save(ActionMapping mapping, ActionForm form,

           HttpServletRequest request, HttpServletResponse response)

           throws Exception {

 

      String[] titles=new String[]{"登记号","来文单位","&nbsp;&nbsp;","收文日期","&nbsp;&nbsp;","当前环节名称","&nbsp;&nbsp;"};

      

       StringBuilder sb = new StringBuilder();

      

      

       //设置Excel头部信息

       sb.append(ExcelOprateNew.getExcelOprateNew().setExcelHeader());

       //第一行标题信息

        sb.append("<table><tr>").append(ExcelOprateNew.getExcelOprateNew().setTitle(titles)).append("</tr>" );

      

      

       //获得需要导出的数据

       List<ShouWen> nopager = (List<ShouWen>) CacheMgr.getInstance().getObjInCache("test");

       SimpleDateFormat sf_ = new SimpleDateFormat("yyyy-MM-dd");

      

       System.out.println("要导出的数据长度===="+nopager.size());

       //展示数据的拼装

       for (int i = 0; i < nopager.size(); i++) {

           ShouWen shouwen=(ShouWen)nopager.get(i);

           sb.append("<tr><td>");

           sb.append(shouwen.getDjh()==null?"":shouwen.getDjh()).append("</td><td>");

           sb.append(shouwen.getLwdwmc()==null?"":shouwen.getLwdwmc()).append("</td><td>");

           sb.append(shouwen.getWenhao()==null?"":shouwen.getWenhao()).append("</td><td>");

           sb.append(sf_.format(shouwen.getSwrq())==null?"":sf_.format(shouwen.getSwrq())).append("</td><td>");

           sb.append(shouwen.getTiming()==null?"":shouwen.getTiming()).append("</td><td>");

           sb.append(shouwen.getHdmc()==null?"":shouwen.getHdmc()).append("</td><td>");

           sb.append(shouwen.getBeizhu()==null?"":shouwen.getBeizhu()).append("</td></tr>");

       }

       sb.append("</table>");

      

       //设置Excel尾部信息

       sb.append(ExcelOprateNew.getExcelOprateNew().setExcelFoot());

 

      

      

      

       //导出的Excel默认文件名为test.xls

       response.addHeader("Content-Disposition","attachment;filename=test.xls");

       //response.addHeader("Content-Disposition","inline;filename=test.xls");

       // response.setContentType("application/octet-stream");

       response.setContentType("application/application/vnd.ms-excel");

       response.setCharacterEncoding("GBK");

       response.getWriter().write(sb.toString());

       response.flushBuffer();

       return mapping.findForward("");

    }

}