poi实现数据库数据导出到excel

第一步:在你的pom文件里面引入poi的依赖

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

第二部:在后台获取数据

try {
			// 第一步,创建一个webbook,对应一个Excel文件
			HSSFWorkbook wb = new HSSFWorkbook();
			// 第二步,在webbook中添加一个sheet,对应Excel文件中的sheet
			HSSFSheet sheet = wb.createSheet("项目详情");
			// 第三步,在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制short
			HSSFRow row = sheet.createRow((int) 0);
			// 第四步,创建单元格,并设置值表头 设置表头居中
			HSSFCellStyle style = wb.createCellStyle();
			HSSFFont font = wb.createFont();
			font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);//粗体显示
			style.setFont(font);
			style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式
			
			// 设置表格默认列宽度
			sheet.setDefaultColumnWidth(15);

			row.setHeight((short)350);
			HSSFCell cell = row.createCell(0);
			cell.setCellValue("项目归属委");
			cell.setCellStyle(style);
			cell = row.createCell(1);
			cell.setCellValue("项目归属BMU");
			cell.setCellStyle(style);
			cell = row.createCell(2);
			cell.setCellValue("所属行业");
			cell.setCellStyle(style);
			cell = row.createCell(3);
			cell.setCellValue("子业务");
			cell.setCellStyle(style);
			cell = row.createCell(4);
			cell.setCellValue("公司全称或项目名称(地域+简写)");
			cell.setCellStyle(style);
			cell = row.createCell(5);
			cell.setCellValue("项目经理");
			cell.setCellStyle(style);
			cell = row.createCell(6);
			cell.setCellValue("推荐人");
			cell.setCellStyle(style);
			cell = row.createCell(7);
			cell.setCellValue("项目阶段");
			cell.setCellStyle(style);
			cell = row.createCell(8);
			cell.setCellValue("本周进展");
			cell.setCellStyle(style);
			cell = row.createCell(9);
			cell.setCellValue("优先级");
			cell.setCellStyle(style);
			cell = row.createCell(10);
			cell.setCellValue("公司阶段");
			cell.setCellStyle(style);
			cell = row.createCell(11);
			cell.setCellValue("项目公司类型");
			cell.setCellStyle(style);
			cell = row.createCell(12);
			cell.setCellValue("公司简介");
			cell.setCellStyle(style);
			cell = row.createCell(13);
			cell.setCellValue("历史财务1年度收入(万)");
			cell.setCellStyle(style);
			cell = row.createCell(14);
			cell.setCellValue("历史财务1年度净利润(万)");
			cell.setCellStyle(style);
			cell = row.createCell(15);
			cell.setCellValue("历史财务2年度收入(万)");
			cell.setCellStyle(style);
			cell = row.createCell(16);
			cell.setCellValue("历史财务2年度净利润(万)");
			cell.setCellStyle(style);
			cell = row.createCell(17);
			cell.setCellValue("预测财务1年度预计收入(万)");
			cell.setCellStyle(style);
			cell = row.createCell(18);
			cell.setCellValue("预测财务1年度预计净利润(万)");
			cell.setCellStyle(style);
			cell = row.createCell(19);
			cell.setCellValue("预测财务2年度预计收入(万)");
			cell.setCellStyle(style);
			cell = row.createCell(20);
			cell.setCellValue("预测财务2年度预计净利润(万)");
			cell.setCellStyle(style);
			cell = row.createCell(21);
			cell.setCellValue("预测财务3年度预计收入(万)");
			cell.setCellStyle(style);
			cell = row.createCell(22);
			cell.setCellValue("预测财务3年度预计净利润(万)");
			cell.setCellStyle(style);
			cell = row.createCell(23);
			cell.setCellValue("项目估值分析");
			cell.setCellStyle(style);
			cell = row.createCell(24);
			cell.setCellValue("投前估值(万)");
			cell.setCellStyle(style);
			cell = row.createCell(25);
			cell.setCellValue("投资形式");
			cell.setCellStyle(style);
			cell = row.createCell(26);
			cell.setCellValue("融资规模(万)");
			cell.setCellStyle(style);
			cell = row.createCell(27);
			cell.setCellValue("公司主页/信息来源网址");
			cell.setCellStyle(style);
			cell = row.createCell(28);
			cell.setCellValue("备注");
			cell.setCellStyle(style);
			
			// 第五步,写入实体数据 实际应用中这些数据从数据库得到,
			List list = queryProjectAllVOsList(request, maps);
			for (int i = 0; i < list.size(); i++) {
				row = sheet.createRow((int)i+1);
				// 设置表格行高度
				row.setHeight((short) 300);
				ProjectAllVO prVo = (ProjectAllVO)list.get(i);
				
				row.createCell((short) 0).setCellValue(prVo.getCommittee());	//项目归履委
				row.createCell((short) 1).setCellValue(prVo.getBmu());//项目归属BMU
				row.createCell((short) 2).setCellValue(prVo.getIndustry_plate());//所属行业
				row.createCell((short) 3).setCellValue(prVo.getSub_service());//子业务
				row.createCell((short) 4).setCellValue(prVo.getEnterprise_name());//公司全称或项目名称(地域+简写)
				row.createCell((short) 5).setCellValue(prVo.getUsername());//项目经理
				row.createCell((short) 6).setCellValue(prVo.getCue_man());//推荐人
				//项目阶段
				if("1".equals(prVo.getProject_phase())) {
					row.createCell((short) 7).setCellValue("BMU预立项");
				}
				if("2".equals(prVo.getProject_phase())) {
					row.createCell((short) 7).setCellValue("立项评审");
				}
				if("3".equals(prVo.getProject_phase())) {
					row.createCell((short) 7).setCellValue("尽职调查阶段");
				}
				if("4".equals(prVo.getProject_phase())) {
					row.createCell((short) 7).setCellValue("投资决策评审");
				}
				if("5".equals(prVo.getProject_phase())) {
					row.createCell((short) 7).setCellValue("募资阶段");
				}
				if("6".equals(prVo.getProject_phase())) {
					row.createCell((short) 7).setCellValue("投资完成");
				}
				if("7".equals(prVo.getProject_phase())) {
					row.createCell((short) 7).setCellValue("投后管理");
				}
				if("8".equals(prVo.getProject_phase())) {
					row.createCell((short) 7).setCellValue("暂缓投资");
				}
				if("0".equals(prVo.getProject_status())) {
					row.createCell((short) 7).setCellValue("新项目待分配");
				}
				if("1".equals(prVo.getProject_status())) {
					row.createCell((short) 7).setCellValue("待分配项目经理");
				}
				if("2".equals(prVo.getProject_status())) {
					row.createCell((short) 7).setCellValue("BMU预立项");
				}
				
				row.createCell((short) 8).setCellValue(prVo.getContent());//本周进展
				//优先级
				if("1".equals(prVo.getPriority_level())) {
					row.createCell((short) 9).setCellValue("优先级1");
				}
				if("2".equals(prVo.getPriority_level())) {
					row.createCell((short) 9).setCellValue("优先级2");
				}
				if("3".equals(prVo.getPriority_level())) {
					row.createCell((short) 9).setCellValue("优先级3");
				}
				if("4".equals(prVo.getPriority_level())) {
					row.createCell((short) 9).setCellValue("优先级4");
				}
				//公司阶段
				if("0".equals(prVo.getStage())) {
					row.createCell((short) 10).setCellValue("初创期");
				}
				if("1".equals(prVo.getStage())) {
					row.createCell((short) 10).setCellValue("早中期");
				}
				if("2".equals(prVo.getStage())) {
					row.createCell((short) 10).setCellValue("发展期");
				}
				if("3".equals(prVo.getStage())) {
					row.createCell((short) 10).setCellValue("成熟期");
				}
				if("4".equals(prVo.getStage())) {
					row.createCell((short) 10).setCellValue("持续成熟期");
				}
				if("5".equals(prVo.getStage())) {
					row.createCell((short) 10).setCellValue("衰退期");
				}
				//项目公司类型
				if("1".equals(prVo.getProject_type())) {
					row.createCell((short) 11).setCellValue("抓手公司");
				}
				if("2".equals(prVo.getProject_type())) {
					row.createCell((short) 11).setCellValue("协同公司");
				}
				row.createCell((short) 12).setCellValue(prVo.getCompany_profile());//公司简介
				
				//前年年度收入(万)和预计收入,数据结构为 2016,500,600用逗号分隔,第一位为年份第二位为年度收入(万)第三位为预计收入。
				if(!StringUtil.isEmpty(prVo.getThe_year_before_last()) && !" ".equals(prVo.getThe_year_before_last().split("/")[0])) {
					row.createCell((short) 13).setCellValue(prVo.getThe_year_before_last().split("/")[0]+"年:"+prVo.getThe_year_before_last().split("/")[1]+" "+prVo.getThe_year_before_last().split("/")[2]);
					row.createCell((short) 14).setCellValue(prVo.getThe_year_before_last().split("/")[0]+"年:"+prVo.getThe_year_before_last().split("/")[3]+" "+prVo.getThe_year_before_last().split("/")[4]);
				}else{
					row.createCell((short) 13).setCellValue("");
					row.createCell((short) 14).setCellValue("");				
				}
				//去年年度收入(万)和预计收入,数据结构为 2016,500,600用逗号分隔,第一位为年份第二位为年度收入(万)第三位为预计收入。
				if(!StringUtil.isEmpty(prVo.getLast_year()) && !" ".equals(prVo.getLast_year().split("/")[0])) {
					row.createCell((short) 15).setCellValue(prVo.getLast_year().split("/")[0]+"年:"+prVo.getLast_year().split("/")[1]+" "+prVo.getLast_year().split("/")[2]);
					row.createCell((short) 16).setCellValue(prVo.getLast_year().split("/")[0]+"年:"+prVo.getLast_year().split("/")[3]+" "+prVo.getLast_year().split("/")[4]);
				}else{
					row.createCell((short) 15).setCellValue("");
					row.createCell((short) 16).setCellValue("");				
				}
				//今年年度收入(万)和预计收入,数据结构为 2016,500,600用逗号分隔,第一位为年份第二位为年度收入(万)第三位为预计收入。
				if(!StringUtil.isEmpty(prVo.getThis_year()) && !" ".equals(prVo.getThis_year().split("/")[0])) {
					row.createCell((short) 17).setCellValue(prVo.getThis_year().split("/")[0]+"年:"+prVo.getThis_year().split("/")[1]+" "+prVo.getThis_year().split("/")[2]);
					row.createCell((short) 18).setCellValue(prVo.getThis_year().split("/")[0]+"年:"+prVo.getThis_year().split("/")[3]+" "+prVo.getThis_year().split("/")[4]);
				}else{
					row.createCell((short) 17).setCellValue("");
					row.createCell((short) 18).setCellValue("");				
				}
				//明年年度收入(万)和预计收入,数据结构为 2016,500,600用逗号分隔,第一位为年份第二位为年度收入(万)第三位为预计收入。
				if(!StringUtil.isEmpty(prVo.getNext_year()) && !" ".equals(prVo.getNext_year().split("/")[0])) {
					row.createCell((short) 19).setCellValue(prVo.getNext_year().split("/")[0]+"年:"+prVo.getNext_year().split("/")[1]+" "+prVo.getNext_year().split("/")[2]);
					row.createCell((short) 20).setCellValue(prVo.getNext_year().split("/")[0]+"年:"+prVo.getNext_year().split("/")[3]+" "+prVo.getNext_year().split("/")[4]);
				}else{
					row.createCell((short) 19).setCellValue("");
					row.createCell((short) 20).setCellValue("");				
				}
				//后年年度收入(万)和预计收入,数据结构为 2016,500,600用逗号分隔,第一位为年份第二位为年度收入(万)第三位为预计收入。
				if(!StringUtil.isEmpty(prVo.getThe_year_after_next()) && !" ".equals(prVo.getThe_year_after_next().split("/")[0])) {
					row.createCell((short) 21).setCellValue(prVo.getThe_year_after_next().split("/")[0]+"年:"+prVo.getThe_year_after_next().split("/")[1]+" "+prVo.getThe_year_after_next().split("/")[2]);
					row.createCell((short) 22).setCellValue(prVo.getThe_year_after_next().split("/")[0]+"年:"+prVo.getThe_year_after_next().split("/")[3]+" "+prVo.getThe_year_after_next().split("/")[4]);
				}else{
					row.createCell((short) 21).setCellValue("");
					row.createCell((short) 22).setCellValue("");				
				}
				row.createCell((short) 23).setCellValue(prVo.getProject_valuation_analysis());//项目估值分析
				row.createCell((short) 24).setCellValue(prVo.getInvestment_valuation());//投前估值(万)
				row.createCell((short) 25).setCellValue(prVo.getInvestment_form());//投资形式
				row.createCell((short) 26).setCellValue(prVo.getFinancing_scale());//融资规模(万)
				row.createCell((short) 27).setCellValue(prVo.getCompany_homepage());//公司主页/信息来源网址
				row.createCell((short) 28).setCellValue(prVo.getRemark());//备注
			}
			OutputStream outputStream = response.getOutputStream();
			response.setContentType("application/force-download");
			response.addHeader("Content-Disposition","attachment; filename=" + URLEncoder.encode(fileName, "UTF-8"));
			
			wb.write(outputStream);
			outputStream.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
3:效果图


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值