【JAVA,POI】生成excel文件

【JAVA,POI】生成excel文件

项目上遇到一个需要生成Excel的操作,然后就一通查找资源。这里就是做下记录。

关于实现生成excel主流的有两种方法:

1.使用jxl.jar的jar包提供的API;

2.使用Apache提供的POI API。

我自己使用的是,第二种方法。其中需要的jar包现在地址:http://poi.apache.org/download.html#POI

下面是代码:

public void ExportExcel(String sheetName, String titleName,String fileName, 
			List<String> columnName, List<String> columnId, IBizMessage pShowMeaasge ) throws Exception {
		
		//Start 测试使用
				int columnNum = 0;
				int rowNum    = pShowMeaasge.getBusinessParameters().getTable("RECORDS").getRowCount();
				columnName = new ArrayList<String>();
				columnName.add("预算科目编号");
				columnName.add("预算科目名称");
				columnName.add("预算单位编号");
				columnName.add("预算单位名称");
				columnName.add("预算部门编号");
				columnName.add("预算部门名称");
				columnName.add("预算项目编号");
				columnName.add("预算项目名称");
				for(int month = 1;month <= 12 ;month ++){
					columnName.add(month+"月预算数");
					columnName.add(month+"月冻结数");
					columnName.add(month+"月执行数");
					columnName.add(month+"月剩余数");
					columnName.add(month+"月调整数");
					columnName.add(month+"月上月结转数");
				}
				
				columnName.add("年度预算数");
				columnName.add("年度冻结数");
				columnName.add("年度执行数");
				columnName.add("年度剩余数");
				columnName.add("年度调整数");
				columnNum = columnName.size();
				columnId = new ArrayList<String>();
				columnId.add("F_YSBH");
				columnId.add("F_YSMC");
				columnId.add("F_DWBH");
				columnId.add("F_DWMC");
				columnId.add("F_BMBH");
				columnId.add("F_BMMC");
				columnId.add("F_XMBH");
				columnId.add("F_XMMC");
				for(int month = 1;month <= 12 ;month ++){
					columnId.add("F_YSSM"+month);
					columnId.add("F_DJSM"+month);
					columnId.add("F_ZXSM"+month);
					columnId.add("F_SYSM"+month);
					columnId.add("F_TZSM"+month);
					columnId.add("F_JZSM"+month);
				}
				columnId.add("F_YYSS");
				columnId.add("F_YDJS");
				columnId.add("F_YZXS");
				columnId.add("F_YSYS");
				columnId.add("F_YTZS");
				
				
				//End   测试使用
				// 第一步,创建一个webbook,对应一个Excel文件
				HSSFWorkbook wb = new HSSFWorkbook();
				// 第二步,在webbook中添加一个sheet,对应Excel文件中的sheet
				HSSFSheet sheet = wb.createSheet(sheetName);
				sheet.setDefaultColumnWidth(15); //统一设置列宽
				//sheet.autoSizeColumn(1, true);
				
				//设置sheet的
				
				// 创建第0行 也就是标题
				HSSFRow row1 = sheet.createRow((int) 0);
				row1.setHeightInPoints(50);// 设备标题的高度
				// 第三步创建标题的单元格样式style2以及字体样式headerFont1
				HSSFCellStyle style2 = wb.createCellStyle();
				style2.setAlignment(HSSFCellStyle.ALIGN_CENTER);
				style2.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
				style2.setFillForegroundColor(HSSFColor.LIGHT_TURQUOISE.index);
				style2.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
				HSSFFont headerFont1 = (HSSFFont) wb.createFont(); // 创建字体样式
				headerFont1.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); // 字体加粗
				headerFont1.setFontName("黑体"); // 设置字体类型
				headerFont1.setFontHeightInPoints((short) 15); // 设置字体大小
				style2.setFont(headerFont1); // 为标题样式设置字体样式
				
				HSSFCell cell1 = row1.createCell(0);// 创建标题第一列
				sheet.addMergedRegion(new CellRangeAddress(0, 0, 0,
						columnNum - 1)); // 合并第0到第多少列
				cell1.setCellValue(titleName); // 设置值标题
				cell1.setCellStyle(style2); // 设置标题样式
				
				// 创建第1行 也就是表头
				HSSFRow row = sheet.createRow((int) 1);
				row.setHeightInPoints(37);// 设置表头高度
				
				// 第四步,创建表头单元格样式 以及表头的字体样式
				HSSFCellStyle style = wb.createCellStyle();
				style.setWrapText(true);// 设置自动换行
				style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
				style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); // 创建一个居中格式

				style.setBottomBorderColor(HSSFColor.BLACK.index);
				style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
				style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
				style.setBorderRight(HSSFCellStyle.BORDER_THIN);
				style.setBorderTop(HSSFCellStyle.BORDER_THIN);

				HSSFFont headerFont = (HSSFFont) wb.createFont(); // 创建字体样式
				headerFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); // 字体加粗
				headerFont.setFontName("黑体"); // 设置字体类型
				headerFont.setFontHeightInPoints((short) 10); // 设置字体大小
				style.setFont(headerFont); // 为标题样式设置字体样式
				
				for (int i = 0; i < columnNum; i++) 
				{
					HSSFCell cell = row.createCell(i);
					cell.setCellValue(columnName.get(i));
					cell.setCellStyle(style);
				}
				//List<String> tempList = new ArrayList<String>();
				HSSFCellStyle cellStyleOne = wb.createCellStyle();
				cellStyleOne.setWrapText(true);// 设置自动换行
				cellStyleOne.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); // 创建一个居中格式
				// 设置边框
				cellStyleOne.setBottomBorderColor(HSSFColor.BLACK.index);
				cellStyleOne.setBorderBottom(HSSFCellStyle.BORDER_THIN);
				cellStyleOne.setBorderLeft(HSSFCellStyle.BORDER_THIN);
				cellStyleOne.setBorderRight(HSSFCellStyle.BORDER_THIN);
				cellStyleOne.setBorderTop(HSSFCellStyle.BORDER_THIN);
				HSSFCellStyle cellStyleTwo = wb.createCellStyle();
				cellStyleTwo.setWrapText(true);// 设置自动换行
				cellStyleTwo.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); // 创建一个上下居中格式
				cellStyleTwo.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 左右居中
				// 设置边框
				cellStyleTwo.setBottomBorderColor(HSSFColor.BLACK.index);
				cellStyleTwo.setBorderBottom(HSSFCellStyle.BORDER_THIN);
				cellStyleTwo.setBorderLeft(HSSFCellStyle.BORDER_THIN);
				cellStyleTwo.setBorderRight(HSSFCellStyle.BORDER_THIN);
				cellStyleTwo.setBorderTop(HSSFCellStyle.BORDER_THIN);
				// 为数据内容设置特点新单元格样式3不自动换行 上下居中右对齐,千分符以及保留两个小数
				HSSFCellStyle cellStyleThree = wb.createCellStyle();
				cellStyleThree.setWrapText(false);// 设置自动换行
				cellStyleThree.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); // 创建一个上下居中格式
				cellStyleThree.setAlignment(HSSFCellStyle.ALIGN_RIGHT);// 右对齐
				// 设置边框
				cellStyleThree.setBottomBorderColor(HSSFColor.BLACK.index);
				cellStyleThree.setBorderBottom(HSSFCellStyle.BORDER_THIN);
				cellStyleThree.setBorderLeft(HSSFCellStyle.BORDER_THIN);
				cellStyleThree.setBorderRight(HSSFCellStyle.BORDER_THIN);
				cellStyleThree.setBorderTop(HSSFCellStyle.BORDER_THIN);
				HSSFDataFormat format= wb.createDataFormat();
				cellStyleThree.setDataFormat(format.getFormat("#,##0.00"));
				IStructure   pListMessage = null;
				for (int i = 0; i < rowNum; i++) 
				{
					pListMessage = pShowMeaasge.getBusinessParameters().getTable("RECORDS").getRow(i);
					row = sheet.createRow((int) i + 2);
					// 为数据内容设置特点新单元格样式1 自动换行 上下居中
					

					// 为数据内容设置特点新单元格样式2 自动换行 上下居中左右也居中
					
					
					/**/
					HSSFCell datacell = null;
					for (int j = 0; j < columnNum; j++) 
					{
						//tempList = columnData.get(j);sheet.autoSizeColumn(1, true);
						if(j<8){
							datacell = row.createCell(j);
							datacell.setCellValue(pListMessage.AsString(columnId.get(j)));
							datacell.setCellStyle(cellStyleTwo);
						}else{
							datacell = row.createCell(j);
							datacell.setCellValue(Double.parseDouble(pListMessage.AsString(columnId.get(j))));
							datacell.setCellStyle(cellStyleThree);
						}
					}
					System.err.println();
				}
		try {
			String budgetUserConditionPath  = TProfiles.getProperty("file_transfer", "budget_user_condition","");
			File file = new File(budgetUserConditionPath);
			if(!file.exists()){  
			    file.mkdirs();  
			} 
			FileOutputStream fout = new FileOutputStream(budgetUserConditionPath+"\\"+fileName+".xls");
			//FileOutputStream fout = new FileOutputStream(budgetUserConditionPath+"\\预算1.xls");
			sheet.createFreezePane(8, 0);
			wb.write(fout);
			String str = "导出" + fileName + "成功!";
			System.out.println(str);
			fout.close();
		} catch (Exception e) {
			e.printStackTrace();
			String str1 = "导出" + fileName + "失败!";
			System.out.println(str1);
		}finally{
		}
		
	}

这里没有对代码进行优化,为测试的DEMO大致看下。

                // 第一步,创建一个webbook,对应一个Excel文件
                HSSFWorkbook wb = new HSSFWorkbook();
                // 第二步,在webbook中添加一个sheet,对应Excel文件中的sheet,sheetName为sheet的名字,如下图的红框标识处
                HSSFSheet sheet = wb.createSheet(sheetName);

 sheet.createFreezePane(8, 0);

可以利用createFreezePane对excel面板进行冻结。

 

主要需要进行描述的东西不多,实现还是很简单的,可能后面需要进行优化,将列名以及参数的提取,然后最好,进行通用处理。也可以通过实现一个接口,然后根据不同的业务对生成excel的方法进行编码,业务实现。

第一次写博客,可能有很多不合适的地方,包括语言组织,具体时实现,欢迎大家提出建议以及指导。

 

参考博客:https://blog.csdn.net/baijianjun123456/article/details/51035145

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值