Getting java.lang.NullPointerException when i try to get the value of the
last row in a excel sheet using getLastRowNum() function.
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class Excel{
public static File file;
public static FileInputStream input;
public static FileOutputStream output;
public static HSSFWorkbook book;
public static HSSFSheet sheet;
public static int value;
public Excel(String path) {
try {
file= new File(path);//creating file//
input=new FileInputStream(file);
book=new HSSFWorkbook(input);
sheet=book.getSheetAt(0);
output=new FileOutputStream(file);
}
catch(Exception e) {
e.getMessage();
}
}
public static void readData() {
int value =sheet.getLastRowNum();//trying to get the last row value//
System.out.println(value);
}
}
Driver class:
public class ExcelTest {
public static void main(String[] args) {
Excel excel = new Excel("C:/Users/HOME/Desktop/Sample.xlsx");
Excel.readData();
}
}
Please help on the issue.
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class readExcel {
public static void main(String[] args) throws Exception{
readData("path to sample.xlsx");
}
public static void readData(String path) throws FileNotFoundException, IOException{
//getting xlsx file from path
FileInputStream file = new FileInputStream(new File(path));
XSSFWorkbook workbook = new XSSFWorkbook(file);
//pointing to particlar workbook
XSSFSheet spreadsheet = workbook.getSheetAt(0);
//getting no: of rows
int value=spreadsheet.getLastRowNum()+1;
System.out.println("total rows:"+value);
}
}
Please add dom4j-1.6.1,xmlbeans-2.3.0 jars along with the poi-3.9 jar file
Related
I want to read the data from the Excel in selenium using java, but it is throwing the exception as below:
"main" java.lang.ExceptionInInitializerErrorat org.apache.poi.ss.util.CellReference.<init>(CellReference.java:110).
Tried multiple ways, but still getting the exception of main.
I have created the folder as "excel" in the selenium project, in which I have pasted the excel.
package utils;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
public class GetRowCount {
public static void main(String[] args) throws Exception {
ReadExcel();
}
public static void ReadExcel()
{
File src = new File("C:....");
FileInputStream fis = new FileInputStream(src);
XSSFWorkbook wb = new XSSFWorkbook("fis");
XSSFSheet sheet1 = wb.getSheetAt(0);
String data0 =sheet1.getRow(0).getCell(0).getStringCellValue();
System.out.println("Data from Excel is "+data0);
}
This is wrong:
XSSFWorkbook wb = new XSSFWorkbook("fis");
should be:
XSSFWorkbook wb = new XSSFWorkbook(fis);
Tested with file like
and modified class:
package utils;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class GetRowCount {
public static void main(String[] args) throws Exception {
ReadExcel();
}
public static void ReadExcel() throws IOException {
File src = new File("C:\\test.xlsx");
FileInputStream fis = new FileInputStream(src);
XSSFWorkbook wb = new XSSFWorkbook(fis);
XSSFSheet sheet1 = wb.getSheetAt(0);
String data0 = sheet1.getRow(0).getCell(0).getStringCellValue();
System.out.println("Data from Excel is " + data0);
// don't forget to close the workbook
wb.close();
}
}
Output:
Data from Excel is FOO
PS: I'm using Apache POI 4.1.2
Reading an excel sheet is basically called as utils, don't mixed reading data code, and your automation code
create utils class and use (property file reader)
I am getting error like [ Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/collections4/ListValuedMap
at excelExport.ReadWriteExcel1.main(ReadWriteExcel1.java:15)) ] . Can Someone please guide me for the same.
package excelExport;
import java.io.File;
import java.io.FileInputStream;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class ReadWriteExcel1
{
public static void main (String []args) throws Exception
{
File src=new File("C:\\Users\\techbrain\\Downloads\\Selenium Jar\\TestData.xlsx");
FileInputStream fis = new FileInputStream(src);
XSSFWorkbook wb=new XSSFWorkbook(fis);
XSSFSheet sheet=wb.getSheetAt(0);
int rowcount=sheet.getLastRowNum()+1;
System.out.println("Total rows is "+rowcount);
for(int i=0; i<rowcount; i++)
{
String data0=sheet.getRow(i).getCell(0).getStringCellValue();
System.out.println("Data from Row"+i+" is "+data0);
}
wb.close();
}
}
I am trying to insert an image into the Excel sheet.
Succeded in inserting into the Excel sheet, but the problem comes here.
When I am trying to insert multiple images into the excel sheet, then the first image is getting removed. From two days I am trying to resolve this but nowhere I can see a green signal if someone has any idea please help me out.
Below is my code :
public static void main(String args[]) throws IOException {
openExcel();
pasteInExcel();
openExcel();
pasteInExcel();
openExcel();
pasteInExcel();
}
private static void pasteInExcel() throws IOException {
InputStream my_banner_image = new FileInputStream(imgPath);
byte[] bytes = org.apache.poi.util.IOUtils.toByteArray(my_banner_image);
int my_picture_id = my_workbook.addPicture(bytes, Workbook.PICTURE_TYPE_PNG);
my_banner_image.close();
XSSFDrawing drawing = my_sheet.createDrawingPatriarch();
XSSFPicture my_picture = drawing.createPicture(getAnchorPoint(), my_picture_id);
my_picture.resize();
fileClose();
}
public static void openExcel() throws IOException {
File f = new File(excelPath);
if (!f.exists()) {
my_workbook = new XSSFWorkbook();
my_sheet = my_workbook.createSheet("MyLogo");
} else {
my_workbook = new XSSFWorkbook(new FileInputStream(excelPath));
my_sheet = my_workbook.getSheet("MyLogo");
}
}
public static XSSFClientAnchor getAnchorPoint() {
System.out.println("Row is "+row);
XSSFClientAnchor my_anchor = new XSSFClientAnchor();
my_anchor.setCol1(2);
my_anchor.setRow1(row);
row = row + 5;
return my_anchor;
}
public static void fileClose() throws IOException {
FileOutputStream fos = new FileOutputStream(excelPath);
my_workbook.write(fos);
fos.close();
}
Here is the result
I've run your code (I've added some missing fields declaration which you did not provide). But could not reproduce your problem. This is the output excel file.
In my case I'm using maven with the following dependencies:
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.11</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.11</version>
</dependency>
This is the full code (based on the code that you have supplied):
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFClientAnchor;
import org.apache.poi.xssf.usermodel.XSSFDrawing;
import org.apache.poi.xssf.usermodel.XSSFPicture;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class Main{
private static Workbook my_workbook;
private static Sheet my_sheet;
private static String imgPath;
private static String excelPath;
private static int row=0;
public static void main(String args[]) throws IOException {
init();
openExcel();
pasteInExcel();
openExcel();
pasteInExcel();
openExcel();
pasteInExcel();
}
private static void init() {
excelPath = "C:\\temp\\test.xlsx";
imgPath = "C:\\temp\\test-image.png";
}
private static void pasteInExcel() throws IOException {
InputStream my_banner_image = new FileInputStream(imgPath);
byte[] bytes = org.apache.poi.util.IOUtils.toByteArray(my_banner_image);
int my_picture_id = my_workbook.addPicture(bytes, Workbook.PICTURE_TYPE_PNG);
my_banner_image.close();
XSSFDrawing drawing = (XSSFDrawing) my_sheet.createDrawingPatriarch();
XSSFPicture my_picture = drawing.createPicture(getAnchorPoint(), my_picture_id);
my_picture.resize();
fileClose();
}
public static void openExcel() throws IOException {
File f = new File(excelPath);
if (!f.exists()) {
my_workbook = new XSSFWorkbook();
my_sheet = my_workbook.createSheet("MyLogo");
} else {
my_workbook = new XSSFWorkbook(new FileInputStream(excelPath));
my_sheet = my_workbook.getSheet("MyLogo");
}
}
public static XSSFClientAnchor getAnchorPoint() {
System.out.println("Row is "+row);
XSSFClientAnchor my_anchor = new XSSFClientAnchor();
my_anchor.setCol1(2);
my_anchor.setRow1(row);
row = row + 5;
return my_anchor;
}
public static void fileClose() throws IOException {
FileOutputStream fos = new FileOutputStream(excelPath);
my_workbook.write(fos);
fos.close();
}
}
Proposed solution:
Try to change the versions of poi and poi-ooxml to 3.11
I'm trying to write a code to automate a website testing for mobile using Appium. I need to read data from Excel. I'm writing the project as a Maven code. What all are to be taken from the maven repository?
NB:I'm a newbie to Java and any type of automation.
You should use jxl or poi jar file to read data from excel,it also provide maven dependencies
You can use poi.
Add the dependency in pom.xml
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.13</version>
</dependency>
Add a utility class in your project like below: make sure you get the sheet name(Sheet1 in class) correct
package com.appium.utils;
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;
import org.apache.poi.util.POILogger;
import org.apache.poi.openxml4j.opc.PackageRelationshipCollection;
public class ExcelDriven {
public static XSSFWorkbook wb;
public static XSSFSheet sheet;
public static XSSFRow row;
public static XSSFCell cell;
public static FileInputStream fis;
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
// fis = new FileInputStream("/Users/tabish/Downloads/data.xlsx");
}
public static String getCellData(int rownum,int col, String fileName)
{
try {
fis = new FileInputStream("/path/to/file/"+fileName+".xlsx");
wb = new XSSFWorkbook(fis);
sheet= wb.getSheet("Sheet1");
row = sheet.getRow(rownum);
cell = row.getCell(col);
// System.out.println(cell.getStringCellValue());
if (cell==null){
return "";
}
return cell.getStringCellValue();
}
catch (Exception e)
{
System.out.println("In the Catch Block:"+e);
return "Exception Occured";
}
}
}
Accesing Data
while (!ExcelDriven.getCellData(0,i,"fileName").equals(""))
{
SomeClass.SomeMethod(ExcelDriven.getCellData(0,i,"fileName"));
i++;
}
package jexcel.jxl.nimit;
import java.io.*;
import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
import jxl.read.biff.File;
public class ExampleJxl {
/**
* #param args
*/
public static void main(String[] args)throws IOException, BiffException {
ExampleJxl.ExcelFile("D:/nimit.xls");
}
public static String ExcelFile(String path){
Workbook workbook = Workbook.getWorkbook(File(path));
Sheet sheet = workbook.getSheet(0);
Cell a1 = sheet.getCell(0,0);
Cell a2 = sheet.getCell(0,1);
String s1=a1.getContents();
String s2=a2.getContents();
System.out.println("My name is"+a1+"\t"+a2);
}
}
I don't understand why the File(path) show a error The method File(String) is undefined for the type ExampleJxl
I'm trying to print my name entered in the excel file.
Chang your code from
Workbook workbook = Workbook.getWorkbook(File(path));
to
Workbook workbook = Workbook.getWorkbook(new java.io.File(path));