packagecom.ibm.fsw.pr.util;importjava.io.File;importjava.io.FileInputStream;importjava.io.FileNotFoundException;importjava.io.FileOutputStream;importjava.io.IOException;importjava.io.InputStream;importjava.math.BigDecimal;importjava.rmi.RemoteException;importjava.sql.SQLException;importjava.util.ArrayList;importjava.util.Collection;importjava.util.Date;importjavax.ejb.CreateException;importjavax.naming.NamingException;importorg.apache.poi.hssf.usermodel.HSSFCell;importorg.apache.poi.hssf.usermodel.HSSFCellStyle;importorg.apache.poi.hssf.usermodel.HSSFRow;importorg.apache.poi.hssf.usermodel.HSSFSheet;importorg.apache.poi.hssf.usermodel.HSSFWorkbook;importorg.apache.poi.hssf.util.HSSFColor;importorg.apache.poi.poifs.filesystem.POIFSFileSystem;importcom.ibm.ccibm.infra.CCException;importcom.ibm.fsw.pr.vo.PriceVO;/** * @author agodinho */publicclassPriceHelper{publicstaticInputStreambuildExcel(finalStringtemplatePath)throwsCCException,SQLException,NamingException,RemoteException,CreateException{// SSPricingAccessBean ejb = new SSPricingAccessBean();// RowSet rws = ejb.getPriceRowSet( txYear, txMonth, cdBrand );Filetemp=newFile(templatePath+"\\PriceTemplate.xls");if(temp.exists())temp.delete();try{if(!temp.createNewFile())System.out.println("bichou ...");}catch(finalIOExceptione){}// TODO: need a better solution to this. // I have a input stream and a wb created, but - yet - need to write then in disk ...InputStreamexcel=null;HSSFWorkbookwb=newHSSFWorkbook();HSSFSheetsheet=wb.createSheet("Price List");sheet.setSelected(true);// if( rws == null ) {// }// else {// BrandVO brand = ejb.getBrand( cdBrand );// BrandType bt = brand.getBrandType();// boolean bPC = bt.equals( BrandType.PC );// boolean bNORMAL = bt.equals( BrandType.NORMAL );// if( !bPC && !bNORMAL )// throw new CCException( "FactorHelper.BuildSampleExcel: Invalid Brand Type." ); // Style the cell with borders all around.HSSFCellStylestyleL=wb.createCellStyle();styleL.setBorderTop(HSSFCellStyle.BORDER_MEDIUM);styleL.setBorderLeft(HSSFCellStyle.BORDER_MEDIUM);styleL.setBorderBottom(HSSFCellStyle.BORDER_MEDIUM);styleL.setFillBackgroundColor(HSSFColor.GREY_25_PERCENT.index);HSSFCellStylestyleM=wb.createCellStyle();styleM.setBorderTop(HSSFCellStyle.BORDER_MEDIUM);styleM.setBorderBottom(HSSFCellStyle.BORDER_MEDIUM);styleM.setFillBackgroundColor(HSSFColor.GREY_25_PERCENT.index);HSSFCellStylestyleR=wb.createCellStyle();styleR.setBorderTop(HSSFCellStyle.BORDER_MEDIUM);styleR.setBorderBottom(HSSFCellStyle.BORDER_MEDIUM);styleR.setBorderRight(HSSFCellStyle.BORDER_MEDIUM);styleR.setFillBackgroundColor(HSSFColor.GREY_25_PERCENT.index);HSSFRowrow;HSSFCellcel;shortiRow=1;shortiCel=1;row=sheet.createRow(iRow++);cel=row.createCell(iCel++);cel.setCellType(HSSFCell.CELL_TYPE_STRING);cel.setCellValue("Type");cel.setCellStyle(styleL);cel=row.createCell(iCel++);cel.setCellType(HSSFCell.CELL_TYPE_STRING);cel.setCellValue("Model");cel.setCellStyle(styleM);cel=row.createCell(iCel++);cel.setCellType(HSSFCell.CELL_TYPE_STRING);cel.setCellValue("Feature");cel.setCellStyle(styleM);cel=row.createCell(iCel++);cel.setCellType(HSSFCell.CELL_TYPE_STRING);cel.setCellValue("Description");cel.setCellStyle(styleM);cel=row.createCell(iCel++);cel.setCellType(HSSFCell.CELL_TYPE_STRING);cel.setCellValue("Category");cel.setCellStyle(styleM);cel=row.createCell(iCel++);cel.setCellType(HSSFCell.CELL_TYPE_STRING);cel.setCellValue("List Price");cel.setCellStyle(styleM);cel=row.createCell(iCel++);cel.setCellType(HSSFCell.CELL_TYPE_STRING);cel.setCellValue("Amt Retail");cel.setCellStyle(styleM);cel=row.createCell(iCel++);cel.setCellType(HSSFCell.CELL_TYPE_STRING);cel.setCellValue("Amt Whlse");cel.setCellStyle(styleR);// while( rws.next() ) {// }sheet.setColumnWidth((short)1,(short)1500);// typesheet.setColumnWidth((short)2,(short)1500);// modelsheet.setColumnWidth((short)3,(short)1500);// featuresheet.setColumnWidth((short)4,(short)3000);// descriptionsheet.setColumnWidth((short)5,(short)3000);// categorysheet.setColumnWidth((short)6,(short)2000);// vl1 sheet.setColumnWidth((short)7,(short)2000);// vl2sheet.setColumnWidth((short)8,(short)2000);// vl3// only to go home;row=sheet.createRow(0);cel=row.createCell((short)0);// }try{FileOutputStreamoutput=newFileOutputStream(temp);wb.write(output);output.close();excel=newFileInputStream(temp);}catch(finalFileNotFoundExceptione){System.out.println("Arquivo bichado ..."+e.getMessage());}catch(finalIOExceptione){System.out.println("Arquivo bichado ..."+e.getMessage());}returnexcel;}publicstaticCollectionreadExcel(finalStringfullFileName,finalStringtxMonthYearB,finalStringtxMonthYearE,finalIntegercdBrand,finalbooleanbNORMAL)throwsCCException,SQLException,NamingException,RemoteException,CreateException{Collectioncol=newArrayList();try{FileInputStreamfis=newFileInputStream(fullFileName);POIFSFileSystemfs=newPOIFSFileSystem(fis);HSSFWorkbookwb=newHSSFWorkbook(fs);HSSFSheetsheet=wb.getSheetAt(0);Dated[]=XHelper.getDatesBetweenBeginEnd(XHelper.formatStringMMyyyyToDate(txMonthYearB),XHelper.formatStringMMyyyyToDate(txMonthYearE));shortiRow=0;shortiCel=0;HSSFRowrow=null;HSSFCellcel=null;booleanerror=false;Stringvalue="";try{row=sheet.getRow(iRow++);cel=row.getCell(iCel);error=cel==null;if(!error)value=cel.getStringCellValue();error=!value.equalsIgnoreCase("Type");}catch(finalExceptione){error=true;}if(error)thrownewCCException("Invalid Microsoft Excel Template.");inter=0,ec=0;booleanhasData=true;while(hasData){iCel=0;ec=0;PriceVOvo=newPriceVO((short)(iRow+1));vo.setCdBrand(cdBrand);row=sheet.getRow(iRow++);for(inti=1;i<9;i++){booleanunread=false;value="";try{cel=row.getCell(iCel++);intt=cel.getCellType();switch(t){caseHSSFCell.CELL_TYPE_BLANK:unread=true;break;caseHSSFCell.CELL_TYPE_BOOLEAN:value=String.valueOf(cel.getBooleanCellValue());break;caseHSSFCell.CELL_TYPE_ERROR:value=String.valueOf(cel.getErrorCellValue());// byte? what this thing means?break;caseHSSFCell.CELL_TYPE_FORMULA:try{value=String.valueOf(cel.getNumericCellValue());break;}catch(finalExceptione){/*/ can't get as numeric ... /*/}try{value=cel.getStringCellValue();break;}catch(finalExceptione){/*/ can't get as string ... /*/}value=cel.getCellFormula();break;caseHSSFCell.CELL_TYPE_NUMERIC:value=String.valueOf(cel.getNumericCellValue());break;caseHSSFCell.CELL_TYPE_STRING:value=cel.getStringCellValue();break;default:/*/ Unknow cell data. /*/unread=true;}}catch(finalExceptione){unread=true;}// empty cells raises error, this is normal ...if(bNORMAL&&i<4&&value.indexOf('.')>-1){// must ignore points in id fields (if have one, sure);Stringn[];n=XHelper.parseKey(value,".");value=n[0];}if(unread)ec++;elsevalue=value.trim().toUpperCase();try{switch(i){case1:vo.setCdMachineType(value);break;// can be empty //case2:vo.setCdModel(value);break;case3:vo.setCdFeature(value);break;case4:vo.setTxDescription(value);break;case5:vo.setTxCategory(value);break;case6:vo.setVlAmtHedge(newBigDecimal(value.equals("")?"0.00":value));break;case7:vo.setVlAmtRetail(newBigDecimal(value.equals("")?"0.00":value));break;case8:vo.setVlAmtWhlse(newBigDecimal(value.equals("")?"0.00":value));break;default:}}catch(finalExceptione){/*/ some dummy error inside /*/}}// if have less than 6 empty columns (of 8 columns);if(ec<6){// iterate in array dates to build factors in interval;intl=d.length;for(inti=0;i<l;i++){Strings=XHelper.formatDateToStringMMyyyy(d[i]);Stringa[]=XHelper.parseKey(s,"-");PriceVOclone=(PriceVO)vo.clone();clone.setTxMonth(a[0]);clone.setTxYear(a[1]);col.add(clone);}}er=(ec<6?0:er+1);hasData=(er<3);}// to help gc, this objects can be heavy; sheet=null;wb=null;fs=null;fis.close();fis=null;}catch(finalExceptione){thrownewCCException(e);}returncol;}}