YangTao
厌浅尝辄止喜有始有终
E.t's Blog
poi操作excel表格
poi操作excel表格

近期有用到JAVA操作导出excel的功能,采用了poi,但用下来觉得还不如layui前端框架导出好用,但各有各的功能;

附上一个简单的Demo

//定义表头
		String[] title={"序号","用户名","姓名","省份","城市","详细地址","身份证号","手机号码"};
		//创建excel工作簿
		HSSFWorkbook workbook=new HSSFWorkbook();
		//创建工作表sheet
		HSSFSheet sheet=workbook.createSheet();
		//创建第一行
		HSSFRow row=sheet.createRow(0);
		HSSFCell cell=null;
		//插入第一行数据的表头
		for(int i=0;i<title.length;i++){
		    cell=row.createCell(i);
		    cell.setCellValue(title[i]);
		}
		//写入数据
		SqlSession session =MybatisUtils.creatSqlSession();
		List<VpsBuy> buys =session.getMapper(GetInfo.class).getAllVpsBuy();
		int i=0;
		for (VpsBuy vsp : buys) {
			Member member=session.getMapper(GetInfo.class).getMemberInfo(vsp.getUsername());
			i++;
			HSSFRow nrow=sheet.createRow(i);
		    HSSFCell ncell=nrow.createCell(0);
		    ncell.setCellValue(""+i);
		    //username
		    ncell=nrow.createCell(1);
		    ncell.setCellValue(member.getUsername());
		    //getPrv
		    ncell=nrow.createCell(2);
		    ncell.setCellValue(member.getTruename());
		    //getCity
		    ncell=nrow.createCell(3);
		    ncell.setCellValue(member.getPrv());
		    //getAddress
		    ncell=nrow.createCell(4);
		    ncell.setCellValue(member.getCity());
		    //getAddress
		    ncell=nrow.createCell(5);
		    ncell.setCellValue(member.getAddress());
		    //getAddress
		    ncell=nrow.createCell(6);
		    ncell.setCellValue(member.getIDcard());
		    ncell=nrow.createCell(7);
		    ncell.setCellValue(member.getTel());
		}
		//创建excel文件
		File file=new File("D:\\1232.xlsx");
		try {
		    file.createNewFile();
		    //将excel写入
		    FileOutputStream stream= FileUtils.openOutputStream(file);
		    workbook.write(stream);
		    stream.close();
		    System.out.println("ok");
		} catch (IOException e) {
		    e.printStackTrace();

	}

对应的包:poi-4.0.0.jar commons-logging-1.2.jar commons-io-2.4.jar

没有标签
首页      Java      poi操作excel表格
https://secure.gravatar.com/avatar/77f815bec37eb34e2eef92ae146f899a?s=256&d=mm&r=g

et

文章作者

发表评论

textsms
account_circle
email

E.t's Blog

poi操作excel表格
近期有用到JAVA操作导出excel的功能,采用了poi,但用下来觉得还不如layui前端框架导出好用,但各有各的功能; 附上一个简单的Demo //定义表头 String[] title={"序号","用户名","姓名…
扫描二维码继续阅读
2018-11-09