如何通过二维数组中的变量行循环我的代码?
How do I loop my code through rows of variables in a 2D array?
我正在从 Excel 文件中读取二维数组。该数组在 class ExcelRead
.
中读取和创建
我的其他 class、SendAll
需要使用每一行作为一组不同的变量进行循环。它将为每一行发送一封电子邮件,其中包含唯一的 "To" 和 "From" 电子邮件。发送电子邮件的逻辑正常,但我发送的每封电子邮件都有单独的 classes 代码。当我有很多电子邮件收件人时,这将是令人望而却步的。最好将它们存储在 Excel 行中,并让我的代码循环并 运行 每行的代码。
基本上,我需要遍历我的 SendAll
代码,运行为数组中的每一行设置它。
这是我 运行 分别 Send
class 的代码。 IE。仅提取此电子邮件需要的专门定义的数组值。
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)
这是重要的部分,我不确定它是否有帮助,但下面是我的数组 class 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 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.
}
看来您的第一项工作是稍微转换此数据,您需要将原始数据数组合理化为唯一的 to/from 地址。这里的 To/From 地址构成了您数据的唯一 'key'。出于这个原因,我建议您将 dataReader 更改为 return a Map>。关键是 to/from 地址的串联,第二个元素是实际数据的映射。如果您的 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;
}
现在,您可以采取很多巧妙的措施来为您的地图创建键值,但这只是一个开始。
我假设您现在唯一的问题是在遍历 data 字符串二维数组时为每一行发送一封电子邮件。如果我说错了请评论。
int row = data.length;
int col = data[0].length;
for(int i=0;i<row;i++)
{
//write the email code here?
}
我正在从 Excel 文件中读取二维数组。该数组在 class ExcelRead
.
我的其他 class、SendAll
需要使用每一行作为一组不同的变量进行循环。它将为每一行发送一封电子邮件,其中包含唯一的 "To" 和 "From" 电子邮件。发送电子邮件的逻辑正常,但我发送的每封电子邮件都有单独的 classes 代码。当我有很多电子邮件收件人时,这将是令人望而却步的。最好将它们存储在 Excel 行中,并让我的代码循环并 运行 每行的代码。
基本上,我需要遍历我的 SendAll
代码,运行为数组中的每一行设置它。
这是我 运行 分别 Send
class 的代码。 IE。仅提取此电子邮件需要的专门定义的数组值。
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)
这是重要的部分,我不确定它是否有帮助,但下面是我的数组 class 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 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.
}
看来您的第一项工作是稍微转换此数据,您需要将原始数据数组合理化为唯一的 to/from 地址。这里的 To/From 地址构成了您数据的唯一 'key'。出于这个原因,我建议您将 dataReader 更改为 return a Map>。关键是 to/from 地址的串联,第二个元素是实际数据的映射。如果您的 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;
}
现在,您可以采取很多巧妙的措施来为您的地图创建键值,但这只是一个开始。
我假设您现在唯一的问题是在遍历 data 字符串二维数组时为每一行发送一封电子邮件。如果我说错了请评论。
int row = data.length;
int col = data[0].length;
for(int i=0;i<row;i++)
{
//write the email code here?
}