初試poi HssfWorkBook導出excel

昨天看了一個例子,是用戶在填寫註冊信息的時候,能夠導出該用戶的註冊信息,現在僅僅實現了這個功能,這是直接在servlet裏面寫的!

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
            response.setContentType("application/ vnd.ms-excel");//設置正文的MIME類型爲excel           
        String name = request.getParameter("name");
        String psswd = request.getParameter("passwd");
        String sex = request.getParameter("sex");
        String email = request.getParameter("email");
        String fileName = "userinfo.xls";
        response.setHeader("Content-disposition","attachment;filename="+fileName);
        ServletOutputStream out = response.getOutputStream();//響應輸出流對象
        HSSFWorkbook wb = new HSSFWorkbook();//創建excel表格
        HSSFSheet sheet = wb.createSheet("用戶註冊信息");//創建工作簿
        sheet.setColumnWidth(4, 5000);//設置列寬
        HSSFRow titleRow = sheet.createRow(0);//設置標題行
        HSSFCell titleCell = titleRow.createCell(0);//第一個單元格
        titleCell.setCellValue("用戶姓名");//第一個值
        HSSFCell titleCel2 = titleRow.createCell(1);
        titleCel2.setCellValue("用戶密碼");
        HSSFCell titleCel3 = titleRow.createCell(2);
        titleCel3.setCellValue("用戶性別");
        HSSFCell titleCel4 = titleRow.createCell(3);
        titleCel4.setCellValue("用戶郵箱");
        HSSFRow rowValue = sheet.createRow(1);//設置第二行
        HSSFCell nameCell = rowValue.createCell(0);
        nameCell.setCellValue(name);
        HSSFCell passCell = rowValue.createCell(1);
        passCell.setCellValue(psswd);
        HSSFCell sexCell = rowValue.createCell(2);
        sexCell.setCellValue(sex);
        HSSFCell emailCell = rowValue.createCell(3);
        emailCell.setCellValue(email);
        wb.createCellStyle();
        wb.write(out);
        out.flush();
        out.close();
    }


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章