java中导出Excel表

2 篇文章 0 订阅

java中导出Excel表

需要的jar包:

<dependency>
		<groupId>org.apache.poi</groupId>
		<artifactId>poi</artifactId>
		<version>3.9</version>
</dependency>

实体类:

public class student{
	@Column
    private String name;//姓名
    
    @Column
    private String gender;//性别
    
    @Column
    private int age;//年龄
    
	@Column
    private String date;//出生日期
    
    @Column
    private String familyAddress;//家庭地址
}

需要导出的字段:

public class studentQueryForm {
	private String name;//姓名
    private String gender;//性别
    private int age;//年龄
}

业务层接口类:

	/**
	 * 导出表格
	 */
	HSSFWorkbook exportBlock(List<student> list);

业务层实现类:

	/**
	 * 导出表格
	 */
	 @Override
	 public HSSFWorkbook exportBlock(List<student> list) {
		HSSFWorkbook wb = new HSSFWorkbook();//创建HSSFWorkbook对象
        HSSFSheet sheet = wb.createSheet("基本安置数据");//创建HSSFSheet对象
        sheet.setColumnWidth(0, 30 * 256);
        sheet.setColumnWidth(1, 20 * 256);
        sheet.setColumnWidth(2, 20 * 256);
      
        HSSFCellStyle cellStyle=wb.createCellStyle();
        cellStyle.setAlignment(HSSFCellStyle.ALIGN_LEFT);// 设置单元格的横向和纵向对齐方式
        cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
        HSSFFont fontStyle=wb.createFont();
        fontStyle.setFontName("宋体");//设置字体样式
        fontStyle.setFontHeightInPoints((short)11);//设置字体高度
        cellStyle.setFont(fontStyle);// 将字体对象赋值给单元格样式对象
        HSSFRow row = sheet.createRow(0);//创建HSSFRow对象
        row.setHeightInPoints(30);
        HSSFCell cell=row.createCell(0);//创建HSSFCell对象
        cell.setCellStyle(cellStyle);
        cell.setCellValue("姓名");
        cell = row.createCell(1);
        cell.setCellStyle(cellStyle);
        cell.setCellValue("性别");
        cell = row.createCell(2);
        cell.setCellStyle(cellStyle);
        cell.setCellValue("年龄");
        for(int i = 0;i<list.size();i++){
        	Info orders = list.get(i);
            row = sheet.createRow(i+1);//创建HSSFRow对象
            row.setHeightInPoints(30);
            cell=row.createCell(0);//创建HSSFCell对象
            cell.setCellStyle(cellStyle);
            cell.setCellValue(orders.getName());//姓名
            cell = row.createCell(1);
            cell.setCellStyle(cellStyle);
            cell.setCellValue(orders.getGender());//性别
            cell = row.createCell(2);
            cell.setCellStyle(cellStyle);
            cell.setCellValue(orders.getAge());//年龄
        }
        return wb;
	 }

控制层:

	/**
	 * 导出到Excel信息表格
	 */
	@RequestMapping("/studentExcle")
	@ResponseBody
	public String studentExcle(studentQueryForm form,HttpServletResponse response){
		String result;
		try {
			List<student> ordersList = studentService.getStudent(form);
			HSSFWorkbook workbook = studentService.exportBlock(ordersList);
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");
			Date date = new Date();
			String filedisplay = "学生基本数据"+sdf.format(date)+".xls";
			filedisplay = new String(filedisplay.getBytes("gbk"), "iso8859-1");
			response.setContentType("application/vnd.ms-excel;charset=utf-8");
			response.setHeader("Content-Disposition", "attachment;filename="+filedisplay);
			OutputStream out = response.getOutputStream();
			workbook.write(out);
			out.flush();
			out.close();
			result = "导出成功";
		} catch (Exception e) {
			result = "发生异常";
			e.printStackTrace();
		}
		return result;
	}

前端页面

 <button type="button" class="btn waves-effect waves-light btn-info" id="export">导出</button>
 //导出安置数据
$("#export").on("click",function(){
	var name=$("#name").val();
    var gender=$("#gender").val();
    var age=$("#age").val();
                
    window.location.href='请求路径/exportExcle?name='+name+'&gender='+gender+'&age='+age;
 });
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值