Reading content from an Excel file - java

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

Related

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)

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

How to access XSSF content when no sheets are recognized

I am trying to read in an excel file that is produced by a web app. I can't get to any of the columns or rows, though, because the XSSF tool keeps telling me I have no worksheets. The single worksheet is called "MySheet", but when I search for it by name, I get a -1. When I search for the number of worksheets, it tells me I have 0. What I am doing wrong?
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
public class excelParser {
private static final String FILE_NAME = "C:\\Users\\me\\Downloads\\output.xlsx";
public static void main(String[] args) throws FileNotFoundException {
if (new File(FILE_NAME).exists()){
FileInputStream excelFile = new FileInputStream(new File(FILE_NAME));
Workbook workbook = new XSSFWorkbook();
//Sheet datatypeSheet = workbook.getSheet("MySheet");
//System.out.println(datatypeSheet.getSheetName());
System.out.println(workbook.getSheetIndex("MySheet"));
}
else System.out.println("file not found");
}
}
The problem is that you are not using the file in your Workbook workbook = new XSSFWorkbook(); constructor. Try with Workbook workbook = new XSSFWorkbook(excelFile);

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

Writing to an Existing Excel File

package jexcel.jxl.nimit;
import java.awt.Label;
import java.io.File;
import java.io.IOException;
import jxl.Cell;
import jxl.CellType;
import jxl.LabelCell;
import jxl.NumberCell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
import jxl.write.WritableCell;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;
public class ExcelJxl {
/**
* #param args
* #throws IOException
* #throws BiffException
* #throws WriteException
* #throws RowsExceededException
*/
public static void main(String[] args) throws BiffException, IOException, RowsExceededException, WriteException {
// TODO Auto-generated method stub
ExcelJxl.WriteFile("D:\nimit.xls");
}
public static void WriteFile(String path) throws BiffException, IOException, RowsExceededException, WriteException{
Workbook wb=Workbook.getWorkbook(new File(path));
WritableWorkbook copy=Workbook.createWorkbook(new File("D:\temp.xls"),wb);
WritableSheet sheet = copy.getSheet(1);
WritableCell cell = sheet.getWritableCell(0,0);
String S="nimit";
if (cell.getType() == CellType.LABEL)
{
LabelCell l = (LabelCell) cell;
l.setString(S);
}
copy.write();
copy.close();
wb.close();
}
}
I have edited my program, and now it says that setString() The method setString(String) is undefined for the type LabelCell I read the Documentation, there is a method setString in the LabelCell type.
LabelCell is just an interface with only one method i.e getString() you can learn more about it here
You should use jxl.write.Label instead.
What you should exactly do is as follows
You should import the following file
import jxl.write.Label
Then following is the code for adding a cell at desired location to an excel file
Workbook existingWorkbook = Workbook.getWorkbook(new File(fileToEdit.getAbsolutePath()));
WritableWorkbook workbookCopy = Workbook.createWorkbook(new File("output.xls"), existingWorkbook);
WritableSheet sheetToEdit = workbookCopy.getSheet(sheetName);
WritableCell cell;
Label l = new Label(currentColumn, currentRow, value);
cell = (WritableCell) l;
sheetToEdit.addCell(cell);
workbookCopy.write();
workbookCopy.close();
existingWorkbook.close();
currentColumn and currentRow define the index and value contains the String to be placed in that cell.
Hope it helps

Categories