码迷,mamicode.com
首页 > 编程语言 > 详细

java生成excel报表

时间:2015-01-19 21:09:46      阅读:278      评论:0      收藏:0      [点我收藏+]

标签:java   excel报表   apache poi   

使用 apache poi包来实现该功能

包可以通过官网下载,也可以通过 http://download.csdn.net/detail/ch717828/8361309 下载

接下来看代码

private static String xls2003 = "myexcel.xls";

public static void main(String[] args) {
		
		//创建2003 excel
		String filePath = generateExcel2003(xls2003);
		System.out.println(filePath+" success");
		
	}


/**
	 * 创建2003文件的方法
	 * 
	 * @param filePath
	 */
	public static String generateExcel2003(String filePath) {
			
		// 先创建工作簿对象
		HSSFWorkbook wb = new HSSFWorkbook();
		//创建表头样式
		HSSFFont headerfont = wb.createFont();
	    headerfont.setFontHeightInPoints((short)18);
	    headerfont.setFontName("Helvetica");
	    headerfont.setBoldweight((short)18);
	    HSSFCellStyle headerstyle = wb.createCellStyle();
        headerstyle.setFont(headerfont);
        headerstyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
        headerstyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
        headerstyle.setWrapText(true);
        
        //创建普通样式
        HSSFFont font = wb.createFont();
        font.setFontHeightInPoints((short)12);
        font.setFontName("Helvetica");
        HSSFCellStyle cellstyle=wb.createCellStyle();
        cellstyle.setFont(font);
        cellstyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
        cellstyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
        cellstyle.setWrapText(true);
 
//       默认空样式
        HSSFCellStyle nonstyle =wb.createCellStyle();
        
    	// 创建工作表对象并命名
		HSSFSheet sheet = wb.createSheet("工作表1");	
		//设置列的宽度
		sheet.setColumnWidth(0,32*80);
		for(int i=1;i<=22;i++)
		{
			sheet.setColumnWidth(i,32*30);
		}
		sheet.setColumnWidth(21,32*100);
/*----------------第一行---------------------------------------*/
		HSSFRow row1 = sheet.createRow(0);
		//设置高度
		row1.setHeightInPoints(55);
		HSSFCell cell = createCell(wb,row1,0,cellstyle,"xxxxxxxxxxxxxxx");
		cell.setCellStyle(headerstyle);
	
		//合并单元格,前两个参数是行上索引,后两个参数是列上索引
		sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 21)); 	
/*----------------第二行---------------------------------------*/
		//换行		
		HSSFRow row2 = sheet.createRow(1);
		//时间:2014年12月 
		createCell(wb,row2,1,cellstyle,"时间:2014年12月 ");
		//合并单元格
		sheet.addMergedRegion(new CellRangeAddress(1, 1, 1, 21)); 		
/*----------------第三行---------------------------------------*/
		HSSFRow row3= sheet.createRow(2);
		row3.setHeightInPoints(30);
		createCell(wb,row3,0,cellstyle,"XXXX");
		//合并单元格
		sheet.addMergedRegion(new CellRangeAddress(2,3,0,0)); 		
		createCell(wb,row3,1,cellstyle,"XXXX");
		sheet.addMergedRegion(new CellRangeAddress(2,2,1,4)); 
		createCell(wb,row3,5,cellstyle,"XXXX");
		sheet.addMergedRegion(new CellRangeAddress(2,2,5,8)); 
		createCell(wb,row3,9,cellstyle,"XXXX");
		sheet.addMergedRegion(new CellRangeAddress(2,2,9,12)); 
		createCell(wb,row3,16,cellstyle,"XXXX");
		sheet.addMergedRegion(new CellRangeAddress(2,2,13,16)); 
		createCell(wb,row3,17,cellstyle,"XXXX");
		sheet.addMergedRegion(new CellRangeAddress(2,2,17,20)); 
		createCell(wb,row3,21,cellstyle,"XXXX"); 
		sheet.addMergedRegion(new CellRangeAddress(2,3,21,21)); 
/*----------------第四行---------------------------------------*/
		HSSFRow row4 = sheet.createRow(3);
		row4.setHeightInPoints(50);
		for(int i=1;i<=20;i++)
		{
			switch(i%4)
			{
			case 1: 
				createCell(wb,row4,i,cellstyle,"好");
				break;
			case 2:	
				createCell(wb,row4,i,cellstyle,"较好");
				break;
			case 3:	
				createCell(wb,row4,i,cellstyle,"一般");
				break;	
			case 0:	
				createCell(wb,row4,i,cellstyle,"差");
				break;	
			}
		}
/*----------------第五行---------------------------------------*/
		HSSFRow row5 = sheet.createRow(4);
		row5.setHeightInPoints(50);
		createCell(wb,row5,0,cellstyle,"A");
/*----------------第六行---------------------------------------*/		
		HSSFRow row6 = sheet.createRow(5);
		row6.setHeightInPoints(50);
		createCell(wb,row6,0,cellstyle,"B");
/*----------------第七行---------------------------------------*/		
		HSSFRow row7 = sheet.createRow(6);
		row7.setHeightInPoints(50);
		createCell(wb,row7,0,cellstyle,"C");		
/*----------------第八行---------------------------------------*/		
		HSSFRow row8 = sheet.createRow(7);
		row8.setHeightInPoints(50);
		createCell(wb,row8,0,cellstyle,"合计");	
/*----------------第九行---------------------------------------*/	
		HSSFRow row9 = sheet.createRow(8);
		createCell(wb,row9,0,nonstyle,"备注:1、参加测评人员范围:班子成员、中层干部及有关代表;\r"+
										   "2、发出民主测评表184张,收回184张,其中空白表1张; \r"+
														"A表:班子成员,收回8张; \r"+
														"B表:中层干部,收回149张;\r"+
														"C表:其他人员,收回27张。");	
		sheet.addMergedRegion(new CellRangeAddress(8,8,0,16)); 	
		
		
/*----------------生成文件---------------------------------------*/	
		createFile(filePath,wb);
		
		return filePath;
	}

	public static void createFile(String filePath,HSSFWorkbook wb)
	{
		
		// 生成文件
		File file = new File(filePath);
		FileOutputStream fos = null;
		try {
			fos = new FileOutputStream(file);
			wb.write(fos);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (fos != null) {
				try {
					fos.close();
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
	}

	
	/**
     * Creates a cell and aligns it a certain way.
     *
     * 参数
     * 	excel模版
     *  模版行引用
     *  列下标
     *  单元格样式
     *  单元格内容
     */
    private static HSSFCell createCell(HSSFWorkbook wb, HSSFRow row, int column, HSSFCellStyle cellStyle,String content) {
        HSSFCell cell = row.createCell(column);
        cell.setCellValue(content);
        cell.setCellStyle(cellStyle);
        return cell;
    }

完整的项目可以通过 http://download.csdn.net/detail/ch717828/8376987 下载

java生成excel报表

标签:java   excel报表   apache poi   

原文地址:http://blog.csdn.net/ch717828/article/details/42876551

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!