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[]{"登記號","來文單位","文 號","收文日期","題 名","當前環節名稱","備 注"};
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("");
}
}