欢迎来到Doc100.Net免费学习资源知识分享平台!
您的位置:首页 > 程序异常 >

poi 生成excel表格一般步骤

更新时间: 2014-01-05 03:07:32 责任编辑: Author_N1

 

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);
    }

 

上一篇:上一篇
下一篇:下一篇

 

随机推荐程序问答结果

 

 

如对文章有任何疑问请提交到问题反馈,或者您对内容不满意,请您反馈给我们DOC100.NET论坛发贴求解。
DOC100.NET资源网,机器学习分类整理更新日期::2014-01-05 03:07:32
如需转载,请注明文章出处和来源网址:http://www.doc100.net/bugs/t/18631/
本文WWW.DOC100.NET DOC100.NET版权所有。