一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|

服務(wù)器之家 - 編程語言 - JAVA教程 - java使用POI讀取properties文件并寫到Excel的方法

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

2019-12-23 15:22紅薯 JAVA教程

這篇文章主要介紹了java使用POI讀取properties文件并寫到Excel的方法,涉及java操作properties文件及Excel文件的相關(guān)技巧,需要的朋友可以參考下

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
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è)計有所幫助。

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 亚洲H成年动漫在线观看不卡 | 国产在线视频一区二区三区 | 日本不卡一区二区三区在线观看 | 国产麻豆精品原创 | 男人的天堂欧美 | 欧美成人乱弄视频 | 亚洲七七久久综合桃花 | 欧美国产日产精品免费视频 | 亚洲欧美日韩综合在线播放 | 高清一级片 | 男人天堂色男人 | 午夜尤物 | 日本人成年视频在线观看 | 国产精品成人免费 | 娇小老少配xxxxx性视频 | 亚洲 综合 自拍 精品 在线 | 亚洲丁香网 | 精品国产欧美一区二区三区成人 | 美女毛片老太婆bbb80岁 | 国产一区私人高清影院 | 欧美一区二区三区在线观看免费 | 免费特黄一级欧美大片 | 日本色频 | 乌克兰13一14娇小 | 欧美高清无砖专区欧美精品 | 99精品久久精品一区二区 | 日韩日韩日韩手机看片自拍 | 青草热视频 | 四虎www| 日韩在线视频免费观看 | 欧美国产影院 | 为什么丈夫插我我却喜欢被打着插 | 日韩欧美一级大片 | juy799大岛优香在线观看 | 亚洲欧美韩国日产综合在线 | 猫咪社区在线播放 | 99爱爱| 国产欧美成人不卡视频 | 欧美一级裸片又黄又裸 | 久久青草费线频观看国产 | 国产午夜视频在线观看网站 |