java使用POI讀取properties文件并寫到Excel的方法
本文實(shí)例講述了java使用POI讀取properties文件并寫到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ì)有所幫助。
- Java POI讀取excel中數(shù)值精度損失問(wèn)題解決
- Java如何利用POI讀取Excel行數(shù)
- JAVA使用POI(XSSFWORKBOOK)讀取EXCEL文件過(guò)程解析
- Java利用POI讀取、寫入Excel的方法指南
- Java使用poi包讀取Excel文檔代碼分享
- POI讀取excel簡(jiǎn)介_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
- java的poi技術(shù)讀取和導(dǎo)入Excel實(shí)例
- Java使用Apache POI庫(kù)讀取Excel表格文檔的示例
- java使用poi讀取ppt文件和poi讀取excel、word示例
- java使用poi讀取excel內(nèi)容方法實(shí)例
- java poi讀取excel操作示例(2個(gè)代碼)
- java利用POI讀取excel文件的方法
相關(guān)文章
Mybatis-plus?代碼生成器?AutoGenerator?的簡(jiǎn)介和使用詳解
AutoGenerator是MyBatis-Plus的代碼生成器,通過(guò)AutoGenerator可以快速生成?Entity、Mapper、Mapper XML、Service、Controller等各個(gè)模塊的代碼,極大的提升了開發(fā)效率,這篇文章主要介紹了Mybatis-plus代碼生成器AutoGenerator的簡(jiǎn)介和使用,需要的朋友可以參考下2023-05-05idea企業(yè)開發(fā)之新建各類型項(xiàng)目的詳細(xì)教程
這篇文章主要介紹了idea企業(yè)開發(fā)之新建各類型項(xiàng)目的詳細(xì)教程,本文通過(guò)圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-12-12解決IDEA2020 創(chuàng)建maven項(xiàng)目沒(méi)有src/main/java目錄和webapp目錄問(wèn)題
這篇文章主要介紹了IDEA2020 創(chuàng)建maven項(xiàng)目沒(méi)有src/main/java目錄和webapp目錄問(wèn)題解決方法,本文通過(guò)圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-10-10封裝了一個(gè)Java數(shù)據(jù)庫(kù)訪問(wèn)管理類
剛剛試著用JDBC,仿著原來(lái)C#的寫法寫了這段代碼,自己覺(jué)得還是挺粗糙的,還煩請(qǐng)路過(guò)的朋友推薦一個(gè)寫得較好較完整的相關(guān)例程以便學(xué)習(xí)。謝謝!2009-02-02SpringBoot中整合Minio文件存儲(chǔ)的安裝部署過(guò)程
這篇文章主要介紹了SpringBoot整合Minio文件存儲(chǔ)的相關(guān)知識(shí),詳細(xì)介紹了Minio安裝部署過(guò)程,需要的朋友可以參考下2022-04-04Ubuntu安裝JDK與IntelliJ?IDEA的詳細(xì)過(guò)程
APT是Linux系統(tǒng)上的包管理工具,能自動(dòng)解決軟件包依賴關(guān)系并從遠(yuǎn)程存儲(chǔ)庫(kù)中獲取安裝軟件包,這篇文章主要介紹了Ubuntu安裝JDK與IntelliJ?IDEA的過(guò)程,需要的朋友可以參考下2023-08-08Nacos服務(wù)注冊(cè)客戶端服務(wù)端原理分析
這篇文章主要為大家介紹了Nacos服務(wù)注冊(cè)客戶端服務(wù)端原理分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-02-02