The example of this article tells the method of Java using Poi to read the Properties file and write to Excel. Share it for everyone for your reference. The specific implementation method is as follows:
package com.hubberspot.code; Import Java.iO.File; Import Java.FileInputStream; Import Java.filenotFoundexception; Import utstream; Import Java.io.ioException; Import Java.util.enumeration; Import java.util.hashmap; Import java.util.Iterator; Import java.util.properties; import organ .poi.hssf.Usermodel.hssfcellStyle; Import org.apache .poi.hssf.Usermodel.hssfrichtextString; Import org.apache.hssf.Usermodel.hssfrow; Import port org.apache.poi.hssf.Usermodel.hssfworkbook; Import org .apache.poi.hssf.util.hssfcolor; Public Class ReadwritexlSproperties {// Create A HashMap Which Will Store The Properties HashMap <string > propmap = New HashMap <string, String> (); Public Static Void Main (String [[String ] ARGS) {// Create Object of ReadWritexlSproperties ReadwritexlSproperties ReadwritexlSDEMO = New ReadWritexlSpropers (); /// call medhod Adproperties () It take Path to Properties File ReadWritexlsdemo.ReadProperties ("Config.properties"); // Call Method WriteExcel () It Will take path to excel file readritexlsdemo.writetoexcel ("test.xls");} Private Void ReadProperties (String PropertiesFilepath) {// Create A File CT TAKING in PATH of Properties // File File PropertiesFile = New File (PropertiesFilepath); / / / If Properties File is a File Do Below Stuff If (PropertiesFile.isfile ()) {Try {// Create A FileInputStream for Loading The Properties FileInputStream Fisprop = New FileInputStream (PropertiesFile); // Create A Properties Object and LOAD // Properties key and value to it through fileinputstream proprTies properties = New Properties (); Properties.load (FISPROP); // Create A Object of ENUMER ATION and CALL Keys () // Method Over Properties Object Created Above // It Will Return us back With a enumeration types enumeration <object> Keysenum = Properties.keys (); // Looping Over the Elements of Enumeration While / Extrating the key and resin from it. String propkey = (string) Keysenum.nextelement (); String PropValue = (String) Properties.getProperty (Propkey); // After expect the key and value from the problem // We will store the values in a havemap. Propmap.put (propkey.tolowercase ( ) .trim (), PropValue.tologyCase (). Trim ());} // Printing the hashmap and closing the file fileinputstream system.out.println ("PROPERTIES MAP ... /N" ropmap); fisprop.close ();} Catch (FilenotFoundexception E) {e.printstacktrace ();} Catch (IOEXCEPTION E) {e.printstacktrace ();}}} Private VoidOEEEEXcel (Str ing ExcelPath) {// Create A Workbook using hssfringbook object hssforkbook workbook = New hssforkbook (); // Create a sheet with name "proprTies" by // The createsheet method of the workbook hssheet workSheet = workbook.createSheet ("p Roperties "); // Create a Row by Calling Calling Method of the // Worksheet hssfrow row = Worksheet.createrow ((Short) 0); // Create a Cell Style by Calling Createcellstyle () // From the Workbook Hssfcellstyle = Workbook.creat EcellStyle (); // Setting of the Foreground and Fill Pattern by Calling Methods // of hssfcellstyle as setfillforegroundcolor () and setFillpattern () CellStyle.SetFillforegroundcolor (hssfcolor.gold.index); CellStyle.SetFil lpattern (hssfcellstyle.solid_Foreground); // Create A hssfcell from the row object created above hssfcell cell1 = row.createcell (0); // setting the value of the cell as the keys by calling // setCellValue () Method over the hssfcell cell1.SetcellValue (New HSSFRICHTEXTRING ("KEYS "); // giving it the style created above. Cell1. SetCellStyle (CellStyle); hssfcell cell2 = row.createcell (1); Cell2.SetCellValue (New HSSFRICHTEXTRING ("Values"); Cell2.SetCellStyle (CellStyle); // Record a Iterator and as propmap is a havemap // it is Converted to a hashSet by Calling KeySet () Method // Which Will Return with Set. // Iterator Object Is Points of Set ITERTOR <strong> .keyset (). Iterator (); // looping across the elements Of the iterator while (iterator.hasnext ()) {// Creating a new row from the worksheet // at the last userd row + 1 localfroW rowone = Worksheet.createrow (worldSheet. getlastropnum ()+1); // Creating two Cells in the row at 0 and 1 posity. hssfcell cellzero = rowone.createcell (0); hssfcell cellone = rowone.createcell (1); // Extracting key and value from the map. and set string key = (string) Iterator.next (); String value = (string) propmap.get (key); // setting the excbract keys and value in the cells cellzero.SetCellValue (New Key); Cellone.setcellValue (New HSSFRICHTEXTRING (VALUE));} Try {fileoutstream fosexcel = null; // Creating a xls file file filexcel = new file (ExcelPath); // setting the file to fileoutstream fileout putstream (FileExcel); // write the contents of workbook to the xls workbook.write (Fosexcel); // Flushing The FileoutPutStream Fosexcel.flush (); // CLOSING The FileoutStream Fosexcel.close ();} Catch (Exception E) { race ();}}}
It is hoped that this article is helpful to everyone's Java program design.