昨天看了一个例子,是用户在填写注册信息的时候,能够导出该用户的注册信息,现在仅仅实现了这个功能,这是直接在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","p_w_upload;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(); }