如何使用在另一个类中声明的变量?

时间:2015-08-13 20:20:08

标签: java parsing variables apache-poi

我正在自动发送每晚发送的电子邮件。 "发送电子邮件" class包含变量mailFrommailToExcelReader是一个先决条件类,它读取将包含电子邮件地址的Excel工作表的单元格。我需要我的"发送电子邮件" (SendCf)课程,以便从mailFrom课程中获取mailToExcelReader个变量(从Excel工作表中获取变量&#39} ; s细胞)。

目的是让其他人可以修改具有不同To / From电子邮件地址的Excel工作表,我每晚发送的电子邮件将反映出来。因此,每次发送电子邮件时都需要从Excel工作表中提取(不知道如何将该方法作为电子邮件的先决条件运行)。

首先,我的ExcelReader。它声明了变量a1Valb1Val的值(包含所需电子邮件地址的单元格)。

package com.test.Email;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException; 
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class ExcelReader {

    public static void main(String[] args) {
        try {
            FileInputStream fileInputStream = new  FileInputStream("C:/Users/TEST.xlsx");
            XSSFWorkbook workbook = new XSSFWorkbook(fileInputStream);
            XSSFSheet worksheet = workbook.getSheetAt(0);

            XSSFRow row1 = worksheet.getRow(0);
            XSSFCell cellA1 = row1.getCell((int) 0);
            String a1Val = cellA1.getStringCellValue();
            XSSFCell cellB1 = row1.getCell((int) 1);
            String b1Val = cellB1.getStringCellValue();

所以现在我们有从excel设置的变量。 mailTo和mailFrom位于单元格A1和B1中。现在,我需要发送电子邮件类从ExcelReader类中获取A1和B1,并将其值分配给发件人和收件人。我知道下面的内容非常不正确。

package com.test.Email;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

     public class SendCF {

        //Not sure about this, I think I need to declare a local ExcelReader here?
        ExcelReader cfExcel = new ExcelReader();


     public static void main(String[] args) 

            String host = "smtp.gmail.com";
            String port = "587";
//These two variables, I need to get from the excel sheet via the ExcelReader class.
            String mailFrom = cfExcel.a1Val; 
            String mailTo = "cfExcel.b1Val;
            String password = "QAtesting";

            // after this I compose the email and send it.

编辑:我现在在主方法之外声明变量,但它不允许我公开它们。我得到"变量a2Val的非法修饰符;只允许决赛#34;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;


import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class ExcelReader {
                {
    try {
        FileInputStream fileInputStream = new     FileInputStream("C:/Users/test.xlsx");
        XSSFWorkbook workbook = new XSSFWorkbook(fileInputStream);
        XSSFSheet worksheet = workbook.getSheetAt(0);

        XSSFRow row1 = worksheet.getRow(0);
        XSSFCell cellA1 = row1.getCell((int) 0);
        String a1Val = cellA1.getStringCellValue();
        XSSFCell cellB1 = row1.getCell((int) 1);
        String b1Val = cellB1.getStringCellValue();
        XSSFCell cellC1 = row1.getCell((int) 2);
        String c1Val = cellC1.getStringCellValue();
        XSSFCell cellD1 = row1.getCell((int) 3);
        String d1Val = cellD1.getStringCellValue();
        XSSFCell cellE1 = row1.getCell((int) 4);
        String e1Val = cellE1.getStringCellValue();
        XSSFCell cellF1 = row1.getCell((int) 5);
        String f1Val = cellF1.getStringCellValue();

      } catch (FileNotFoundException e) {
        e.printStackTrace();
        } catch (IOException e) {
        e.printStackTrace();
        }
}


public static void main(String[] args) {
    //dont think I need anything here?

    }
}

1 个答案:

答案 0 :(得分:3)

您在main方法中定义了a1Valb1Val,这些变量只能在该范围内访问。如果要在class之外访问它们,则需要将它们作为全局变量。如果您想直接访问它们,则需要使用public修饰符,或者您可以使用private并创建getterssetters第二个选项。

方法1:

public class ExcelReader {
    public String a1Val;
    public String b1Val;

    public static void main(String[] args) {

像这样访问:

    cfExcel.a1Val;

方法2:

public class ExcelReader {
    private String a1Val;
    private String b1Val;

    public String geta1Val()
    {
        return this.a1Val;
    }

    public String getb1Val()
    {
        return this.b1Val;
    }

    public void seta1Val(String a1Val)
    {
        this.a1Val = a1Val;
    }

    public void setb1Val(String b1Val)
    {
        this.b1Val = b1Val;
    }

    public static void main(String[] args) {

像这样访问:

cfExcel.geta1Val();
cfExcel.seta1Val("test");

*************** ********* UPDATE **************************************** ******************

这应该是它的样子:

public class ExcelReader {
    public String a1Val;
    public String b1Val;

    public static void main(String[] args) {
        try {
            FileInputStream fileInputStream = new  FileInputStream("C:/Users/TEST.xlsx");
            XSSFWorkbook workbook = new XSSFWorkbook(fileInputStream);
            XSSFSheet worksheet = workbook.getSheetAt(0);
            XSSFRow row1 = worksheet.getRow(0);
            XSSFCell cellA1 = row1.getCell((int) 0);
            a1Val = cellA1.getStringCellValue();
            XSSFCell cellB1 = row1.getCell((int) 1);
            b1Val = cellB1.getStringCellValue();