Java uses POI to read properties files and write to Excel methods _java

Source: Internet
Author: User

The example in this article describes how Java uses POI to read properties files and write to Excel. Share to everyone for your reference. The implementation method is as follows:

Package Com.hubberspot.code;
Import Java.io.File;
Import Java.io.FileInputStream;
Import java.io.FileNotFoundException;
Import Java.io.FileOutputStream;
Import java.io.IOException;
Import java.util.Enumeration;
Import Java.util.HashMap;
Import Java.util.Iterator;
Import java.util.Properties;
Import Org.apache.poi.hssf.usermodel.HSSFCell;
Import Org.apache.poi.hssf.usermodel.HSSFCellStyle;
Import org.apache.poi.hssf.usermodel.HSSFRichTextString;
Import Org.apache.poi.hssf.usermodel.HSSFRow;
Import Org.apache.poi.hssf.usermodel.HSSFSheet;
Import Org.apache.poi.hssf.usermodel.HSSFWorkbook;
Import Org.apache.poi.hssf.util.HSSFColor; public class Readwritexlsproperties {//Create a HASHMAP which'll store the properties hashmap< string, string
  > propmap = new hashmap< string, String > (); public static void Main (string[] args) {//Create object of Readwritexlsproperties readwritexlsproperties Readwri
    Texlsdemo = new Readwritexlsproperties (); Call Method REadproperties () It take path to properties file Readwritexlsdemo.readproperties ("Config.properties");
  Call Method Writetoexcel () it would take path to Excel file Readwritexlsdemo.writetoexcel ("Test.xls"); 
    } private void ReadProperties (String propertiesfilepath) {//Create a File object taking in Path of properties
    File File Propertiesfile = new file (Propertiesfilepath);  If properties file is a file does below stuff if (Propertiesfile.isfile ()) {try {//Create a
        FileInputStream for loading the properties file FileInputStream Fisprop = new FileInputStream (propertiesfile); Create a Properties object and load//Properties key and value to it through FileInputStream P
        roperties Properties = new properties ();
        Properties.load (Fisprop); Create an object of enumeration and call keys ()//method over properties object created above//it wil L return USWith a enumeration types enumeration< Object > keysenum = Properties.keys (); Looping over the elements of enumeration while (Keysenum.hasmoreelements ()) {//extracting th
          e key and respective values from it.
          String propkey = (string) keysenum.nextelement ();
          String propvalue = (string) properties.getproperty (Propkey);
          After extracting the key and value from the properties file//We'll store the values in a HashMap.
        Propmap.put (Propkey.tolowercase (). Trim (), propvalue.tolowercase (). Trim ()); }//Printing the HASHMAP and closing the file FileInputStream System.out.println ("Properties Map ... \ n
        "+ PropMap);
      Fisprop.close ();
      catch (FileNotFoundException e) {e.printstacktrace ();
      catch (IOException e) {e.printstacktrace (); }} private void Writetoexcel (String excelpATH) {//Create a workbook using Hssfworkbook object Hssfworkbook workbook = new Hssfworkbook (); Create a sheet with name ' Properties ' by//The Createsheet method of the workbook hssfsheet worksheet = Workb
    Ook.createsheet ("Properties");
    Create a row by calling CreateRow to the//worksheet Hssfrow row = Worksheet.createrow ((short) 0); Create a cell style by calling Createcellstyle ()//from the workbook hssfcellstyle CellStyle = Workbook.crea
    Tecellstyle (); setting of the foreground and fill pattern by calling methods//of Hssfcellstyle as Setfillforegroundcolor () and S
    Etfillpattern () Cellstyle.setfillforegroundcolor (HSSFColor.GOLD.index);
    Cellstyle.setfillpattern (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 () to over the Hssfcell Cell1.setcellvalue (New hssfrichtextstring ("Keys"));
    Giving it the style created above.
    Cell1.setcellstyle (CellStyle);
    Hssfcell cell2 = Row.createcell (1);
    Cell2.setcellvalue (New hssfrichtextstring ("Values"));
    Cell2.setcellstyle (CellStyle); Create a iterator and as PropMap is a HashMap//it are converted to a hashset by calling keyset () method//W
    Hich'll return with Set.
    Iterator object is pointed to keys of Set iterator< String > iterator = Propmap.keyset (). iterator (); Looping across the elements of iterator while (Iterator.hasnext ()) {//Creating a new row from the W Orksheet//At the last used row + 1 location hssfrow Rowone = Worksheet.createrow (Worksheet.getlastrownum () +1
      );
      Creating two cells in the row at 0 and 1 position.
      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 extracted keys and values in the cells Cellzero.setcellvalue (new hssfrichtextstring (key));
    Cellone.setcellvalue (new hssfrichtextstring (value));     
      } try{fileoutputstream Fosexcel =null;        
      Creating a xls file file Fileexcel = new file (Excelpath);
      Setting the File to FileOutputStream fosexcel = new FileOutputStream (fileexcel);
      Writing the contents of workbook to the XLS workbook.write (fosexcel);
      Flushing the FileOutputStream Fosexcel.flush ();
    Closing the FileOutputStream fosexcel.close ();
    }catch (Exception e) {e.printstacktrace (); }
  }
}

I hope this article will help you with your Java programming.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.