通过java反射实现的excel数据导出


Excel.java


    @SuppressWarnings("deprecation")
	public static  <T> void ExportExcel(String title, String[] headers,Collection<T> dataset, OutputStream out) throws IOException, SecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException, NoSuchMethodException, InvocationTargetException{
		     
		        //第一种方法 通过实体反射机制
		        // 声明一个工作薄
				HSSFWorkbook workbook = new HSSFWorkbook();
				// 生成一个表格
				HSSFSheet sheet = workbook.createSheet(title);
				// 设置表格默认列宽度为15个字节
				sheet.setDefaultColumnWidth((short) 15);
				// 生成一个样式
				HSSFCellStyle style = workbook.createCellStyle();
				// 设置这些样式
				style.setFillForegroundColor(HSSFColor.SKY_BLUE.index);
				style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
				style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
				style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
				style.setBorderRight(HSSFCellStyle.BORDER_THIN);
				style.setBorderTop(HSSFCellStyle.BORDER_THIN);
				style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
				// 生成一个字体
				HSSFFont font = workbook.createFont();
				font.setColor(HSSFColor.VIOLET.index);
				font.setFontHeightInPoints((short) 12);
				font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
				// 把字体应用到当前的样式
				style.setFont(font);
				// 生成并设置另一个样式
				HSSFCellStyle style2 = workbook.createCellStyle();
				style2.setFillForegroundColor(HSSFColor.LIGHT_YELLOW.index);
				style2.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
				style2.setBorderBottom(HSSFCellStyle.BORDER_THIN);
				style2.setBorderLeft(HSSFCellStyle.BORDER_THIN);
				style2.setBorderRight(HSSFCellStyle.BORDER_THIN);
				style2.setBorderTop(HSSFCellStyle.BORDER_THIN);
				style2.setAlignment(HSSFCellStyle.ALIGN_CENTER);
				style2.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
				// 生成另一个字体
				HSSFFont font2 = workbook.createFont();
				font2.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL);
				// 把字体应用到当前的样式
				style2.setFont(font2);
				// 产生表格标题行
				HSSFRow row = sheet.createRow(0);
				for (short i = 0; i < headers.length; i++) {
					HSSFCell cell = row.createCell(i);
					cell.setCellStyle(style);
					HSSFRichTextString text = new HSSFRichTextString(headers[i]);
					cell.setCellValue(text);
				}
				// 遍历集合数据,产生数据行
				Iterator<T> it = dataset.iterator();
				int index = 0;
				while(it.hasNext()){
					index++;
					
					row = sheet.createRow(index);//创建一行
					Object t =  it.next();
					// 利用反射,根据javabean属性的先后顺序,动态调用getXxx()方法得到属性值
					Field[] fields = t.getClass().getDeclaredFields();//得到所有属性
					//循环javabean属性
					for(short i=0;i<fields.length;i++){
						 HSSFCell cell = row.createCell(i);
						    cell.setCellStyle(style2);
						    //得到T第一个javabean属性
						    //fields[i].setAccessible(true);
						   // System.out.println("iiiiiiiiiiiii"+fields[i].get(t));
						    Field field = fields[i];
							String fieldName = field.getName();//得到get方法名
							System.out.println(field);//private java.lang.Integer com.zcc.entity.AccountBean.userid
							String[] split=field.toString().split(" ");//目的是判断有没有boolean类型的数据
							String getMethodName="";
						    //bolean类型 Is开头 其他get开头 得到get方法
							if("boolean".equalsIgnoreCase(split[1])||"bool".equalsIgnoreCase(split[1])){
								getMethodName = "is"
										+ fieldName.substring(0, 1).toUpperCase()
										+ fieldName.substring(1);
							}else {
								getMethodName = "get"
										+ fieldName.substring(0, 1).toUpperCase()
										+ fieldName.substring(1);
							}
							Class tCls = t.getClass();
							Method getMethod = tCls.getMethod(getMethodName,new Class[] {});
							Object value = getMethod.invoke(t, new Object[] {});
							//值已经得到了  下面可能需要根据类型不同进行判断 ,省略。。。。。
							cell.setCellValue(value.toString());
					}
				}
		
				workbook.write(out);
				out.close();
    }

原文地址:https://www.cnblogs.com/lovellll/p/10222623.html