写入excel时无法使用动态字符串值作为列名

时间:2017-08-07 16:29:47

标签: java excel

我正在尝试将值“articlevalue”写入Excel工作表。 我有专栏“星期一”,“星期二”......“星期天”,因为我每天都在运行。 我找出了用于写入值的列,使用名为“dayofweek”的字符串变量,但是:

datatable.setCellData( “记录”,星期几,I + 1,articlevalue);

它不喜欢dayofweek变量?

如果我使用: datatable.setCellData( “记录”, “星期一”,+ 1,articlevalue);

它可以正常工作,但我希望控制列值。

参见代码:

  String articlevalue = "1 " + outArray[0] + "\r\n" + "2 " + outArray[1] + "\r\n" + "3 " + outArray[2] + "\r\n" + "4 " + outArray[3] + "\r\n" + "5 " + outArray[4];
//  System.out.println(article value);

    Calendar calendar = Calendar.getInstance();
    int day = calendar.get(Calendar.DAY_OF_WEEK); 

    String todayDate = String.valueOf(day);

        if (todayDate .equals("1")) {
            String dayofweek = "Sunday";
            System.out.println(dayofweek);
        }
        else if (todayDate .equals("2")) {
            String dayofweek = "Monday";
            System.out.println(dayofweek);
        }
        else if (todayDate .equals("3")) {
            String dayofweek = "Tuesday";
            System.out.println(dayofweek);
        }
        else if (todayDate .equals("4")) {
            String dayofweek = "Wednesday";
            System.out.println(dayofweek);
        }
        else if (todayDate .equals("5")) {
            String dayofweek = "Thursday";
            System.out.println(dayofweek);
        }
        else if (todayDate .equals("6")) {
            String dayofweek = "Friday";
            System.out.println(dayofweek);
        }
        else if (todayDate .equals("7")) {
            String dayofweek = "Saturday";
            System.out.println(dayofweek);
        }
// I want to use the following to , but it doesn't like dayofweek
  datatable.setCellData("Records",dayofweek,i+1,articlevalue);

1 个答案:

答案 0 :(得分:0)

问题是dayofweek没有在正确的范围内定义,因此当你进行setCellData调用时,该变量不存在。

尝试这样的事情:

    String dayofweek = null;
    if (todayDate .equals("1")) {
        dayofweek = "Sunday";
        System.out.println(dayofweek);
    }
    else if (todayDate .equals("2")) {
        dayofweek = "Monday";
        System.out.println(dayofweek);
    }
    else if (todayDate .equals("3")) {
        dayofweek = "Tuesday";
        System.out.println(dayofweek);
    }
    else if (todayDate .equals("4")) {
        dayofweek = "Wednesday";
        System.out.println(dayofweek);
    }
    else if (todayDate .equals("5")) {
        dayofweek = "Thursday";
        System.out.println(dayofweek);
    }
    else if (todayDate .equals("6")) {
        dayofweek = "Friday";
        System.out.println(dayofweek);
    }
    else if (todayDate .equals("7")) {
        dayofweek = "Saturday";
        System.out.println(dayofweek);
    }

通过使用开关/外壳或可能是Map,这也可以更加优雅。