poi 生成excel报表一般步骤

//表题

//表头

//数据

//合并区域

//是否显示网格线

//行号

//外边框加粗

//内边框普通

//设置打印区域

//打印设置

 

	public static  Workbook getWorkbook(ProjectStatReporter reporter){
		List<String> x=reporter.getX();
		List<String> f=reporter.getStatField();
		List<List<String>> y=reporter.getY();
		Workbook workbook = new HSSFWorkbook();
		Sheet sheet=workbook.createSheet();
		//x行  和度量值
		Row rowx=sheet.createRow(0);
		Row rowf=sheet.createRow(1);
		
		Font font = workbook.createFont();
	    font.setFontName("宋体");
	    font.setBoldweight(Font.BOLDWEIGHT_BOLD);

		for(int i=0;i<x.size();i++){
			//x
			RichTextString richString = new HSSFRichTextString(x.get(i));
			richString.applyFont(font);   
			createCell(workbook, rowx, i*f.size()+1, CellStyle.ALIGN_CENTER, CellStyle.VERTICAL_BOTTOM,richString);
			//度量值 
			for(int j=0;j<f.size();j++){				
				RichTextString richString2 = new HSSFRichTextString(f.get(j));
				richString2.applyFont(font);   
				createCell(workbook, rowf, i*f.size()+1+j, CellStyle.ALIGN_CENTER, CellStyle.VERTICAL_BOTTOM,richString2);
		    }
		
		}
		
		//y数据
		
		for(int i=0;i<y.size();i++){
			Row row=sheet.createRow(i+2);
			for(int j=0;j<y.get(i).size();j++){
				if(j==0){//第一列  字体加粗
					RichTextString richString = new HSSFRichTextString(y.get(i).get(j));
					richString.applyFont(font);   
					createCell(workbook, row, j, CellStyle.ALIGN_CENTER, CellStyle.VERTICAL_BOTTOM,richString);
				}else{
					createCell(workbook, row, j, CellStyle.ALIGN_CENTER, CellStyle.VERTICAL_BOTTOM,y.get(i).get(j));
				}
				
			}
		}
		//合并区域
		sheet.addMergedRegion(new CellRangeAddress(0,1,0,0));//左上角的空白
		for(int i=0;i<x.size();i++){//合并x
	      sheet.addMergedRegion(new CellRangeAddress(0,0,i*f.size()+1,i*f.size()+1+f.size()-1));
		}
		//样式:不显示网格线 居中  行号25.5  外边框加粗   内边框普通   row0 1 字体宋体加粗  col 1 字体宋体加粗
		//不显示网格线
		sheet.setDisplayGridlines(false);
		//居中   row0 1 字体宋体加粗  col 1 字体宋体加粗
		             //调用createcell()
		//行号25.5
		for(int i=0;i<2+y.size();i++){
			Row row=sheet.getRow(i);
			row.setHeightInPoints((float) 25.5);
		}
		//外边框加粗
		
		CellRangeAddress region=new CellRangeAddress(0,2+y.size()-1,0,x.size()*f.size());
		RegionUtil.setBorderBottom(CellStyle.BORDER_THICK, region, sheet, workbook);
		RegionUtil.setBorderLeft(CellStyle.BORDER_THICK, region, sheet, workbook);
		RegionUtil.setBorderRight(CellStyle.BORDER_THICK, region, sheet, workbook);
		RegionUtil.setBorderTop(CellStyle.BORDER_THICK, region, sheet, workbook);
          
		//内边框普通
		    //调用createcell() 中设置
		
		//返回
		//测试
		Sheet sheet1 = workbook.createSheet("new sheet");

	    sheet1.groupRow( 5, 14 );
	    sheet1.groupRow( 7, 14 );
	    sheet1.groupRow( 16, 19 );

	    sheet1.groupColumn( (short)4, (short)7 );
	    sheet1.groupColumn( (short)9, (short)12 );
	    sheet1.groupColumn( (short)10, (short)11 );


		return  workbook;
	}

  

   

	private static void createCell(Workbook wb, Row row, int column, short halign, short valign,String text){
        Cell cell = row.createCell(column);
        cell.setCellValue(text);
        CellStyle cellStyle = wb.createCellStyle();
        cellStyle.setAlignment(halign);
        cellStyle.setVerticalAlignment(valign);
        
        cellStyle.setBorderBottom(CellStyle.BORDER_THIN);
        cellStyle.setBorderLeft(CellStyle.BORDER_THIN);
        cellStyle.setBorderRight(CellStyle.BORDER_THIN);
        cellStyle.setBorderTop(CellStyle.BORDER_THIN);
        cell.setCellStyle(cellStyle);
    }
	
	private static void createCell(Workbook wb, Row row, int column, short halign, short valign,RichTextString richText){
        Cell cell = row.createCell(column);
        cell.setCellValue(richText);
        CellStyle cellStyle = wb.createCellStyle();
        cellStyle.setAlignment(halign);
        cellStyle.setVerticalAlignment(valign);
        cell.setCellStyle(cellStyle);
        cellStyle.setBorderBottom(CellStyle.BORDER_THIN);
        cellStyle.setBorderLeft(CellStyle.BORDER_THIN);
        cellStyle.setBorderRight(CellStyle.BORDER_THIN);
        cellStyle.setBorderTop(CellStyle.BORDER_THIN);
        cell.setCellStyle(cellStyle);
    }

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值