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错误!");
}
}