如何让我的java程序接受命令行中给出的excel文件?

时间:2018-06-15 10:10:51

标签: java

当我将其作为可执行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;
&#13;
&#13;

2 个答案:

答案 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)

你可以试试这个。它正在运作
https://www.callicoder.com/java-read-excel-file-apache-poi/