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
时间: 2024-10-05 05:04:47