使用poi实现导出数据到Excel

第一步:导入poi所需的jar包

地址:https://pan.baidu.com/s/1mkuX89QQfaY48YNyxDZndw 

 第二步:创建控制器

注意: 各位只需要替换userList集合即可

@RequestMapping(value="/daochu.html")
	public void daochu(HttpServletRequest request,HttpServletResponse response){
		//获得所有用户
		List<User> userList = userService.getUserList(null,0,1,userService.getUserCount(null, 0));
		
		//---------------------------------------------------------下面执行导出操作-----------------------------------------
		String docsPath = request.getSession().getServletContext().getRealPath("document");
		String fileName = "用户列表_" + System.currentTimeMillis() + ".xlsx";
		String filePath = docsPath + File.separator + fileName;
		try {
			// 输出流
			OutputStream os = new FileOutputStream(filePath);
			// 工作区
			XSSFWorkbook wb = new XSSFWorkbook();
			XSSFSheet sheet = wb.createSheet("用户表");
			//---------------设置excel表格样式----------------------------
			 XSSFCellStyle cellStyle = wb.createCellStyle();        //表格样式
			 Font cellFont = wb.createFont();   
			 XSSFRow row = null;
			 XSSFCell cell = null;
			 cellFont.setFontHeightInPoints((short)10);    // 将字体大小设置为18px
			 cellFont.setColor(Font.COLOR_NORMAL);            // 将字体设置为“红色” 
			 cellFont.setFontName("宋体");             // 字体应用到当前单元格上 
			 cellStyle.setFont(cellFont);
			 cellStyle.setWrapText(true);//设置自动换行
			 cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 水平居中
			 cellStyle.setAlignment(HSSFCellStyle.VERTICAL_CENTER);//垂直居中  
			 
			// ------------------创建表头start---------------------
			row = sheet.createRow(0); // 创建第一行
			cell = row.createCell(0);
			cell.setCellValue("序号");
			cell.setCellStyle(cellStyle);
			
			cell = row.createCell(1);
			cell.setCellValue("用户编码");
			cell.setCellStyle(cellStyle);
			
			cell = row.createCell(2);
			cell.setCellValue("用户名");
			cell.setCellStyle(cellStyle);
			
			cell = row.createCell(3);
			cell.setCellValue("性别");
			cell.setCellStyle(cellStyle);
			
			cell = row.createCell(4);
			cell.setCellValue("年龄");
			cell.setCellStyle(cellStyle);
			
			cell = row.createCell(5);
			cell.setCellValue("电话");
			cell.setCellStyle(cellStyle);
			sheet.setColumnWidth((short)5, (short)200 * 256);
			
			cell = row.createCell(6);
			cell.setCellValue("用户角色");
			cell.setCellStyle(cellStyle);
			sheet.setColumnWidth((short)6, (short)255 * 256);
			
			for (int i = 0; i < userList.size(); i++) {
				// 创建第一个sheet
				    row = sheet.createRow(i+1);
					// 给这一行的第一列赋值
					row.createCell(0).setCellValue(i+1);
					// 给这一行的第二列赋值
					row.createCell(1).setCellValue(userList.get(i).getUserCode());
					// 给这一行的第三列赋值
					row.createCell(2).setCellValue(userList.get(i).getUserName());
					// 给这一行的第四列赋值
					String gender=null;
					if(userList.get(i).getGender()==1){
						gender="女";
					}else{
						gender="男";
					}
					row.createCell(3).setCellValue(gender);
					// 给这一行的第五列赋值
					row.createCell(4).setCellValue(userList.get(i).getAge());
					// 给这一行的第六列赋值
					row.createCell(5).setCellValue(userList.get(i).getPhone());
					// 给这一行的第七列赋值
					row.createCell(6).setCellValue(userList.get(i).getUserRoleName());
					//设置内容自适应
					sheet.autoSizeColumn(i+1);
			}
			// 写文件
			wb.write(os);
			// 关闭输出流
			os.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		try {
			// path是指欲下载的文件的路径。
			File file = new File(filePath);
			// 取得文件名。
			String filename = file.getName();
			// 以流的形式下载文件。
			InputStream fis = new BufferedInputStream(new FileInputStream(filePath));
			byte[] buffer = new byte[fis.available()];
			fis.read(buffer);
			fis.close();
			// 清空response
			response.reset();
			// 设置response的Header
			response.addHeader("Content-Disposition", "attachment;filename="
					+ new String(filename.getBytes(),"ISO-8859-1"));
			response.addHeader("Content-Length", "" + file.length());
			OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
			response.setContentType("application/vnd.ms-excel;charset=gb2312");
			toClient.write(buffer);
			toClient.flush();
			toClient.close();
		} catch (IOException ex) {
			ex.printStackTrace();
		}
	}

 第三步:测试

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值