81,092
社区成员
发帖
与我相关
我的任务
分享
public class ExportUtil {
private Configuration configuration = null;
public ExportUtil(){
configuration = new Configuration();
configuration.setDefaultEncoding("UTF-8");
}
public void createApplyInfoExcel(String modelPath,String modelName,String fileName,Map dataMap) throws UnsupportedEncodingException{
Map<String,Object> excelMap=new HashMap<String,Object>();
getData(excelMap,dataMap);
configuration.setClassForTemplateLoading(this.getClass(),modelPath); //FTL文件所存在的位置
Template t=null;
try {
t = configuration.getTemplate(modelName); //文件名
} catch (IOException e) {
e.printStackTrace();
}
File outFile = new File("C:/"+fileName+".xls"); //生成文件的路径
//String path=System.getProperty("user.dir"); //放web程序可用该路径
//File outFile=new File(path+fileName+".xls");
Writer out = null;
try {
out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile),"utf-8"));
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}
try {
t.process(excelMap, out);
} catch (TemplateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
至于导出另存
可以用response.sendRedirect(request.getContextPath()+"/存放路径/"+fileName);