最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

java使用POI讀取properties文件并寫(xiě)到Excel的方法

 更新時(shí)間:2015年06月16日 12:26:21   作者:紅薯  
這篇文章主要介紹了java使用POI讀取properties文件并寫(xiě)到Excel的方法,涉及java操作properties文件及Excel文件的相關(guān)技巧,需要的朋友可以參考下

本文實(shí)例講述了java使用POI讀取properties文件并寫(xiě)到Excel的方法。分享給大家供大家參考。具體實(shí)現(xiàn)方法如下:

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();
    }
  }
}

希望本文所述對(duì)大家的java程序設(shè)計(jì)有所幫助。

相關(guān)文章

  • Java SSL與TLS客戶端證書(shū)配置方式

    Java SSL與TLS客戶端證書(shū)配置方式

    這篇文章主要介紹了Java SSL與TLS客戶端證書(shū)配置方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-01-01
  • springBoot項(xiàng)目打包idea的多種方法

    springBoot項(xiàng)目打包idea的多種方法

    這篇文章主要介紹了springBoot項(xiàng)目打包idea的多種方法,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-07-07
  • Mybatis整合Spring 由于版本引起的BUG問(wèn)題

    Mybatis整合Spring 由于版本引起的BUG問(wèn)題

    這篇文章主要介紹了Mybatis整合Spring 由于版本引起的BUG問(wèn)題,需要的朋友可以參考下
    2017-06-06
  • 多層嵌套的json的值如何解析/替換

    多層嵌套的json的值如何解析/替換

    這篇文章主要介紹了多層嵌套的json的值如何解析/替換的方法示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-10-10
  • Springboot連接和操作mongoDB方式

    Springboot連接和操作mongoDB方式

    這篇文章主要介紹了Springboot連接和操作mongoDB方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-01-01
  • springboot讀取yml文件中的list列表、數(shù)組、map集合和對(duì)象方法實(shí)例

    springboot讀取yml文件中的list列表、數(shù)組、map集合和對(duì)象方法實(shí)例

    在平時(shí)的yml配置文件中,我們經(jīng)常使用到配置基本數(shù)據(jù)類型的字符串,下面這篇文章主要給大家介紹了關(guān)于springboot讀取yml文件中的list列表、數(shù)組、map集合和對(duì)象的相關(guān)資料,需要的朋友可以參考下
    2023-02-02
  • 解決JavaWeb-file.isDirectory()遇到的坑問(wèn)題

    解決JavaWeb-file.isDirectory()遇到的坑問(wèn)題

    JavaWeb開(kāi)發(fā)中,使用`file.isDirectory()`判斷路徑是否為文件夾時(shí),需要特別注意:該方法只能判斷已存在的文件夾,若路徑不存在,無(wú)論其實(shí)際是否應(yīng)為文件夾,均會(huì)返回`false`,為了解決這個(gè)問(wèn)題,可以采用正則表達(dá)式進(jìn)行判斷,但要求路徑字符串的結(jié)尾必須添加反斜杠(\)
    2025-02-02
  • Java合并兩個(gè)及以上有序鏈表的示例詳解

    Java合并兩個(gè)及以上有序鏈表的示例詳解

    這篇文章主要通過(guò)兩個(gè)例題為大家介紹一下Java合并兩個(gè)及以上有序鏈表的實(shí)現(xiàn)方法,文中的示例代碼講解詳細(xì),具有一定的學(xué)習(xí)價(jià)值,需要的可以參考一下
    2022-11-11
  • jdbc和mybatis的流式查詢使用方法

    jdbc和mybatis的流式查詢使用方法

    有些時(shí)候我們所需要查詢的數(shù)據(jù)量比較大,但是jvm內(nèi)存又是有限制的,數(shù)據(jù)量過(guò)大會(huì)導(dǎo)致內(nèi)存溢出。這個(gè)時(shí)候就可以使用流式查詢,本文就主要介紹了jdbc和mybatis的流式查詢,感興趣的可以了解一下
    2021-11-11
  • springboot跨域CORS處理代碼解析

    springboot跨域CORS處理代碼解析

    這篇文章主要介紹了springboot跨域CORS處理代碼解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-12-12

最新評(píng)論

颍上县| 湟中县| 晋州市| 辉南县| 界首市| 甘孜县| 遂溪县| 云浮市| 蛟河市| 应城市| 普定县| 井研县| 乐平市| 潞西市| 塔城市| 启东市| 广水市| 唐海县| 五台县| 镇原县| 吴桥县| 西乡县| 沅江市| 南开区| 奎屯市| 甘德县| 宜宾市| 启东市| 宜兰市| 屯昌县| 济宁市| 静乐县| 玉溪市| 澄城县| 阳春市| 鹤山市| 黄浦区| 玛纳斯县| 满洲里市| 武功县| 二连浩特市|