gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 17:50:24 26 4
gpt4 key购买 nike

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

最佳答案

只需将字符串参数添加到文件名的方法

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]);
}

关于java - 如何让我的java程序接受命令行中给出的excel文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50873447/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com