java使用POI读取properties文件并写到Excel的方法
本文实例讲述了java使用POI读取properties文件并写到Excel的方法。分享给大家供大家参考。具体实现方法如下:
packagecom.hubberspot.code; importjava.io.File; importjava.io.FileInputStream; importjava.io.FileNotFoundException; importjava.io.FileOutputStream; importjava.io.IOException; importjava.util.Enumeration; importjava.util.HashMap; importjava.util.Iterator; importjava.util.Properties; importorg.apache.poi.hssf.usermodel.HSSFCell; importorg.apache.poi.hssf.usermodel.HSSFCellStyle; importorg.apache.poi.hssf.usermodel.HSSFRichTextString; importorg.apache.poi.hssf.usermodel.HSSFRow; importorg.apache.poi.hssf.usermodel.HSSFSheet; importorg.apache.poi.hssf.usermodel.HSSFWorkbook; importorg.apache.poi.hssf.util.HSSFColor; publicclassReadWriteXlsProperties{ //CreateaHashMapwhichwillstoretheproperties HashMap<String,String>propMap=newHashMap<String,String>(); publicstaticvoidmain(String[]args){ //CreateobjectofReadWriteXlsProperties ReadWriteXlsPropertiesreadWriteXlsDemo=newReadWriteXlsProperties(); //CallmethodreadProperties()ittakepathtopropertiesfile readWriteXlsDemo.readProperties("config.properties"); //CallmethodwriteToExcel()itwilltakepathtoexcelfile readWriteXlsDemo.writeToExcel("test.xls"); } privatevoidreadProperties(StringpropertiesFilePath){ //CreateaFileobjecttakinginpathofproperties //file FilepropertiesFile=newFile(propertiesFilePath); //Ifpropertiesfileisafiledobelowstuff if(propertiesFile.isFile()) { try { //CreateaFileInputStreamforloadingthepropertiesfile FileInputStreamfisProp=newFileInputStream(propertiesFile); //CreateaPropertiesobjectandload //propertieskeyandvaluetoitthroughFileInputStream Propertiesproperties=newProperties(); properties.load(fisProp); //CreateaobjectofEnumerationandcallkeys() //methodoverpropertiesobjectcreatedabove //itwillreturnusbackwithaEnumerationtypes Enumeration<Object>keysEnum=properties.keys(); //LoopingovertheelementsofEnumeration while(keysEnum.hasMoreElements()) { //Extractingthekeyandrespectivevaluesfromit. StringpropKey=(String)keysEnum.nextElement(); StringpropValue=(String)properties.getProperty(propKey); //Afterextractingthekeyandvaluefromthepropertiesfile //wewillstorethevaluesinaHashMap. propMap.put(propKey.toLowerCase().trim(),propValue.toLowerCase().trim()); } //printingtheHashMapandclosingthefileFileInputStream System.out.println("PropertiesMap...\n"+propMap); fisProp.close(); } catch(FileNotFoundExceptione) { e.printStackTrace(); } catch(IOExceptione) { e.printStackTrace(); } } } privatevoidwriteToExcel(StringexcelPath){ //CreateaWorkbookusingHSSFWorkbookobject HSSFWorkbookworkBook=newHSSFWorkbook(); //Createasheetwithname"properties"by //thecreateSheetmethodoftheWorkbook HSSFSheetworksheet=workBook.createSheet("Properties"); //CreatearowbycallingcreateRowmethodofthe //Worksheet HSSFRowrow=worksheet.createRow((short)0); //CreateacellstylebycallingcreateCellStyle() //fromtheworkbook HSSFCellStylecellStyle=workBook.createCellStyle(); //settingoftheforegroundandfillpatternbycallingmethods //ofHSSFCellStyleassetFillForegroundColor()andsetFillPattern() cellStyle.setFillForegroundColor(HSSFColor.GOLD.index); cellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); //CreateaHSSFCellfromtherowobjectcreatedabove HSSFCellcell1=row.createCell(0); //Settingthevalueofthecellasthekeysbycalling //setCellValue()methodovertheHSSFCell cell1.setCellValue(newHSSFRichTextString("Keys")); //Givingitthestylecreatedabove. cell1.setCellStyle(cellStyle); HSSFCellcell2=row.createCell(1); cell2.setCellValue(newHSSFRichTextString("Values")); cell2.setCellStyle(cellStyle); //CreateaIteratorandaspropMapisaHashMap //itisconvertedtoaHashSetbycallingkeySet()method //whichwillreturnwithSet. //IteratorobjectispointedtokeysofSet Iterator<String>iterator=propMap.keySet().iterator(); //LoopingacrosstheelementsofIterator while(iterator.hasNext()) { //Creatinganewrowfromtheworksheet //atthelastusedrow+1location HSSFRowrowOne=worksheet.createRow(worksheet.getLastRowNum()+1); //Creatingtwocellsintherowat0and1position. HSSFCellcellZero=rowOne.createCell(0); HSSFCellcellOne=rowOne.createCell(1); //extractingkeyandvaluefromthemapandset Stringkey=(String)iterator.next(); Stringvalue=(String)propMap.get(key); //settingtheextractedkeysandvaluesinthecells cellZero.setCellValue(newHSSFRichTextString(key)); cellOne.setCellValue(newHSSFRichTextString(value)); } try{ FileOutputStreamfosExcel=null; //CreatingaxlsFile FilefileExcel=newFile(excelPath); //SettingtheFiletoFileOutputStream fosExcel=newFileOutputStream(fileExcel); //Writingthecontentsofworkbooktothexls workBook.write(fosExcel); //FlushingtheFileOutputStream fosExcel.flush(); //ClosingtheFileOutputStream fosExcel.close(); }catch(Exceptione){ e.printStackTrace(); } } }
希望本文所述对大家的java程序设计有所帮助。