Java导出页面数据或数据库数据至Excel文件并下载,采用JXL技术,小demo(servlet实现)
public class ExportExcelServlet extends HttpServlet { /** * */ private static final long serialVersionUID = 1L; @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doPost(req, resp); } @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { try { HttpSession session = request.getSession(); List<ScUser> users = new ArrayList<ScUser>(); users =(List<ScUser>) session.getAttribute("users"); OutputStream os = response.getOutputStream(); response.setContentType("application/vnd.ms-excel"); response.setCharacterEncoding("utf-8"); WritableWorkbook book = Workbook.createWorkbook(os); WritableSheet sheet = book.createSheet("用户管理表", 0); String title[] = { "号码", "部门", "使用人", "口令","IP地址","mac地址","状态" }; for (int i = 0; i < title.length; i++) { Label labe = new Label(i, 0, title[i]); sheet.addCell(labe); } for(int i=0;i<users.size();i++){ ScUser scuser=users.get(i); long id=scuser.getId(); String dept =scuser.getDept(); String name=scuser.getUsername(); String pwd=scuser.getPassword(); String ip=scuser.getUserIP(); String mac=scuser.getMAC(); String status=scuser.getStatus()+""; if(status.equals("F")){ status="启用"; }else{ status="禁用"; } String[] contents={id+"",dept,name,pwd,ip,mac,status}; for(int j=0;j<contents.length;j++){ Label labe=new Label(j,i+1,contents[j]); sheet.addCell(labe); } } book.write(); book.close(); os.flush(); } catch (Exception e) { e.printStackTrace(); } } }
提示:需要jxl的jar包及示例程序(百度网盘):链接:http://pan.baidu.com/s/1dKvV4 密码:io6w
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。