当我将其作为可执行jar文件运行时,我想让我的程序接受命令行中给出的文件名。在我的下面的代码中,我将文件名硬编码为report.xlsx。例如,我希望它使用命令java -jar Vnp.java fileName.xlsx
public ArrayList < String > getReports() throws IOException {
ArrayList < String > reports = new ArrayList < String > ();
FileInputStream fis = new FileInputStream(new File("reports.xlsx"));
workbook = new XSSFWorkbook(fis);
sheet = workbook.getSheet("sheet1");
int rowCount = sheet.getFirstRowNum() + sheet.getLastRowNum() + 1;
reportCount = rowCount;
int colCount = sheet.getRow(0).getLastCellNum();
if (colCount > 1) {
System.out.println("The number of columns are more than 1. Please input only one column with the report keys");
} else if (colCount == 1) {
System.out.println("Number of reports given: " + rowCount);
for (int rNum = 1; rNum <= rowCount; rNum++) {
for (int cNum = 0; cNum < colCount; cNum++) {
reports.add(getCellData("sheet1", cNum, rNum));
}
}
}
return reports;
}
&#13;
答案 0 :(得分:3)
只需将字符串param添加到文件名
的方法中public ArrayList < String > getReports(String filename) throws IOException {
ArrayList < String > reports = new ArrayList < String > ();
FileInputStream fis = new FileInputStream(new File(filename));
...........
}
如果从main调用然后传递第一个索引:
public static void main(String a[]){
if(a.length == 0)//check if command line args passed
System.exit(0);//if not then exit
ArrayList<String> list = obj.getReports(a[0]);
}
答案 1 :(得分:1)