码迷,mamicode.com
首页 > 数据库 > 详细

Java导出页面数据或数据库数据至Excel文件并下载,采用JXL技术,小demo(servlet实现)

时间:2015-04-17 17:58:56      阅读:179      评论:0      收藏:0      [点我收藏+]

标签:

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

Java导出页面数据或数据库数据至Excel文件并下载,采用JXL技术,小demo(servlet实现)

标签:

原文地址:http://www.cnblogs.com/dreamzhiya/p/4435244.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!