Read Write Excel File using Apache POI - java

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();
}
}

Related

How to copy excel sheet to the same worbook?

I'm trying to copy an existing excel sheet to the same workbook(it contains 3 sheets ) in java using Apache poi .
Here is what i did :
FileInputStream file = new FileInputStream(new File("work.xlsx"));
XSSFWorkbook workbook = new XSSFWorkbook(file);
XSSFSheet sheet_copy = workbook.cloneSheet(0);
int num = workbook.getSheetIndex(sheet_copy);
workbook.setSheetName(num, "copy_file");
after running this code, the workbook contains always 3 sheets , the "copy_file" is not created, i'm not getting any errors or exceptions .
any idea ?
You need to open an output stream and write to the workbook. Make sure to close the workbook and the output stream after this write operation.
Demo:
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 Main {
public static void main(String[] args) throws IOException {
FileInputStream file = new FileInputStream(new File("work.xlsx"));
XSSFWorkbook workbook = new XSSFWorkbook(file);
XSSFSheet sheet_copy = workbook.cloneSheet(0);
int num = workbook.getSheetIndex(sheet_copy);
workbook.setSheetName(num, "copy_file");
file.close();
FileOutputStream outputStream = new FileOutputStream("work.xlsx");
workbook.write(outputStream);
workbook.close();
outputStream.close();
}
}

I want to read the data from the Excel in selenium using java, but it is throwing the exception as ""main" java.lang.ExceptionInInitializerError"

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)

Problem with formula when clone from a workbook to another workbook

The formula didn't work at new workbook when I try to clone an old workbook to new workbook. it just displayed value of 0. It might only work when I double-click on it and enter :(
package com.poi.xssf;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
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 Application {
public static void main(String[] args) throws Exception {
FileInputStream fisOld = null;
FileInputStream fisNew = null;
FileOutputStream fos = null;
//initialize vars
File fileOld = new File("test/copied.xlsx");
File fileNew = new File("test/test.xlsx");
fisOld = new FileInputStream(fileOld); // input: access A
//fisNew = new FileInputStream(fileNew);
XSSFWorkbook oldworkbook = new XSSFWorkbook(fisOld);
XSSFWorkbook newworkbook = oldworkbook;
XSSFSheet spreadsheet = newworkbook.getSheet("DeXuat");
Row row = spreadsheet.getRow(0);
Cell cell = row.getCell(0);
cell.setCellFormula("B1-C1");
fos = new FileOutputStream(fileNew);
newworkbook.write(fos);
}
}

Getting last row value using apache poi

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

Reading and writing excel sheet using java apache POI

I am reading excel sheet using java apache POI. After reading i want to write on the same row, The ode is reading perfectly but not writing I want to create 6th column in every row in each iteration.... here is the code
/**
* Created by Muhammad Hussain on 26/10/2016.
*/
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Iterator;
import org.apache.poi.ss.usermodel.Cell;
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;
import java.util.Scanner;
/**
* A dirty simple program that reads an Excel file.
* #author www.codejava.net
*
*/
public class ReadExcel {
public static void main(String[] args) throws IOException {
String excelFilePath = "C:\\Users\\Muhammad Hussain\\Desktop\\Data-Collection.xlsx";
FileInputStream inputStream = new FileInputStream(new File(excelFilePath));
Workbook workbook = new XSSFWorkbook(inputStream);
Sheet firstSheet = workbook.getSheetAt(4);
Scanner input =new Scanner(System.in);
for (int rowIndex = 1; rowIndex <= 5; rowIndex++) {
Row row = firstSheet.getRow(rowIndex);
Cell cell = row.getCell(3);
String review = cell.getStringCellValue();
System.out.println(review);
String label =input.next();
row.createCell(6).setCellValue(label);
}
inputStream.close();
inputStream.close();
}
}
You are only reading the file and not writing it. You have to put something like this in the end:
FileOutputStream out = new FileOutputStream(new File("..."));
workbook.write(out);
out.close();

Categories