ecxel模板导出数据

function exportExcel2(){
var rows = $('#gridTable').datagrid('getSelections');
if(rows.length==0){
$.messager.alert('提示','请至少选择一条记录');
return;
}
var ids = "";
for(var i = 0;i ids += rows[i].id;
if(i ids += ",";
}
}
window.location = "${ctx}/staff-file!exportExcel.do?ids="+ids;

}

public void exportExcelzhigong(){
String str=request.getParameter("ids");
String docPath = request.getServletContext().getRealPath("/") + "template";
        String path = (docPath + "/zhigong.xls"); 
      HqlHelper hel1=new HqlHelper(StaffFile.class);
      hel1.addIn("id", str);
      List list=staffFileService.getList(hel1);
      List> list2=new ArrayList>();
     for(StaffFile staffFile:list){
     int i=1;
     Map map=new HashMap<>();
     map.put("id", i);
     if(Tools.isValid(staffFile.getIdCardNum())){
     map.put("idCardNum", staffFile.getIdCardNum());
     }else {
     map.put("idCardNum", "");
}
     map.put("name", staffFile.getName());
     String str1=DataDictionaryLoad.getText("NATION", staffFile.getGender());
     if(Tools.isValid(str1)){
     map.put("nation", str1);
     }else {
     map.put("nation", "");
}
     if(Tools.isValid(staffFile.getWorkHouse())){
     map.put("workHouse", DateUtil.formatDate(staffFile.getWorkHouse()).toString());
     }else {
     map.put("workHouse", "");
}
     HqlHelper helper2=new HqlHelper(GinsengRadix.class);
     helper2.addEqual("deleted", "N");
     List li=ginsengRadixService.getList(helper2);
     if(Tools.isValid(li)){
     map.put("paySalary", li.get(0).getGinsengRadix().toString());
     }else {
     map.put("paySalary", "");
}
     if(Tools.isValid(staffFile.getHomeAdd())){
     map.put("homeAdd", staffFile.getHomeAdd());
     }else {
     map.put("homeAdd", "");
}
     if(Tools.isValid(staffFile.getContactPhone())){
     map.put("contactPhone", staffFile.getContactPhone());
     }else {
     map.put("contactPhone", "");
}
     if(Tools.isValid(staffFile.getPostalCode())){
     map.put("postalCode", staffFile.getPostalCode());
     }else {
     map.put("postalCode", "");
}
     if(Tools.isValid(staffFile.getPlaceDomicile())){
     map.put("placeDomicile", staffFile.getPlaceDomicile());
     }else {
     map.put("placeDomicile", "");
}
     if(Tools.isValid(staffFile.getPostalAddress())){
     map.put("postalAddress", staffFile.getPostalAddress());
     }else {
     map.put("postalAddress", "");
}
     i++;
     list2.add(map);
     }
     String newName="社会保险职工新增报盘格式";
      JxlsUtil.exportExcel(path,newName, list2, response);
 }
 


/**
 * 从模板导出excel,按行导出
 * @param templateFilePath excel模版路径
 * @param objects 模版中填充的数据list
 * @param fileName 导出文件名称
 */
public static void exportExcel(String templateFilePath,String fileName, List objects, HttpServletResponse response) {
XLSTransformer transformer = new XLSTransformer();
Map beans = new HashMap();
List> beanList=new ArrayList>();
// for(Object obj : objects){
// Map map = obj2Map(obj);  
// beanList.add(map);
// }
beans.put("beans",objects);
FileInputStream is = null;
try {
is = new FileInputStream(templateFilePath);
response.reset();
//response.setHeader("Content-disposition", "attachment; filename="+URLEncoder.encode(fileName, "UTF-8")+".xls");// 设定输出文件头
response.setHeader("Content-disposition", "attachment; filename="+ new String(fileName.getBytes( "UTF-8" ), "ISO8859-1" )+".xls");// 设定输出文件头
response.setContentType("application/msexcel");// 定义输出类型
OutputStream os = response.getOutputStream();
org.apache.poi.ss.usermodel.Workbook workbook = transformer.transformXLS(is, beans);
workbook.write(os);
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}catch (Exception e) {
throw new RuntimeException("导出excel错误!");
}
}

ecxel模板导出数据_第1张图片

你可能感兴趣的:(ecxel模板导出数据)