How to access XSSF content when no sheets are recognized - java

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

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

Excel sheet formatting using apache POI

I am working on creating an excel report from java code using apache POI library. I have encountered a problem while formatting excel cell. Please find below piece of code.
XSSFWorkbook workbook = new XSSFWorkbook();
XSSFSheet sheet = workbook.createSheet("coverageData");
int rownum = 0,cellnum=0;
Row row1 = sheet.createRow(rownum++);
Cell cell10 = row1.createCell(cellnum++);
cell10.setCellValue("cell data");
XSSFCellStyle row1style = (XSSFCellStyle)cell10.getCellStyle();
XSSFColor grayColor = new XSSFColor(Color.DARK_GRAY);
row1style.setFillBackgroundColor(grayColor);
cell10.setCellStyle(row1style);
try
{
//Write the workbook in file system
FileOutputStream out = new FileOutputStream(new File("P:\\automation\\Spreadsheet.xlsx"));
workbook.write(out);
out.close();
System.out.println("Spreadsheet.xlsx written successfully on disk.");
}
catch (Exception e)
{
e.printStackTrace();
}
The problem here is i am setting cell10 style in last line of code, but it is not getting affected in the excel sheet created by the program.
Thanks in advance.
Try it, bellow work fine for me:
import java.io.File;
import java.io.FileOutputStream;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.IndexedColors;
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 TestClass {
public static void main(String[] args){
Workbook wb = new XSSFWorkbook();
Sheet sheet = wb.createSheet("coverageData");
int rownum = 0,cellnum=0;
Row row1 = sheet.createRow((short)rownum++);
//Set Color style start
CellStyle row1style = wb.createCellStyle();
row1style.setFillBackgroundColor(IndexedColors.LIGHT_GREEN.getIndex());
row1style.setFillPattern(CellStyle.BIG_SPOTS);
//Set Color style end
Cell cell10 = row1.createCell((short)cellnum++);
cell10.setCellStyle(row1style);
cell10.setCellValue("cell data");
try{
//Write the workbook in file system
FileOutputStream out = new FileOutputStream(new File("Spreadsheet.xlsx"));
wb.write(out);
out.close();
System.out.println("Spreadsheet.xlsx written successfully on disk.");
} catch (Exception e) { e.printStackTrace(); }
}
}
You need to set the color like this:
final XSSFCellStyle description = (XSSFCellStyle) workbook.createCellStyle();
description.setFillForegroundColor( yourColor );
description.setFillPattern( FillPatternType.SOLID_FOREGROUND );
This is, you need a fill pattern

Reading content from an Excel file

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

Categories