public static boolean exportExcel(String sheetName,List<?> list,Map<String, String> mapFields,
HttpServletResponse response,String path){
boolean result = false;
WritableWorkbook wook = null;//可写的工作薄对象 Object objClass = null;
OutputStream out = null;
try{
if(path != null && !"".equals(path)){
File file = new File(path);
if(!file.exists()){
file.mkdirs();
}
out = new FileOutputStream(file + "\\" + sheetName);
}else{
out = response.getOutputStream();
}
wook = Workbook.createWorkbook(out);
//定义格式 字体 下划线 斜体 粗体 颜色 //定义头样式 WritableFont font = new WritableFont(WritableFont.ARIAL, 11,
WritableFont.BOLD, false, UnderlineStyle.NO_UNDERLINE,Colour.BLACK);
WritableCellFormat wcf_title = new WritableCellFormat(font);
wcf_title.setAlignment(Alignment.CENTRE);
wcf_title.setVerticalAlignment(VerticalAlignment.CENTRE);
wcf_title.setBorder(Border.ALL,BorderLineStyle.THIN,Colour.BLACK);
wcf_title.setWrap(false);//不自动换行 // 定义表格样式 WritableFont tableFont = new WritableFont(WritableFont.ARIAL,10,
WritableFont.NO_BOLD, false, UnderlineStyle.NO_UNDERLINE,Colour.BLACK);
WritableCellFormat wcf_table = new WritableCellFormat(tableFont);
wcf_table.setAlignment(Alignment.CENTRE);
wcf_table.setVerticalAlignment(VerticalAlignment.CENTRE);
wcf_table.setBorder(Border.ALL,BorderLineStyle.THIN,Colour.BLACK);
wcf_table.setWrap(false);//不自动换行 //创建工作表 WritableSheet sheet = wook.createSheet(sheetName, 0);
SheetSettings setting = sheet.getSettings();
setting.setVerticalFreeze(1);//冻结窗口头部 int columnIndex = 0; //列索引 List<String> methodNameList = new ArrayList<String>();
if(mapFields != null){
String key = "";
Map<String,Method> getMap = null;
Method method = null;
//开始导出表格头部 for (Iterator<String> i = mapFields.keySet().iterator();i.hasNext();) {
key = i.next();
/*表格头样式导出*/ sheet.setColumnView(columnIndex, 20);//根据内容自动设置列宽 sheet.addCell(new Label(columnIndex, 0, mapFields.get(key), wcf_title));
//记录字段的顺序,以便于导出的内容与字段不出现偏移 int size=key.split(" ").length;
if(size>1){
methodNameList.add(key.split(" ")[1]);
}
else{
methodNameList.add(key);
}
columnIndex++;
}
if(list != null && list.size() > 0){
//导出表格内容 for (int i = 0,len = list.size(); i < len; i++) {
objClass = list.get(i);
//获得对象所有的get方法 getMap = getAllMethod(objClass);
//按保存的字段顺序导出内容 for (int j = 0; j < methodNameList.size(); j++) {
//根据key获取对应方法 method = getMap.get("GET" + methodNameList.get(j).toString().toUpperCase());
if(method!=null){
//从对应的get方法得到返回值 if(method.invoke(objClass) != null){
String value = method.invoke(objClass).toString();
//应用wcfc样式创建单元格 sheet.addCell(new Label(j, i+1, value, wcf_table));
}else{
sheet.addCell(new Label(j, i+1, "", wcf_table));
}
}else{
//如果没有对应的get方法,则默认将内容设为"" sheet.addCell(new Label(j, i+1, "", wcf_table));
}
}
}
}else{
System.out.println("导出表格无数据!");
}
result = true;
}
wook.write();
}catch (Exception e) {
result = false;
System.out.println("失败");
} finally{
try {
if(wook!=null){
wook.close();
}
if(out!=null){
out.flush();
out.close();
}
} catch (Exception e2) {
e2.printStackTrace();
}
}
return result;
}