我正在使用程序从excel文件读取值,然后返回读取值。我曾尝试使用迭代器以及for循环,但程序不会返回工作表中的所有值。请建议。使用Apache POI API从Excel文件读取值
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.Iterator;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellValue;
import java.io.File;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class ExcelReading {
public static STring ReadExcel(String Path){
String FakeReturn = null;
try
{
FileInputStream file = new FileInputStream(new File(Path));
XSSFWorkbook workbook = new XSSFWorkbook(file);
XSSFSheet sheet = workbook.getSheetAt(0);
for(Row row:sheet)
{
Cell cell=row.getCell(0);
Cell testcell = row.getCell(1);
if(cell.getStringCellValue()!=null)
return testcell.getStringCellValue();
else
break;
}
file.close();
}
catch (Exception e)
{
System.out.println("The code carries an exception");
e.printStackTrace();
return FakeReturn;
}
return FakeReturn;
}
}
你真的认为这个代码将工作。 –