Java 如何在二维数组中的变量行中循环代码?

Java 如何在二维数组中的变量行中循环代码?,java,arrays,loops,iteration,apache-poi,Java,Arrays,Loops,Iteration,Apache Poi,我正在从Excel文件中读取二维数组。此数组是在类ExcelRead中读取和创建的 我的另一个类,SendAll需要循环使用每一行作为不同的变量集。它将为每一行发送一封电子邮件,其中包含唯一的“收件人”和“发件人”电子邮件。发送电子邮件的逻辑是可行的,但我为我发送的每一封电子邮件在不同的类中都有代码。这将是禁止当我有许多电子邮件收件人。最好将它们存储在Excel行中,让我的代码循环并为每一行运行代码 基本上,我需要循环我的SendAll代码,为数组中的每一行运行它 下面是我分别运行每个Send类

我正在从Excel文件中读取二维数组。此数组是在类
ExcelRead
中读取和创建的

我的另一个类,
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;
}
公共类ExcelRead{
公共字符串[][]dataReader()引发异常{
文件excel=新文件(“C:/Users/(location)/Dashboards.xlsx”);
FileInputStream fis=新的FileInputStream(excel);
XSSF工作簿wb=新XSSF工作簿(fis);
XSSFSheet-sheet=wb.getSheetAt(0);
int rowNum=sheet.getLastRowNum()+1;
int colNum=sheet.getRow(0.getLastCellNum();
字符串[][]数据=新字符串[rowNum][colNum];
对于(inti=0;i我希望这能有所帮助

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.
}
ExcelRead newExcelRead=new ExcelRead();
//newExcelRead.dataReader();
字符串[][]数据=newExcelRead.dataReader();
String host=“smtp.gmail.com”;
字符串端口=“587”;

对于(int i=0;i看起来您的第一项工作是稍微转换此数据,您需要将原始数据数组合理化为唯一的to/from地址。此处to/from地址构成数据的唯一“键”。因此,我建议您更改dataReader以返回映射>。键是to/fro的串联m地址,第二个元素是实际数据的映射。如果您的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;
}
Map data=newhashmap();

对于(int i=0;i我假设您现在唯一的问题是在遍历数据字符串2D数组时为每一行发送电子邮件。如果我错了,请发表评论

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

对于(int i=0;iThanks a ton)。这是正确的。我刚刚回来发布同样的东西,在意识到我有一个非常简单的解决方案之后。