2015-08-28 58 views
0

我正在從Excel文件讀取二維數組。這個數組在ExcelRead中讀取和創建。如何通過2D數組中的變量行來循環我的代碼?

我的OTHER類SendAll需要循環使用每一行作爲一組不同的變量。它會爲每行發送一封電子郵件,其中包含一個獨特的「收件人」和「發件人」電子郵件。發送電子郵件的邏輯正在工作,但我爲每封發送的電子郵件分別提供了代碼。當我有很多電子郵件收件人時,這將是禁止的。最好將它們存儲在Excel行中,然後讓我的代碼遍歷併爲每一行運行代碼。

基本上,我需要遍歷我的SendAll代碼,爲陣列中的每一行運行它。

這裏是我分別運行每個Send類的代碼。即只需要提取該電子郵件所需的特定數組值。

public class SendAll extends ExcelRead { 

public static void main(String[] args) throws Exception { 

    ExcelRead newExcelRead = new ExcelRead(); 
    //newExcelRead.dataReader(); 
    String[][] data = newExcelRead.dataReader(); 


    String host = "smtp.gmail.com"; 
    String port = "587"; 
    String mailTo = data[1][3]; 
    String mailFrom = data[1][4]; 
    String password = data[1][5]; 

(Below this is just my Send Email logic) 

那是重要的一部分,我不知道它會幫助,但下面是我的數組類ExcelRead ...

public class ExcelRead { 

public String[][] dataReader() throws Exception { 
File excel = new File ("C:/Users/(location)/Dashboards.xlsx"); 
FileInputStream fis = new FileInputStream(excel); 

XSSFWorkbook wb = new XSSFWorkbook(fis); 
XSSFSheet sheet = wb.getSheetAt(0); 

int rowNum = sheet.getLastRowNum()+1; 
int colNum = sheet.getRow(0).getLastCellNum(); 
String[][] data = new String[rowNum][colNum]; 
for (int i=0; i<rowNum; i++){ 
    //get the row 
    XSSFRow row = sheet.getRow(i); 
     for (int j=0; j<colNum; j++){ 
      //this gets the cell and sets it as blank if it's empty. 
      XSSFCell cell = row.getCell(j, Row.CREATE_NULL_AS_BLANK); 
      String value = String.valueOf(cell);        
      //System.out.println("Value: " + value); 
      data[i][j] = value; 
     }    
    } 
//System.out.println("End Value: " + data[2][0]); 
return data; 
} 

}

回答

0

我希望這可以幫助。

ExcelRead newExcelRead = new ExcelRead(); 
//newExcelRead.dataReader(); 
String[][] data = newExcelRead.dataReader(); 

String host = "smtp.gmail.com"; 
String port = "587"; 
for(int i=0;i<data.length;i++){ 
    String mailTo = data[i][3]; 
    String mailFrom = data[i][4]; 
    String password = data[i][5]; 
    // Send email logic. 
} 
+0

非常感謝。這是對的。在意識到我有一個非常簡單的解決方案後,我只是回來發佈相同的東西。 – dsidler

+0

不客氣...... :) –

1

好看起來像你的第一份工作是將這一數據一點,你需要合理安排你的數據的原始陣列逼到獨特/從地址。這裏的收件人/發件人地址組成了您數據的唯一'鑰匙'。出於這個原因,我建議你改變你的dataReader返回一個Map>。關鍵是連接到/從地址,第二個元素是實際數據的映射。如果你的dataReader產生了這個數據,你將更容易解析這些數據。我沒有測試過,但是像這樣,這個假設,就像在你的例子中,你知道哪些數據在哪些列中。

Map<String, Map<String, String>> data = new HashMap<String, Map<String, String>>(); 
for (int i=0; i<rowNum; i++){ 
    //get the row 
    XSSFRow row = sheet.getRow(i); 
    Map<String, String> rowMap = new HashMap<String, String>(); 

    rowMap.put("to", String.valueOf(row.getCell(3, Row.CREATE_NULL_AS_BLANK))); 
    rowMap.put("from", String.valueOf(row.getCell(4, Row.CREATE_NULL_AS_BLANK))); 
    rowMap.put("password", String.valueOf(row.getCell(5, Row.CREATE_NULL_AS_BLANK))); 

    data.put(rowMap.get("to") + rowMap.get("from"), rowMap); 

    } 
return data; 
} 

現在有很多聰明的事情可以爲您的地圖創建Key值,但這只是一個開始。

1

我假設您現在唯一的問題是在遍歷數據時爲每一行發送一封電子郵件字符串2D數組。請評論,如果我錯了。

int row = data.length; 
int col = data[0].length; 
for(int i=0;i<row;i++) 
{ 
    //write the email code here? 
}