SpringMVC+Hibernate +MySql+ EasyUI實現POI匯出Excel(二)
阿新 • • 發佈:2022-04-28
注:使用的是MyEclipse 10.0 javaee 6.0 tomcat 6.0 匯出指定列名。使用VO接受引數。
SpringMVC+Hibernate +MySql+ EasyUI實現POI匯出Excel(二) 和 批量刪除資料 小功能的實現
1.datagrid新加的匯出按鈕程式碼
/*匯出excel 按鈕*/ toolbar:[{ id:'btnsave', text:'匯出', iconCls:'icon-save', handler:function(){ var url = '${ctx}/export?method=sheetsExport'; window.location.href=url; } }]
2.批量刪除的js程式碼和html程式碼
/****批量刪除******/ $(function(){ $("#deletes").bind("click",function(){ var selrow = $("#dg").datagrid("getSelections"); if(selrow.length==0){ alert("至少選擇一條資料"); return false; } var ids=[]; for(var i = 0; i < selrow.length; i++){ var id = selrow[i].id; ids.push(id); } if(confirm("您確定批量刪除")){ $.getJSON("${ctx}/user/delall",{"array[]":ids}, function(data){ alert(data.msg); if(true == data.success){ $("#dg").datagrid("reload"); } }) } }) }) html <a href="javascript:void(0);" id="deletes" class="blank_btn" >批量刪除</a>
3.匯出Controller程式碼
package com.xs.demo.controller; import java.io.OutputStream; import java.net.URLEncoder; import java.text.SimpleDateFormat; import java.util.Date; import java.util.List; import java.util.Map; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.poi.ss.formula.functions.T; import org.springframework.stereotype.Controller; import org.springframework.web.bind.ServletRequestUtils; import org.springframework.web.bind.annotation.RequestMapping; import com.xs.demo.dao.UserDao; import com.xs.demo.entity.Userinfo; import com.xs.demo.service.ExportExcel; import com.xs.demo.service.UserService; import com.xs.demo.util.ExcelUtils; import com.xs.demo.util.JsGridReportBase; import com.xs.demo.util.StringUtil; import com.xs.demo.util.TableData; /** * * 類名稱:ExportController * @author 宗瀟帥 * 2014-11-18 */ @Controller @RequestMapping(value="/export") public class ExportController { UserService userService; UserDao userDao; ExportExcel<T> excelService = new ExportExcel<T>(); /** * * @param request * @param response * @throws Exception */ @SuppressWarnings({ "rawtypes", "unchecked" }) @RequestMapping(params="method=sheetsExport") public void exportSheets(HttpServletRequest request, HttpServletResponse response)throws Exception { Userinfo userinfo = new Userinfo(); List list = userService.lists(userinfo);//查詢所有的資料 String haders[] = new String []{"序號","名稱","年齡","生日","地址","學號"}; SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); String filename = format.format(new Date().getTime())+".xls"; response.setContentType("application/ms-excel;charset=UTF-8"); response.setHeader("Content-Disposition", "attachment;filename=" .concat(String.valueOf(URLEncoder.encode(filename, "UTF-8")))); OutputStream out = response.getOutputStream(); try { excelService.exportExcel(haders,list,out); System.out.println("success"); } catch (Exception e) { System.out.println("error"); }finally{ out.close(); } } public UserService getUserService() { return userService; } public void setUserService(UserService userService) { this.userService = userService; } public UserDao getUserDao() { return userDao; } public void setUserDao(UserDao userDao) { this.userDao = userDao; } public ExportExcel<T> getExcelService() { return excelService; } public void setExcelService(ExportExcel<T> excelService) { this.excelService = excelService; } }
3.http://my.oschina.net/xshuai/blog/304321 工具類程式碼在這裡。
4.userdao程式碼
public List<Userinfo> lists(Userinfo userinfo) {
return super.list(Userinfo.class);
}
http://yun.baidu.com/pcloud/album/info?query_uk=3724757956&album_id=3094796070610213829原始碼和jar包。講本頁面所需要的程式碼複製相應的位置即可。
個人微博 http://weibo.com/zxshuai319