问题描述
可是还是因为要创建HSSFRow对象的缘故所以写好的每一行被覆盖了!!!while(it.hasNext()){index++;row=sheet.createRow(index);Tt=(T)it.next();//利用反射,根据javabean属性的先后顺序,动态调用getXxx()方法得到属性值Field[]fields=t.getClass().getDeclaredFields();j=fields.length;for(shorti=0;i<(fields.length+1);i++){if(i==(fields.length+1)){for(inty=0;y<k+2;y++){HSSFCellcell9=row.createCell((short)j-1);cell9.setCellFormula("ROUND(E"+(y+3)+"*"+totalPrice+",2)");System.out.println("ROUND(E"+(y+3)+"*"+totalPrice+",2)");}break;}HSSFCellcell=row.createCell(i);cell.setCellStyle(style2);Fieldfield=fields[i];StringfieldName=field.getName();StringgetMethodName="get"+fieldName.substring(0,1).toUpperCase()+fieldName.substring(1);try{ClasstCls=t.getClass();MethodgetMethod=tCls.getMethod(getMethodName,newClass[]{});Objectvalue=getMethod.invoke(t,newObject[]{});//判断值的类型后进行强制类型转换StringtextValue=null;if(valueinstanceofDate){Datedate=(Date)value;SimpleDateFormatsdf=newSimpleDateFormat(pattern);textValue=sdf.format(date);}else{//其它数据类型都当作字符串简单处理textValue=value.toString();}//如果不是图片数据,就利用正则表达式判断textValue是否全部由数字组成if(textValue!=null){Patternp=Pattern.compile("^\d+(\.\d+)?$");Matchermatcher=p.matcher(textValue);if(matcher.matches()){//是数字当作double处理cell.setCellValue(Double.parseDouble(textValue));}else{HSSFRichTextStringrichString=newHSSFRichTextString(textValue);HSSFFontfont3=workbook.createFont();font3.setColor(HSSFColor.BLUE.index);richString.applyFont(font3);cell.setCellValue(richString);}}}catch(SecurityExceptione){e.printStackTrace();}catch(NoSuchMethodExceptione){e.printStackTrace();}catch(IllegalArgumentExceptione){e.printStackTrace();}catch(IllegalAccessExceptione){e.printStackTrace();}catch(InvocationTargetExceptione){e.printStackTrace();}finally{}}}
!for(shorti=0;i<(fields.length+1);i++){if(i==(fields.length+1)){for(inty=0;y<k+2;y++){HSSFCellcell9=row.createCell((short)j-1);cell9.setCellFormula("ROUND(E"+(y+3)+"*"+totalPrice+",2)");System.out.println("ROUND(E"+(y+3)+"*"+totalPrice+",2)");}break;}为什么这错了
解决方案
本帖最后由 yangchengang1111 于 2010-04-23 10:33:06 编辑
解决方案二:
ddddddddddddddddd