Skip to content

Instantly share code, notes, and snippets.

@erajanraja24
Created July 27, 2017 17:28
Show Gist options
  • Save erajanraja24/49508241f6f398de5c9e30ff238593fc to your computer and use it in GitHub Desktop.
Save erajanraja24/49508241f6f398de5c9e30ff238593fc to your computer and use it in GitHub Desktop.
ExcelRead
package excel.read.write;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class ExcelRead {
public static String getvalue(int colno,int rowno,String shtno,String path) throws IOException
{
FileInputStream file;
file = new FileInputStream(new File(path));
XSSFWorkbook wb;
wb = new XSSFWorkbook(file);
XSSFSheet sh0=wb.getSheet(shtno);
Cell cell;
int rn = rowno;
Row r = wb.getSheet(shtno).getRow(rn);
if (r == null)
{
// No values exist in this row
r = wb.getSheet(shtno).createRow(rn);
}
cell=sh0.getRow(rowno).getCell(colno);
if (cell == null || cell.getCellType() == Cell.CELL_TYPE_BLANK || cell.getCellType()==Cell.CELL_TYPE_NUMERIC
||cell.getCellType() == Cell.CELL_TYPE_STRING ||cell.getCellType() == Cell.CELL_TYPE_BOOLEAN )
{
// This cell is empty
if (cell == null)
{
cell = r.createCell(colno, Cell.CELL_TYPE_STRING);
wb.close();
return null;
}
wb.close();
return cell.toString();
}
else
{
wb.close();
return cell.toString();
}
}
@SuppressWarnings("resource")
public static int getLastRow(String shtno,String path) throws IOException
{
int lastRow;
FileInputStream file;
file = new FileInputStream(new File(path));
XSSFWorkbook wb;
wb = new XSSFWorkbook(file);
XSSFSheet sh0=wb.getSheet(shtno);
lastRow=sh0.getLastRowNum();
return lastRow;
}
public static String websiteSelected() throws IOException
{
String websiteName;
websiteName=ExcelRead.getvalue(1, 0, "Sheet3", "Upload_Sequence.xlsx");
return websiteName.toUpperCase();
}
public static String username() throws IOException
{
int lrow;
lrow=ExcelRead.getLastRow("Sheet1", "Upload_Sequence.xlsx");
for(int i=1;i<=lrow;i++)
{
if(ExcelRead.websiteSelected().equals(ExcelRead.getvalue(0, i, "Sheet2", "Upload_Sequence.xlsx").toUpperCase()))
{
System.out.println("UserName is "+getvalue(1,i,"Sheet2","Upload_Sequence.xlsx"));
return getvalue(1,i,"Sheet2","Upload_Sequence.xlsx");
}
}
return null;
}
public static String password() throws IOException
{
int lrow;
lrow=ExcelRead.getLastRow("Sheet1", "Upload_Sequence.xlsx");
for(int i=1;i<=lrow;i++)
{
if(ExcelRead.websiteSelected().equals(ExcelRead.getvalue(0, i, "Sheet2", "Upload_Sequence.xlsx").toUpperCase()))
{
System.out.println("Password is "+getvalue(2,i,"Sheet2","Upload_Sequence.xlsx"));
return getvalue(2,i,"Sheet2","Upload_Sequence.xlsx");
}
}
return null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment