欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

java使用POI读取properties文件并写到Excel的方法

程序员文章站 2024-03-05 10:44:24
本文实例讲述了java使用poi读取properties文件并写到excel的方法。分享给大家供大家参考。具体实现方法如下: package com.hubber...

本文实例讲述了java使用poi读取properties文件并写到excel的方法。分享给大家供大家参考。具体实现方法如下:

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 will store the properties 
  hashmap< string, string > propmap = new hashmap< string, string >();
  public static void main(string[] args) {
    // create object of readwritexlsproperties
    readwritexlsproperties readwritexlsdemo = new readwritexlsproperties();
    // call method readproperties() it take path to properties file 
    readwritexlsdemo.readproperties("config.properties");
    // call method writetoexcel() it will 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 do 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
        properties properties = new properties();
        properties.load(fisprop);
        // create a object of enumeration 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(keysenum.hasmoreelements())
        {
          // extracting the 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 will 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 = workbook.createsheet("properties");
    // create a row by calling createrow method of the 
    // worksheet
    hssfrow row = worksheet.createrow((short) 0);
    // create a cell style by calling createcellstyle()
    // from the workbook
    hssfcellstyle cellstyle = workbook.createcellstyle();
    // setting of the foreground and fill pattern by calling methods
    // of hssfcellstyle as setfillforegroundcolor() and setfillpattern()
    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() method 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 is converted to a hashset by calling keyset() method 
    // which will 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 worksheet
      // 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();
    }
  }
}

希望本文所述对大家的java程序设计有所帮助。