2017-08-07 29 views
0

我正在嘗試將值articlevalue寫入Excel表格。 我有專欄「星期一」,「星期二」......「星期天」在Excel中,因爲我每天運行它。 我計算出哪一列寫入的值也是一個名爲「dayofweek」的字符串變量,但是:datatable.setCellData(「Records」,dayofweek,i + 1,articlevalue);寫入excel時無法使用動態字符串值作爲列名稱

它不喜歡dayofweek變量?

如果我使用: datatable.setCellData(「Records」,「Monday」,i + 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); 
+0

什麼是'datatable'的類型? –

+0

Xls_Reader datatable = new Xls_Reader(「C:\\ Temp \\ results.xlsx」); 整數。 –

回答

0

問題是星期幾是不是在正確的範圍界定,所以當你做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); 
    } 

這也可以作出更優雅。

相關問題