我正在学习selenium webdriver并尝试使用下面的代码段使用Apache POI lib来阅读excel。它已成功运行但在更新excel文件后。它不会读取更新的值或Excel数据。始终打印以前的内容。
public org.apache.poi.ss.usermodel.Sheet sheet;
public static FileInputStream ReadExcel;
public void read_excel(String excel_file_path, String DataSheetName) throws Exception, IOException
{
try
{
ReadExcel=new FileInputStream(new File(excel_file_path));
Workbook wp=WorkbookFactory.create(ReadExcel);
sheet=wp.getSheet(DataSheetName);
Iterator<Row> rowIterator=sheet.rowIterator();
while(rowIterator.hasNext())
{
Row row=rowIterator.next();
Iterator<Cell> cellIterator=row.cellIterator();
while(cellIterator.hasNext())
{
Cell cell=cellIterator.next();
String cellData=cell.getStringCellValue();
System.out.println(cellData);
}
}
ReadExcel.close();
}
catch(Exception e)
{
e.printStackTrace();
}
答案 0 :(得分:0)
excel表格中有哪些更新?尝试使用以前版本的文件格式与excel 97-2003工作簿另一个解决方案是excel文件的路径尚未被修改,因此它不使用较新的文件。同时检查文件名,因为您在此处将其用作字符串。
下面是Excel文件读取示例TC:
package excelExportAndFileIO;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class aafileread {
public void readExcel(String filePath,String fileName,String sheetName) throws IOException{
//Create a object of File class to open xlsx file
File file = new File(filePath+"\\"+fileName);
//Create an object of FileInputStream class to read excel file
FileInputStream inputStream = new FileInputStream(file);
Workbook FILENAME = null;
String fileExtensionName = fileName.substring(fileName.indexOf("."));
//Check condition if the file is xlsx file
if(fileExtensionName.equals(".xlsx")){
//If it is xlsx file then create object of XSSFWorkbook class
filename = new XSSFWorkbook(inputStream);
}
//Check condition if the file is xls file
else if(fileExtensionName.equals(".xls")){
//If it is xls file then create object of XSSFWorkbook class
Filename = new HSSFWorkbook(inputStream);
}
//Read sheet inside the workbook by its name
Sheet Sheetname = Filename.getSheet(sheetName);
//Find number of rows in excel file
int rowCount = sheetname.getLastRowNum()-sheetname.getFirstRowNum();
//Create a loop over all the rows of excel file to read it
for (int i = 0; i < rowCount+1; i++) {
Row row = sheetname.getRow(i);
//Create a loop to print cell values in a row
for (int j = 0; j < row.getLastCellNum(); j++) {
//Print excel data in console
System.out.print(row.getCell(j).getStringCellValue()+"|| ");
}
System.out.println();
}
}
//Main function is calling readExcel function to read data from excel file
public static void main(String...strings) throws IOException{
//Create a class
FilenameFile objExcelFile = new FilenameFile();
//Prepare the path of excel file
String filePath = System.getProperty("user.dir")+"\\src\\excelExportAndFileIO";
//Call read file method of the class to read data
objExcelFile.readExcel(filePath,"Filename.xlsx","Filename");
}
}