Reading and writing excel sheet using java apache POI - java

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

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)

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

Below given Selenium Code for Excel is not Working

package poi;
import java.io.*;
import java.io.File;
import java.io.FileInputStream;
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;
class Demo
{
public static void main(String a[]) throws IOException
{
String temp ="C:/santosh_chikne/Selenium Script/test.xlsx";
FileInputStream fs = new FileInputStream(temp);
Workbook wb = Workbook.getWorkbook(fs);
}
}
try as following:
String temp ="C:/santosh_chikne/Selenium Script/test.xlsx";
FileInputStream fs = new FileInputStream(temp);
HSSFWorkbook wb = new HSSFWorkbook(fs);
HSSFSheet sh = wb.getSheet("Sheet1");
Row row = sh.getRow(1);
Cell cell = row.getCell(1);
String valueFromXl = cell.getStringCellValue();

fetch image from excel using java

I am working with Apache POI. I am able to read data from excel, but unable to read image from the excel. How to read image from excel.
Instead of puzzling around let's have a complete example.
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.PictureData;
import java.io.InputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.List;
import java.util.Iterator;
class ReadExcelImages {
public static void main(String[] args) throws Exception{
InputStream inp = new FileInputStream("test.xls");
//InputStream inp = new FileInputStream("test.xlsx");
Workbook workbook = WorkbookFactory.create(inp);
List lst = workbook.getAllPictures();
int i = 1;
for (Iterator it = lst.iterator(); it.hasNext(); ) {
PictureData pict = (PictureData)it.next();
String ext = pict.suggestFileExtension();
byte[] data = pict.getData();
if (ext.equals("png")){
FileOutputStream out = new FileOutputStream("pict" + i++ + ".png");
out.write(data);
out.close();
} else if (ext.equals("jpeg")) {
FileOutputStream out = new FileOutputStream("pict" + i++ + ".jpeg");
out.write(data);
out.close();
}
}
}
}
Works for me with HSSF (*.xls) as well as with XSSF (*.xlsx).

coloring of column name in excel sheet using java

I am storing some files in excel sheet using java. I need to color the name of column. how can i process this. I want to color different columns with different colors.
resultMap.put("Question", question);
resultMap.put("Option a", options.get(0));
resultMap.put("Option b", options.get(1));
resultMap.put("Option c", options.get(2));
resultMap.put("Option d", options.get(3));
Question, option a, option b, option c, option d are five columns that I am storing. excel sheet is in .xlsx format
XSSFWorkbook myWorkBook = new XSSFWorkbook ();
// Return first sheet from the XLSX workbook
XSSFSheet mySheet = myWorkBook.createSheet(sheetName);
XSSFRow row = mySheet.createRow(0);
row.createCell(7).setCellValue("Question");
row.createCell(8).setCellValue("Option a");
row.createCell(9).setCellValue("Option b");
row.createCell(10).setCellValue("Option c");
row.createCell(11).setCellValue("Option d");
FileOutputStream fileOut = new FileOutputStream(outputFilePath);
myWorkBook.write(fileOut);
fileOut.close();
myWorkBook.close();
Assuming you are using Apache POI, you can do something like the below, or check this page for additional info and examples: http://poi.apache.org/spreadsheet/quick-guide.html#FillsAndFrills
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Font;
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 ExCell {
/**
* #param args
* #throws IOException
*/
public static void main(String[] args) throws IOException {
Workbook myWorkBook = new XSSFWorkbook();
Sheet mySheet = myWorkBook.createSheet("Main");
Row row = mySheet.createRow(0);
Font font = myWorkBook.createFont();
font.setColor(IndexedColors.BLUE.getIndex());
CellStyle style = myWorkBook.createCellStyle();
style.setFillPattern(CellStyle.SOLID_FOREGROUND);
style.setFillForegroundColor(IndexedColors.YELLOW.getIndex());
style.setFont(font);
Cell newCell = row.createCell(7);
newCell.setCellValue("Question");
newCell.setCellStyle(style);
row.createCell(8).setCellValue("Option a");
row.createCell(9).setCellValue("Option b");
row.createCell(10).setCellValue("Option c");
row.createCell(11).setCellValue("Option d");
FileOutputStream fileOut = new FileOutputStream("Sandeep.xlsx");
myWorkBook.write(fileOut);
fileOut.close();
myWorkBook.close();
System.out.println("Done");
}
}

Categories