org.openqa.selenium.ElementNotVisibleException: Element is not currently visible - java

Below is my code.Whne i input url from excel most of the time it shows org.openqa.selenium.ElementNotVisibleException: Element is not currently visible error.
For site like www.travelocity.com it shows after clicking 7 8 links but for www.google.com it shows error from starting.
package test;
import java.awt.HeadlessException;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Properties;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class Linktestparam {
public static void main( String[] args ) throws Exception{
Properties prop = new Properties();
FileInputStream f = new FileInputStream("C:\\Documents and Settings\\bibekananda.sarangi\\workspace\\test\\src\\excelPath");
prop.load(f);
String[][] steps ;
steps = excelRead(prop.getProperty("Linkpath"));
int totallink;
for(int j = 1; j <= steps.length ; j++){
//System.out.println("no of links in " + steps[j][0] + "is" + totallink);
totallink = linktest(steps[j][0]);
}
}
public static int linktest(String url) throws Exception{
WebDriver driver = new FirefoxDriver();
driver.navigate().to(url);
Thread.sleep(12000);
//WebDriverWait wait = new WebDriverWait(driver,60);
//wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("gsr")));
List<WebElement> alllinkspresent=driver.findElements(By.tagName("a"));
int totallink = driver.findElements(By.tagName("a")).size();
System.out.println("no of links in " + totallink);
for (int i = 0; i < totallink; i++)
{
int LastRow = i;
driver.findElements(By.tagName("a")).get(i).getText();
driver.findElements(By.tagName("a")).get(i).click();
System.out.println("LastRow value is" + LastRow);
Thread.sleep(18000);
String pagetitle = driver.getTitle();
System.out.println(pagetitle);
String urltext=driver.getCurrentUrl();
System.out.println(urltext);
if(pagetitle.contains("404")) {
System.out.println("404 Found");
System.out.println("FAIL");
String status="FAIL";
excelwrite(status,urltext,LastRow);
}
else{
System.out.println("PASS");
String status = "PASS";
excelwrite(status,urltext,LastRow);
driver.navigate().back();
Thread.sleep(4000);
}
}
return totallink;
//driver.close();
}
public static String[][] excelRead(String fileName) throws Exception {
File excel = new File(fileName);
FileInputStream fis = new FileInputStream(excel);
HSSFWorkbook wb = new HSSFWorkbook(fis);
HSSFSheet ws = wb.getSheet("Sheet1");
int rowNum = ws.getLastRowNum() + 1;
int colNum = ws.getRow(0).getLastCellNum();
String[][] data = new String[rowNum][colNum];
for (int i = 0 ; i < rowNum ; i++) {
HSSFRow row = ws.getRow(i);
for (int j=0 ; j < colNum ; j++){
HSSFCell cell = row.getCell(j);
String value = cellToString(cell);
data[i][j] = value;
System.out.println("The value is" + value);
}
}
return data;
}
public static String[][] excelwrite(String status,String urltext,int LastRow) throws Exception {
try{
FileInputStream file = new FileInputStream(new File("D:\\Work\\link.xls"));
HSSFWorkbook workbook = new HSSFWorkbook(file);
HSSFSheet sheet = workbook.getSheetAt(1);
Row row = sheet.createRow(LastRow);
System.out.println("LastRow value in excelwrite is " + LastRow);
Cell cell2 = row.createCell(0);
Cell cell3 = row.createCell(1);
cell3.setCellValue(status);
cell2.setCellValue(urltext);
System.out.println(status);
file.close();
FileOutputStream outFile =new FileOutputStream(new File("D:\\Work\\link.xls"));
workbook.write(outFile);
}
catch (FileNotFoundException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
catch (HeadlessException e)
{
e.printStackTrace();
}
return null;
}
public static String cellToString(HSSFCell cell) {
int type;
Object result ;
type = cell.getCellType();
switch (type) {
case 0 :
result = cell.getNumericCellValue();
break;
case 1 :
result = cell.getStringCellValue();
break;
default :
throw new RuntimeException("There are no support for this type of cell");
}
return result.toString();
}
}
============================
OUTPUT:::::::::::
The value isurl
The value ishttps://www.google.co.in/
The value ishttp://www.espire.com/contact-us
The value ishttp://www.travelocity.com/
no of links in 44
Exception in thread "main" org.openqa.selenium.ElementNotVisibleException: Element is not currently visible and so may not be interacted with
Command duration or timeout: 0 milliseconds

You need to use WebdriverWait for visibility of elements Code:
new WebDriverWait(driver, 30).until(ExpectedConditions.elementToBeClickable(By.xpath("Xpath"));

Related

i want to get&set the value in excel .xls file and get the error message from application

i want to get the (valid and invalid) data from excel .xls file and apply that in my application login page.when the error message is display.i want to write failed in excel file.from my code, i can get the value from excel and apply it in my application but cant write the output in excel.i attached my code here.anyone please help me to fix this.
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.poi.EncryptedDocumentException;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
public class exceltowrite {
public FileInputStream fis = null;
public FileOutputStream fos = null;
public HSSFWorkbook workbook = null;
public HSSFSheet sheet = null;
public HSSFRow row = null;
public HSSFCell cell = null;
String xlFilePath;
public exceltowrite(String string, String xlFilePath) throws IOException {
this.xlFilePath = xlFilePath;
fis = new FileInputStream(xlFilePath);
workbook = new HSSFWorkbook(fis);
fis.close();
}
public static void main(String[]args) throws EncryptedDocumentException, InvalidFormatException, IOException, InterruptedException {
for(int i=1;i<=29;i++) {
String u1 =exceltowrite.getdata(i,0);
String p1=exceltowrite.getdata(i,1);
WebDriver d1=new FirefoxDriver();
d1.get("http:\\www.google.com");
d1.get("napplication site");
d1.findElement(By.name("AdminLoginForm[email]")).sendKeys(u1);
d1.findElement(By.name("AdminLoginForm[password]")).sendKeys(p1);
d1.findElement(By.name("login-button")).click();
Thread.sleep(2000);
WebElement e1= d1.findElement(By.xpath("email error message path"));
String s1=e1.getText();
WebElement e2=d1.findElement(By.xpath("password error message path"));
String s2=e2.getText();
if(s1.equals("Email cannot be blank.")) {
System.out.println("emailid failed");
d1.quit();
if(s2.equals("Password cannot be blank.")) {
System.out.println("password failed");
d1.quit();
} else {
System.out.println("password pass");
d1.quit();
}
} else {
d1.quit();
}
}
}
public static String getdata(int i, int j) throws EncryptedDocumentException, InvalidFormatException, IOException {
FileInputStream fis=new FileInputStream("file path");
Workbook wb=WorkbookFactory.create(fis);
String s=wb.getSheet("Sheet1").getRow(i).getCell(j).getStringCellValue();
return s;
}
public boolean setCellData(String sheetName, int colNumber, int rowNum, String value)
{
try
{
sheet = workbook.getSheet(sheetName);
row = sheet.getRow(rowNum);
System.out.println(row);
cell = row.getCell(colNumber);
System.out.println(cell);
cell.setCellValue(value);
fos = new FileOutputStream(xlFilePath);
workbook.write(fos);
fos.close();
System.out.println("Finished");
}
catch (Exception ex)
{
ex.printStackTrace();
return false;
}
return true;
}}
}
my another class is this
public class Demo {
public static void main(String args[]) throws Exception
{
// exceltowrite at=new exceltowrite("file path");
for(int j=0;j<=29;j++){
at.setCellData("Sheet1",2,j,"failed");
}}}
public static void write() throws IOException {
String home = System.getProperty("user.home");
String downlpad = home + "\\Downloads";
File file = new File(downlpad + "\\" + "Suhit-File-to-Download.xlsx");
FileOutputStream outputStream = new FileOutputStream(file);
XSSFWorkbook workbook = new XSSFWorkbook();
XSSFSheet sheet = workbook.createSheet("Java Books");
ArrayList<Object> bookData = new ArrayList<>();
bookData.add("Name");
bookData.add("Address");
bookData.add("Number");
XSSFRow row = sheet.createRow(0);
for (int i = 0; i < bookData.size(); i++) {
XSSFCell cell3 = row.createCell(i);
cell3.setCellValue((String) bookData.get(i));
}
workbook.write(outputStream);
workbook.close();
}

Error while using TestNg DataProvider must return either Object[][] or Iterator<Object>[], not class [[Ljava.lang.Object;

Testcase:
package Test;
import org.testng.annotations.AfterTest;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import Page.LoginPage;
import Utility.TestDataProvider;
import Utility.TestUtil;
//Testcase to perform login
public class LoginTest {
LoginPage page=new LoginPage();
#Test(dataProvider="ExcelDataProvider",dataProviderClass=TestDataProvider.class)
public void Logintest(String username,String password){
//page.login("user#phptravels.com","demouser");
page.login(username,password);
}
#AfterTest
public void TearDown(){
TestUtil.quit();
}
}
DataProvider:
package Utility;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
public class TestDataProvider {
static ExcelReader reader=new ExcelReader();
#DataProvider(name="ExcelDataProvider")
public static Object[][] ExcelDataProvider()
{
Object [][] rest = reader.readDataExcel("UserLogin");
return rest;
}
}
ExcelReader:
package Utility;
import java.io.File;
import java.io.FileInputStream;
import java.util.List;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.DataFormatter;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFTable;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class ExcelReader {
static String fileName="Testdata";
public Object[][] readDataExcel(String TableName){
String filePath="F:\\TravelSite\\TravelSiteAutomation\\Testdata.xlsx";
File file =new File(filePath);
try{
FileInputStream stream=new FileInputStream(file);
Workbook workbook = new XSSFWorkbook(stream);
XSSFSheet sheet=(XSSFSheet) workbook.getSheet("UnitTest");
List<XSSFTable> tables = sheet.getTables();
for(XSSFTable table:tables){
String name=table.getName();
if(name.equals(TableName)){
int ci=0,cj=0;
int rowNum=sheet.getLastRowNum()-sheet.getFirstRowNum();
//System.out.println(rowNum);
String[][] dataArray=new String[rowNum][100];
for(int i = 1; i < rowNum+1; i++) {
Row row = sheet.getRow(i);
//Create a loop to print cell values in a row
int colNum=row.getLastCellNum();
// System.out.println(colNum);
for(int j = 0; j <colNum; j++) {
//Print excel data in console
Cell cell=row.getCell(j);
DataFormatter formatter = new DataFormatter();
String var_name = formatter.formatCellValue(cell);
System.out.print(var_name+" || ");
dataArray[ci][cj]=var_name;
}
System.out.println();
}
workbook.close();
return dataArray;
}
}
Log.info("Table name is incorrect");
return null;
}
catch(Exception e)
{
Log.debug(e.getMessage());
return null;
}
}
}
so when i try to run the testcase i get an error
org.testng.TestNGException:
Data Provider public static java.lang.Object[][]
Utility.TestDataProvider.ExcelDataProvider() must return either
Object[][] or Iterator[], not class [[Ljava.lang.Object;
Nothing seems to resolve it,POI version 3.15, TestNGversion 6.10
The problem is with the ExcelReader class. Please find below the fixed version of the same which should solve the problem
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.DataFormatter;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFTable;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.util.ArrayList;
import java.util.List;
public class ExcelReader {
public static String[][] readDataExcel(String TableName) {
String filePath = "src/test/resources/Testdata.xlsx";
try {
Workbook workbook = new XSSFWorkbook(filePath);
XSSFSheet sheet = (XSSFSheet) workbook.getSheet("UnitTest");
List<XSSFTable> tables = sheet.getTables();
for (XSSFTable table : tables) {
String name = table.getName();
if (! name.equals(TableName)) {
continue;
}
int rowCount = sheet.getLastRowNum() - sheet.getFirstRowNum();
System.out.println(rowCount);
int columnCount = - 1;
List<List<String>> data = new ArrayList<>();
for (int i = 1; i < rowCount + 1; i++) {
Row row = sheet.getRow(i);
int colNum = row.getLastCellNum();
if (columnCount == - 1) {
columnCount = colNum;
}
List<String> rowData = new ArrayList<>();
for (int j = 0; j < colNum; j++) {
Cell cell = row.getCell(j);
DataFormatter formatter = new DataFormatter();
String var_name = formatter.formatCellValue(cell);
if (var_name != null && ! var_name.trim().isEmpty()) {
rowData.add(var_name);
}
}
if (! rowData.isEmpty()) {
data.add(rowData);
}
}
workbook.close();
String[][] dataArray = new String[rowCount-1][columnCount];
int rowIndex = 0;
for (List<String> row : data) {
int colIndex = 0;
for (String rowData : row) {
dataArray[rowIndex][colIndex++] = rowData;
}
rowIndex++;
}
return dataArray;
}
System.err.println("Table name is incorrect");
return new String[][] {{}};
} catch (Exception e) {
e.printStackTrace();
return new String[][] {{}};
}
}
}

reading multiple csv files and putting it in different sheets in excel not working

I am trying to read few csv files which are present inside a folder and putting the absolute paths inside a list and then looping to the list and inserting data into excel in different sheets but for some reason only last loop is overwriting everything.
package excel;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.List;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
public class FileReading {
public static void csvToXLS(String filename, String sheetno) {
try {
String csvFile = filename; //csv file address
String excelFile = "E:\\test.xls"; //xlsx file address
HSSFWorkbook workBook = new HSSFWorkbook();
HSSFSheet sheet = workBook.createSheet(sheetno);
String currentLine = null;
int RowNum = 0;
BufferedReader br = new BufferedReader(new FileReader(csvFile));
while ((currentLine = br.readLine()) != null) {
String str[] = currentLine.split(",");
HSSFRow currentRow = sheet.createRow(RowNum);
RowNum++;
for (int i = 0; i < str.length; i++) {
currentRow.createCell(i).setCellValue(str[i]);
}
}
FileOutputStream fileOutputStream = new FileOutputStream(excelFile);
workBook.write(fileOutputStream);
fileOutputStream.close();
System.out.println("Done");
} catch (Exception ex) {
System.out.println(ex.getMessage() + "Exception in try");
}
}
public static List listOfFiles() {
List<String> results = new ArrayList<String>();
File[] files = new File("f:\\csv").listFiles();
for (File file : files) {
if (file.isFile()) {
results.add(file.getAbsolutePath());
}
}
return results;
}
public static void main(String[] args) {
List list = listOfFiles();
int size = list.size();
for (int i = 0; i < size; i++) {
csvToXLS(list.get(i).toString(), "sheet" + i + 1);
}
}
}
You're re-creating and saving the Workbook every time you call csvToXLS, so only the last call survives, overwriting all others.
Create the Workbook only once, before you start your for loop, passing it into csvToXLS. then that method will create a new Sheet on an existing Workbook. Then after the for loop ends, you can write out and save the Workbook.
I did this thanks for the idea #rgettman
package excel;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.List;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
public class CsvToXlx {
public static void csvToXLSX(HSSFWorkbook workBook,String filename, String sheetno) {
try {
String csvFile = filename; //csv file address
String excelFile = "E:\\test.xls"; //xlsx file address
//HSSFWorkbook workBook = new HSSFWorkbook();
HSSFSheet sheet = workBook.createSheet(sheetno);
String currentLine = null;
int RowNum = 0;
BufferedReader br = new BufferedReader(new FileReader(csvFile));
while ((currentLine = br.readLine()) != null) {
String str[] = currentLine.split(",");
HSSFRow currentRow = sheet.createRow(RowNum);
RowNum++;
for (int i = 0; i < str.length; i++) {
currentRow.createCell(i).setCellValue(str[i]);
}
}
FileOutputStream fileOutputStream = new FileOutputStream(excelFile);
workBook.write(fileOutputStream);
fileOutputStream.close();
System.out.println("Done");
} catch (Exception ex) {
System.out.println(ex.getMessage() + "Exception in try");
}
}
public static List listOfFiles() {
List<String> results = new ArrayList<String>();
File[] files = new File("f:\\csv").listFiles();
for (File file : files) {
if (file.isFile()) {
results.add(file.getAbsolutePath());
}
}
return results;
}
public static void main(String[] args) {
List list = listOfFiles();
int size = list.size();
HSSFWorkbook workBook = new HSSFWorkbook();
for (int i = 0; i < size; i++) {
csvToXLSX(workBook,list.get(i).toString(), "sheet" + i + 1);
}
}
}

Poi Formula evaluation Error

I am using poi-3.8-20120326.jar and Excel 2007, trying to collect all the cell value in one list.
I am using formula for date increment operation. For example: =(i2+3) for k2 cell.
While evaluating this formula through my java code returns #VALUE! (i2 is a date cell.)
Without source it's hard to guess what's wrong, but I think that are two things that you may forget.
1) FormulaEvaluator
2) DataFormatter
Take a look at example.
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.DataFormatter;
import org.apache.poi.ss.usermodel.FormulaEvaluator;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.FileNotFoundException;
public class CellReader {
private Workbook workbook;
private DataFormatter formatter;
private FormulaEvaluator evaluator;
private void openWorkbook(String file) throws FileNotFoundException,
IOException, InvalidFormatException {
FileInputStream fis = null;
try {
fis = new FileInputStream(file);
workbook = WorkbookFactory.create(fis);
evaluator = workbook.getCreationHelper().createFormulaEvaluator();
formatter = new DataFormatter(true);
} finally {
if (fis != null) {
fis.close();
}
}
}
private void printXLSX() {
Sheet sheet = null;
Row row = null;
Cell cell = null;
int lastRowNum;
int lastCellNum;
sheet = workbook.getSheetAt(0);
lastRowNum = sheet.getLastRowNum();
for (int j = 0; j <= lastRowNum; j++) {
row = sheet.getRow(j);
if (row == null) {
continue;
}
lastCellNum = row.getLastCellNum();
for (int k = 0; k <= lastCellNum; k++) {
cell = row.getCell(k);
if (cell == null) {
continue;
}
if (cell.getCellType() != Cell.CELL_TYPE_FORMULA) {
System.out.println(formatter.formatCellValue(cell));
} else {
System.out.println(formatter.formatCellValue(cell,evaluator));
}
}
}
}
public static void main(String[] args) {
CellReader converter = null;
try {
converter = new CellReader();
converter.openWorkbook("a.xlsx");
converter.printXLSX();
} catch (Exception ex) {
ex.printStackTrace();
}
}
}

How to read values from an excel file using Apache POI

I am currently working on a project for my computer science class, and I was trying to figure out how to retrieve and print certain values from a file in excel. Such as, how would I go about printing the integer in column J, row 6?
Better yet, is there a way for me to return the row number of a string in column 1? Such as, if I had a string "Phone" in column 1, could I use a command to return the row number of the first instance of "phone"?
I have looked at other questions, none of which sufficiently answered my own.
Here you go refer to this class file for iterating over an excel file
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.text.ParseException;
import java.util.Iterator;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.DataFormatter;
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 Test {
private static final String FILE_NAME = "/users/developer/Documents/myFile.xlsx";
public void employeesUpload() {
String fName = "";
String lName = "";
String phoneNumber = "";
String email = "";
String gender = "";
String employeeCode = "";
try {
FileInputStream excelFile = new FileInputStream(new File(FILE_NAME));
Workbook workbook = new XSSFWorkbook(excelFile);
Sheet datatypeSheet = workbook.getSheetAt(0);
Iterator<Row> iterator = datatypeSheet.iterator();
int rowIndex = 0;
DataFormatter formatter = new DataFormatter();
while (iterator.hasNext()) {
Row currentRow = iterator.next();
if (rowIndex > 0) {
Iterator<Cell> cellIterator = currentRow.iterator();
employeeCode = fName = lName = phoneNumber = email = gender = "";
int cellIndex = 0;
while (cellIndex <= 5) {
Cell currentCell = currentRow.getCell(cellIndex);
if (cellIndex == 4) {
employeeCode = formatter.formatCellValue(currentCell).trim();
}
if (cellIndex == 1) {
fName = formatter.formatCellValue(currentCell).trim();
}
if (cellIndex == 2) {
lName = formatter.formatCellValue(currentCell).trim();
}
if (cellIndex == 0) {
email = formatter.formatCellValue(currentCell);
email = email.trim().toLowerCase();
}
if (cellIndex == 3) {
phoneNumber = formatter.formatCellValue(currentCell).trim();
}
cellIndex++;
}
Cell resultCell = currentRow.getCell(7);
if (resultCell == null) {
resultCell = currentRow.createCell(7);
}
Cell employementIdCell = currentRow.getCell(8);
if (employementIdCell == null) {
employementIdCell = currentRow.createCell(8);
}
if (true) {
resultCell.setCellType(Cell.CELL_TYPE_STRING);
employementIdCell.setCellValue("Success");
resultCell.setCellValue(email);
} else {
resultCell.setCellType(Cell.CELL_TYPE_STRING);
resultCell.setCellValue("Error");
}
}
rowIndex++;
}
FileOutputStream outputStream = new FileOutputStream(FILE_NAME);
workbook.write(outputStream);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) throws ParseException, UnsupportedEncodingException {
Test employeesBulkUpload = new Test();
employeesBulkUpload.employeesUpload();
}
}
Hope this helps :)
user https://github.com/jueyue/easypoi this jar
use annotion to easy read excel
public class ExcelImportNewDateTest {
#Test
public void importTest() {
ImportParams params = new ImportParams();
params.setTitleRows(1);
params.setHeadRows(1);
long start = new Date().getTime();
List<NewDateEntity> list = ExcelImportUtil.importExcel(
new File(FileUtilTest.getWebRootPath("import/ExcelNewDateTest.xlsx")), NewDateEntity.class, params);
System.out.println(new Date().getTime() - start);
Assert.assertEquals(list.size(), 100);
System.out.println(list.size());
System.out.println(ReflectionToStringBuilder.toString(list.get(1)));
}
}

Categories

Resources