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

 更新时间:2015年06月16日 12:26:21   作者:红薯  
这篇文章主要介绍了java使用POI读取properties文件并写到Excel的方法,涉及java操作properties文件及Excel文件的相关技巧,需要的朋友可以参考下

本文实例讲述了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程序设计有所帮助。

相关文章

  • Java SSL与TLS客户端证书配置方式

    Java SSL与TLS客户端证书配置方式

    这篇文章主要介绍了Java SSL与TLS客户端证书配置方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2024-01-01
  • springBoot项目打包idea的多种方法

    springBoot项目打包idea的多种方法

    这篇文章主要介绍了springBoot项目打包idea的多种方法,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    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列表、数组、map集合和对象方法实例

    springboot读取yml文件中的list列表、数组、map集合和对象方法实例

    在平时的yml配置文件中,我们经常使用到配置基本数据类型的字符串,下面这篇文章主要给大家介绍了关于springboot读取yml文件中的list列表、数组、map集合和对象的相关资料,需要的朋友可以参考下
    2023-02-02
  • 解决JavaWeb-file.isDirectory()遇到的坑问题

    解决JavaWeb-file.isDirectory()遇到的坑问题

    JavaWeb开发中,使用`file.isDirectory()`判断路径是否为文件夹时,需要特别注意:该方法只能判断已存在的文件夹,若路径不存在,无论其实际是否应为文件夹,均会返回`false`,为了解决这个问题,可以采用正则表达式进行判断,但要求路径字符串的结尾必须添加反斜杠(\)
    2025-02-02
  • Java合并两个及以上有序链表的示例详解

    Java合并两个及以上有序链表的示例详解

    这篇文章主要通过两个例题为大家介绍一下Java合并两个及以上有序链表的实现方法,文中的示例代码讲解详细,具有一定的学习价值,需要的可以参考一下
    2022-11-11
  • jdbc和mybatis的流式查询使用方法

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

    有些时候我们所需要查询的数据量比较大,但是jvm内存又是有限制的,数据量过大会导致内存溢出。这个时候就可以使用流式查询,本文就主要介绍了jdbc和mybatis的流式查询,感兴趣的可以了解一下
    2021-11-11
  • springboot跨域CORS处理代码解析

    springboot跨域CORS处理代码解析

    这篇文章主要介绍了springboot跨域CORS处理代码解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2019-12-12

最新评论