[英]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.