ecxel模板导出数据

function exportExcel2(){
var rows = $('#gridTable').datagrid('getSelections');
if(rows.length==0){
$.messager.alert('提示','请至少选择一条记录');
return;
}
var ids = "";
for(var i = 0;i<rows.length;i++) {
ids += rows[i].id;
if(i<rows.length-1) {
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<StaffFile> list=staffFileService.getList(hel1);
      List<Map<String, Object>> list2=new ArrayList<Map<String, Object>>();
     for(StaffFile staffFile:list){
     int i=1;
     Map<String, Object> 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<GinsengRadix> 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<String,Object> beans = new HashMap<String, Object>();
List<Map<String,String>> beanList=new ArrayList<Map<String,String>>();
// for(Object obj : objects){
// Map<String,String> 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错误!");
}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值