//在zk中获得session
static HttpSession session = (HttpSession) Sessions.getCurrent()
.getNativeSession();
//tomcat
/**
* 輸入excel文件地址
*/
private static String templateFileName = session.getServletContext().getRealPath("/") + "form/form.xls";
/**
* 輸出excel文件地址
*/
private static String destFileName = session.getServletContext().getRealPath("/") + "form_output/output.xls";
//weblogic
/**
* 輸入excel文件地址
*/
private static String templateFileName = session.getServletContext().getResource("/").getPath()+ "form/form.xls";
/**
* 輸出excel文件地址
*/
private static String destFileName = session.getServletContext().getResource("/").getPath()+ "form_output/output.xls";
//导出excel
Map map = new HashMap();
map.put("key", value);
--第一种方式--
// 執行導入excel
XLSTransformer transformer = new XLSTransformer();
try {
transformer.transformXLS(templateFileName, map, destFileName);
} catch (ParsePropertyException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
File f = new File(destFileName );
Filedownload.save(f, null);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
--第二种方式--
InputStream in = null;
HSSFWorkbook workbook = null;
ByteArrayOutputStream bao = null;
try {
in = new BufferedInputStream(new FileInputStream(templateFileName));
workbook = transformer.transformXLS(in,map);
bao = new ByteArrayOutputStream();
workbook.write(bao);
Filedownload.save(bao.toByteArray(), "application/vnd.ms-excel", "output.xls");
} catch (ParsePropertyException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
if(in!=null){
in.close();
}
if(bao!=null){
bao.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}