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

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

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

本文實例講述了java使用POI讀取properties文件并寫到Excel的方法。分享給大家供大家參考。具體實現(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();
    }
  }
}

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

相關(guān)文章

  • Java SSL與TLS客戶端證書配置方式

    Java SSL與TLS客戶端證書配置方式

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

    springBoot項目打包idea的多種方法

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

    Mybatis整合Spring 由于版本引起的BUG問題

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

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

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

    Springboot連接和操作mongoDB方式

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

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

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

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

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

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

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

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

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

    springboot跨域CORS處理代碼解析

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

最新評論

加查县| 东方市| 巴青县| 随州市| 甘南县| 五峰| 萨嘎县| 隆安县| 汤原县| 习水县| 武邑县| 淄博市| 灵武市| 北海市| 尖扎县| 石渠县| 太康县| 南华县| 罗定市| 丰都县| 威信县| 隆昌县| 阿合奇县| 伊金霍洛旗| 同心县| 三穗县| 宁津县| 嫩江县| 万年县| 万山特区| 海林市| 瑞昌市| 蕉岭县| 嘉禾县| 都兰县| 博乐市| 涟水县| 阿鲁科尔沁旗| 海南省| 吉安市| 高密市|