我正在從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;
}
}
非常感謝。這是對的。在意識到我有一個非常簡單的解決方案後,我只是回來發佈相同的東西。 – dsidler
不客氣...... :) –