I wrote a code in Java to read an excel(.xlsx) file and save the same file with different name. The code is working fine but the size of original and generated excel is different. I got the problem as on saving the original, the excel is losing some data. But when I opened the generated file through Ms Excel it get restored to its original size. What could I use in my code so that no data is lost?
Code is:
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
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;
public class MyExcel {
public static void main(String[] args) throws IOException {
FileInputStream fis = new FileInputStream(new File("D:\\test.xlsx"));
XSSFWorkbook workbook = new XSSFWorkbook(fis);
XSSFSheet sheet = workbook.getSheetAt(1);
XSSFRow row1 = sheet.getRow(1);
XSSFCell cell1 = row1.getCell(1);
cell1.setCellValue("Mahesh");
XSSFRow row2 = sheet.getRow(2);
XSSFCell cell2 = row2.getCell(1);
cell2.setCellValue("Ramesh");
OutputStream fos = new FileOutputStream(new File("D:\\test_new.xlsx"));
workbook.write(fos);
fis.close();
fos.close();
System.out.println("Done");
}
}
The image shows the different in size of two files.
Related
I would like to retain the data in the existing excel file.After that then try to output something to this file.But I got an error.
I have been searching the answer but none of it matches my situation.
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Iterator;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
public class TryHyperlink{
public static void main(String[] args) {
File in = new File("D:\\Test1.xlsx");
try {
FileInputStream fin = new FileInputStream(in);
//read input
XSSFWorkbook wb;
wb = new XSSFWorkbook(fin);//The StackTrace shows that the error is here
XSSFSheet sheet = wb.getSheetAt(0);
Iterator<Row> rowItr = sheet.iterator();
//iterate through every row and cell
while(rowItr.hasNext()) {
Row row = rowItr.next();
Iterator<Cell> cellItr = row.iterator();
while(cellItr.hasNext()) {
Cell cell = cellItr.next();
}
}
fin.close();
//close the input stream so output stream can write
FileOutputStream fout = new FileOutputStream(in);
wb.write(fout);
} catch (IOException e) {
e.printStackTrace();
}
}
}
Edit:
After referring to Alex Richter Comment,I closed the output stream and the error gone. Now Another error come out:
WARNING: An illegal reflective access operation has occurred and so on(the link of the photo at the bottom of the question)
package net.excel;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Iterator;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
public class TryHyperlink{
public static void main(String[] args) {
File in = new File("D:\\penril.xlsx");
try {
FileInputStream fin = new FileInputStream(in);
XSSFWorkbook wb;
wb = new XSSFWorkbook(fin);
XSSFSheet sheet = wb.getSheetAt(0);
Iterator<Row> rowItr = sheet.iterator();
while(rowItr.hasNext()) {
Row row = rowItr.next();
Iterator<Cell> cellItr = row.iterator();
while(cellItr.hasNext()) {
Cell cell = cellItr.next();
cell.setCellValue(cell.getNumericCellValue());
}
}
fin.close();
FileOutputStream fout = new FileOutputStream(in);
wb.write(fout);
fout.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
```package net.excel;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Iterator;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
public class TryHyperlink{
public static void main(String[] args) {
File in = new File("D:\\penril.xlsx");
try {
FileInputStream fin = new FileInputStream(in);
XSSFWorkbook wb;
wb = new XSSFWorkbook(fin);
XSSFSheet sheet = wb.getSheetAt(0);
Iterator<Row> rowItr = sheet.iterator();
while(rowItr.hasNext()) {
Row row = rowItr.next();
Iterator<Cell> cellItr = row.iterator();
while(cellItr.hasNext()) {
Cell cell = cellItr.next();
cell.setCellValue(cell.getNumericCellValue());
}
}
fin.close();
FileOutputStream fout = new FileOutputStream(in);
wb.write(fout);
fout.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}```
[1]: https://i.stack.imgur.com/WTOn3.png
when the first column of the row is empty, excel write is not working. second and remaining column values also checking if the first column is filled or empty. any reason behind or how to over come this issue.
Test1: first column empty
package DataDrivern;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class ExcelWrite {
public static void main(String[] args) throws Exception
{
File src=new File("D:\\Selenium Learning\\input.xlsx");
FileInputStream fis=new FileInputStream(src);
XSSFWorkbook wb=new XSSFWorkbook(fis);
XSSFSheet ws=wb.getSheet("Sheet2");
System.out.println("excel read successfully");
ws.getRow(0).createCell(0).setCellValue("test");
FileOutputStream fos=new FileOutputStream(src);
wb.write(fos);
wb.close();
}
}
Output
excel read successfully
Exception in thread "main" java.lang.NullPointerException
at DataDrivern.ExcelWrite.main(ExcelWrite.java:22)
Test 2: first column not empty
before running code:
package DataDrivern;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class ExcelWrite {
public static void main(String[] args) throws Exception
{
File src=new File("D:\\Selenium Learning\\input.xlsx");
FileInputStream fis=new FileInputStream(src);
XSSFWorkbook wb=new XSSFWorkbook(fis);
XSSFSheet ws=wb.getSheet("Sheet2");
System.out.println("excel read successfully");
ws.getRow(0).createCell(0).setCellValue("test");
FileOutputStream fos=new FileOutputStream(src);
wb.write(fos);
wb.close();
}
}
after running code
Wrong usage of method instead of getRow you have to use createRow
ws.createRow(0).createCell(0).setCellValue("test");
This will write string "test" to row 0 and column 0
I have downloaded Apache Poi Jar but when I write the following code (which a youtube instructor ran with ease), it does not give me any output excel file. What am I doing wrong here? I hover my mouse over HSSFWorkbook eclipse tells me
org.apache.poi.hssf.usermodel.HSSFWorkbook Note: This element neither has attached source nor attached Javadoc and hence no Javadoc could be found.
The code.
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
public class WriteExcel {
public static void main(String[] args) throws FileNotFoundException, IOException {
HSSFWorkbook workbook= new HSSFWorkbook();
HSSFSheet sheet= workbook.createSheet("FirstExcelSheet");
HSSFRow row= sheet.createRow(0);
HSSFCell cell= row.createCell(0);
cell.setCellValue("1,Cell");
workbook.write(new FileOutputStream("excel.xls"));
workbook.close();
workbook.getFirstVisibleTab();
}
}
the first thing use XSSFWorkbook instead of HSSFWorkbook take a look at this link,
and the second thing you have to out stream this file add this line
try (FileOutputStream outputStream = new FileOutputStream("slsx.xlsx")) {
workbook.write(outputStream);
so your code should be like this
public static void main(String[] args) throws Exception {
XSSFWorkbook workbook = new XSSFWorkbook();
XSSFSheet spreadsheet = workbook.createSheet("sheet1");
XSSFRow row = spreadsheet.createRow(1);
XSSFCell cell = row.createCell(1);
cell.setCellValue("test");
try (FileOutputStream out = new FileOutputStream(new File("Writesheet.xlsx"))) {
workbook.write(out);
}
System.out.println("Writesheet.xlsx written successfully");
}
you should add your path in this line like:
workbook.write(new FileOutputStream("{yourpath}/excel.xls"));
it works..
First of all, I apologize for my bad english, it's not my primary language.
I'm having a problem with my code when it's reading an excel file (xlsx). The first time I used my code it works perfectly, but now I can't use it.
The excel content is printed on the console, but next to it there is a NullPointerException that is caused by the for loop marked with the (-->>>).
If anyone can help me, I would be very thankful, this code has given me several headaches.
package modleerjava;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class leerExcel {
public void readExcel(String rutaFile,String nombreFile, String sheetName )
throws IOException {
File file = new File ("C:/Users/Pablo/Desktop/prueba.xlsx");
FileInputStream inputStream = new FileInputStream(file);
XSSFWorkbook excelWorkbook ;
excelWorkbook = new XSSFWorkbook(inputStream);
Sheet excelSheet = excelWorkbook.getSheet(sheetName);
int filasCount = excelSheet.getLastRowNum()-excelSheet.getFirstRowNum();
for (int i=0; i< filasCount+1 ; i++) {
Row filas;
filas = excelSheet.getRow(i);
-->>> for (int j=0 ; j < filas.getLastCellNum(); j++) {
System.out.print(filas.getCell(j).getStringCellValue()+"|| ");
}
System.out.println();
}
}
}
Your sheet probably has missing rows or cells. The best way to iterate over a spreadsheet in poi is using the for each syntax like this.
package modleerjava;
import java.io.File;
import java.io.IOException;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
public class leerExcel {
public void readExcel(String rutaFile,String nombreFile, String sheetName )
throws IOException {
Workbook wb = WorkbookFactory.create(new File("C:/Users/Pablo/Desktop/prueba.xlsx"));
Sheet excelSheet = wb.getSheet(sheetName);
for (Row filas: excelSheet) {
for (Cell cell: filas) {
System.out.print(cell.getStringCellValue()+"|| ");
}
System.out.println();
}
}
}
Many simple questions can be answered by looking at the quick guide on the poi website.
By seeing the code, I think that NullPointerException will occur if the first row in the excel sheet is blank. So, I have created an Iterator on rows.
Try the below code:
package modleerjava;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class leerExcel {
public void readExcel(String rutaFile,String nombreFile, String sheetName )
throws IOException {
File file = new File ("C:/Users/Pablo/Desktop/prueba.xlsx");
FileInputStream inputStream = new FileInputStream(file);
XSSFWorkbook excelWorkbook ;
excelWorkbook = new XSSFWorkbook(inputStream);
inputStream.close();
Sheet excelSheet = excelWorkbook.getSheet(sheetName);
//int filasCount = excelSheet.getLastRowNum()-excelSheet.getFirstRowNum();
Iterator<Row> row = excelSheet.rowIterator();
while(row.hasNext()) {
Row filas;
filas = row.next();
for (int j=0 ; j < filas.getLastCellNum(); j++) {
System.out.print(filas.getCell(j).getStringCellValue()+"|| ");
}
System.out.println();
}
}
}
I am unable to write data into second sheet of excel using Apache POI in selenium web driver. It showing up a Null point exception error. However, i am able to write data in first sheet. Below is my code,
package RWExcel;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class WriteExcel {
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
File src = new File("D:/selenium/Test/ReadExcel.xlsx");
FileInputStream fis = new FileInputStream(src);
XSSFWorkbook wbook = new XSSFWorkbook(fis);
XSSFSheet sheet0 = wbook.getSheetAt(1);
sheet0.getRow(0).createCell(2).setCellValue("Pass");
sheet0.getRow(1).createCell(2).setCellValue("Fail");
FileOutputStream output = new FileOutputStream(src);
wbook.write(output);
wbook.close();
}
}