2015-10-06 23 views
2

我正在使用java(eclipse)讀取excel文件的內容並將其存儲在行對象列表中。要讀取文件,我已經在代碼中指定了文件路徑。在這種情況下,它類似於D:/Refreshed_data_daily/all_hue_posts_in_excel.xlsx。 如果代碼存在於其他某個位置,即某些其他系統中,該路徑不必相同。我該如何處理這種情況?如何從系統外部的java代碼中讀取excel文件?

公共類FacebookDataList {

private static final String FILE_NAME="D:/Refreshed_data_daily/all_hue_posts_in_excel.xlsx"; 
private static final String SHEET_NAME="nextv54plus_actions"; 
XSSFWorkbook workbook; 

public static void main(String[] args){ 

    FacebookDataList obj= new FacebookDataList(); 
    List<FacebookFields> displayList= new ArrayList<FacebookFields>(); 
    displayList=obj.getTheDataIntoList(); 
    System.out.println("The Size of the list is:"+ displayList.size()); 
} 

public List<FacebookFields> getTheDataIntoList() { 
    List<FacebookFields> fbList= new ArrayList<FacebookFields>(); 
    try 
    { 
     FileInputStream fin= new FileInputStream(FILE_NAME); 
     workbook= new XSSFWorkbook(fin); 
     int sheetIndex=0; 
     for (Sheet sheet : workbook) { 
      readSheet(sheet,sheetIndex ++, fbList);} 

    }catch(FileNotFoundException e){ 
     e.printStackTrace(); 
    } 
    catch(IOException e){ 
     e.printStackTrace(); 
    } 
    return fbList; 
} 

private void readSheet(Sheet sheet, int sheetIndex , List<FacebookFields> fbList) { 

    if(SHEET_NAME.equals(sheet.getSheetName())){ 
     workbook.removeSheetAt(sheetIndex); 
     return; 
    } 
    for (Row row : sheet){ 
     if (row.getRowNum() > 0) 
      fbList.add(readRow(row));} 

} 

private FacebookFields readRow(Row row) { 

    FacebookFields record= new FacebookFields(); 
    for (Cell cell : row) { 
     switch (cell.getColumnIndex()) { 
     case 0: record.setName(cell.getStringCellValue()); 
     break; 
     case 1: record.setId(cell.getStringCellValue()); 
     break; 
     case 2: record.setDate(cell.getStringCellValue()); 
     break; 
     case 3: record.setMessage(cell.getStringCellValue()); 
     break; 
     case 4: record.setType(cell.getStringCellValue()); 
     break; 
     case 5: record.setPage(cell.getStringCellValue()); 
     break; 
     case 6: record.setLikeCount(String.valueOf(cell.getNumericCellValue())); 
     break; 
     case 7: record.setCommentCount(String.valueOf(cell.getNumericCellValue())); 
     break; 
     case 8: record.setShareCount(String.valueOf(cell.getNumericCellValue())); 
     break; 
     } 
    } 

    return record; 
} 

public boolean checkIfListContainsData() { 

    List<FacebookFields> checkList= getTheDataIntoList(); 
    return !checkList.isEmpty() ; 
} 

}

+0

哪裏是代碼時

改變功能到

public List<FacebookFields> getTheDataIntoList(String filePath) 

並使用文件路徑? – pelumi

+0

正確提及您的查詢。什麼樣的應用程序是獨立的還是Web應用程序?該文件位置是否會動態變化? –

回答

0

你可以讓用戶輸入從一開始的位置。或者,如果該文件不存在,請將其發送到應該在的位置並向用戶返回錯誤。

+0

即使我有相同的選擇從用戶採取輸入。但我不知道我該怎麼做。你能幫助我嗎? – ShridharBavannawar

0

將其添加到配置文件中。我爲配置文件對象編寫了以下類。配置應位於運行路徑中名爲config.properties的文件中。

https://github.com/achinthagunasekara/JavaConfigFileReader

像這樣的事情非常適用於存儲應用程序的配置。

配置文件

ConfigItem1=ItemValue1 
. 
. 
. 
ConfigItemN=ItemValueN 

的Java類

package Config; 

import java.io.FileInputStream; 
import java.io.IOException; 
import java.util.Properties; 

/** 
* 
* @author Achintha Gunasekara 
* @date 07.09.2015 
*/ 

public class ConfigFileReader { 

    //Configuration file used by the config reader 
    //config file must be located inside the application path 
    private final String file = "config.properties"; 
    private Properties properties; 
    private static ConfigFileReader instance; 

    public static ConfigFileReader getConfigFileReaderInstance() throws IOException { 

     //there can only be once instance of the config reader 
     if(instance == null) { 

      instance = new ConfigFileReader(); 
     } 

     return instance; 
    } 

    private ConfigFileReader() throws IOException { 

     loadConfig(file); 
    } 

    //read and load the config file 
    private void loadConfig(String configFile) throws IOException { 

     properties = new Properties(); 
     properties.load(new FileInputStream(configFile)); 
    } 

    //reloads the configuration during runtime 
    public void reloadConfig() throws IOException { 

     loadConfig(file); 
    } 

    //reloads the configuration during runtime from a provided configuration file 
    public void reloadConfig(String configFile) throws IOException { 

     loadConfig(configFile); 
    } 

    //get the propery value for strng s 
    public String getPropertyFor(String configItem) throws Exception { 

     String value = properties.getProperty(configItem); 

     if(value == null) { 

      throw new Exception(configItem + " not found!"); 
     } 

     return value; 
    } 
    //returns int property 
    public Integer getIntPropertyFor(String configItem) throws Exception { 

     return Integer.parseInt(getPropertyFor(configItem)); 
    } 

    //returns double property 
    public Double getDoublePropertyFor(String configItem) throws Exception { 

     return Double.parseDouble(getPropertyFor(configItem)); 
    } 

    //returns bool property 
    public Boolean getBooleanPropertyFor(String s) throws Exception { 

     return Boolean.parseBoolean(getPropertyFor(s)); 
    } 
} 
+0

將路徑名添加到config.properties有什麼好處?我不明白!你可以解釋嗎? – ShridharBavannawar

0

字符串數組ARGS這裏定義

包含執行所述的類或可以在蝕運行被傳遞時通過命令行參數組態。初始化文件輸入流