[英]How to read the excel file from java code which is external to the system?
我正在使用Java(Eclipse)讀取Excel文件的內容並將其存儲在行對象列表中。 要讀取文件,我已在代碼中指定了文件路徑。 在這種情況下,它類似於D:/Refreshed_data_daily/all_hue_posts_in_excel.xlsx。 如果代碼存在於其他位置(即,在其他系統中),該路徑不必相同。 我該如何處理這種情況?
公共類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() ;
}
}
您可以讓用戶從頭開始輸入位置。 或者,將其伸到應有的位置,如果文件不存在,則將錯誤返回給用戶。
將其添加到配置文件。 我為配置文件對象編寫了以下類。 配置應位於運行路徑中名為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));
}
}
此處定義的字符串數組參數
public static void main(String[] args)
包含在執行類時傳遞的命令行參數,或者可以在Eclipse運行配置中傳遞。
將功能更改為
public List<FacebookFields> getTheDataIntoList(String filePath)
並在初始化文件輸入流時使用filePath
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.