jxl.jar
jxls-core-0.9.9.jar
jxls-reader-0.9.9.jar
poi-3.5-FINAL.jar(必須3.5以上的版本)
其他jar根據提示可以去 http://jarvana.com/jarvana/ 查找
// 生成excel 傳入模闆檔案 要生成的内容 生成檔案 傳回生成檔案的完整路徑
public static String doExcel(String from, Map beans, String to) {
ServletContext sc = (ServletContext) ActionContext.getContext().get(
ServletActionContext.SERVLET_CONTEXT);
String path = sc.getRealPath("/model");
XLSTransformer transformer = new XLSTransformer();
String sfrom = path + "\\" + from;// 模闆檔案
String sto = path + "\\" + to;// 要生成的檔案
try {
transformer.transformXLS(sfrom, beans, sto);
} catch (ParsePropertyException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return sto;
}
// 下載下傳
public static void doDownLoad(String path, String name,
HttpServletResponse response) {
try {
response.reset();
response.setHeader("Content-disposition",
"attachment;success=true;filename ="
+ URLEncoder.encode(name, "utf-8"));
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
OutputStream fos = null;
InputStream fis = null;
File uploadFile = new File(path);
fis = new FileInputStream(uploadFile);
bis = new BufferedInputStream(fis);
fos = response.getOutputStream();
bos = new BufferedOutputStream(fos);
// 彈出下載下傳對話框
int bytesRead = 0;
byte[] buffer = new byte[8192];
while ((bytesRead = bis.read(buffer, 0, 8192)) != -1) {
bos.write(buffer, 0, bytesRead);
}
bos.flush();
fis.close();
bis.close();
fos.close();
bos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
調用:
List resultList=new ArrayList();
for (int i = 0; i < list.size(); i++) {
。。。。。。
InspectionStisfaction vo=new InspectionStisfaction();
vo.setXjrq(SystemUtil.getTimeStr2Str(String.valueOf(map.get("apply_date"))));
vo.setXjsl(lxjsl);
vo.setFcmy(lfcmy);
vo.setMy(lmy);
vo.setYb(lyb);
vo.setBmy(lbmy);
vo.setFcbmy(lfcbmy);
resultList.add(vo);
}
//生成EXcel
Map beans = new HashMap();
beans.put("resultList",resultList);
String path=SystemUtil.doExcel("巡檢商戶滿意度報表模闆.xls" , beans, "巡檢商戶滿意度報表.xls");
SystemUtil.doDownLoad(path, "巡檢商戶滿意度報表.xls",this.response);
return null;
模闆樣式:
巡檢數量(個) 總體滿意度 非常滿意 滿意 一般 不滿意 非常不滿意 合計
<jx:forEach items="${resultList}" var="l">
${l.xjrq} ${l.xjsl} ${l.fcmy} ${l.my} ${l.yb} ${l.bmy} ${l.fcbmy} $[B#+C#+D#]
</jx:forEach>
當月數量總計(個) $[SUM(B4)] $[SUM(D4)] $[SUM(E4)] $[SUM(F4)] $[SUM(G4)] $[SUM(H4)] $[SUM(I4)]