How To handle Null Row using Apache POI? - java

I am using Apache POI to read xlsx file, it works well. I have question to you when row is found null, how I'm able to handle it? My file contain 500 row, but it show 105667 row, rest of row found null.
used code:
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.DateUtil;
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.ss.usermodel.WorkbookFactory;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
/**
*
* #author SAMEEK
*/
public class readXLSXFile {
public int getNumberOfColumn(String fileName, int sheetIndex) throws FileNotFoundException, IOException {
File inputFile = null;
FileInputStream fis = null;
XSSFWorkbook workbook = null;
XSSFSheet sheet = null;
XSSFRow row = null;
int lastRowNum = 0;
int lastCellNum = 0;
// Open the workbook
inputFile = new File(fileName);
fis = new FileInputStream(inputFile);
workbook = new XSSFWorkbook(fis);
sheet = workbook.getSheetAt(sheetIndex);
lastRowNum = sheet.getLastRowNum();
for (int i = 0; i < lastRowNum; i++) {
row = sheet.getRow(i);
if (row != null) {
if (row.getLastCellNum() > lastCellNum) {
lastCellNum = row.getLastCellNum();
}
}
}
return lastCellNum;
}
public int getNumberOfRow(String fileName, int sheetIndex) throws FileNotFoundException, IOException {
File inputFile = null;
FileInputStream fis = null;
XSSFWorkbook workbook = null;
XSSFSheet sheet = null;
int lastRowNum = 0;
// Open the workbook
inputFile = new File(fileName);
fis = new FileInputStream(inputFile);
workbook = new XSSFWorkbook(fis);
sheet = workbook.getSheetAt(sheetIndex);
lastRowNum = sheet.getLastRowNum();
return lastRowNum;
}
public String[] getSheetName(String fileName) throws FileNotFoundException, IOException {
int totalsheet = 0;
int i = 0;
String[] sheetName = null;
File inputFile = null;
FileInputStream fis = null;
XSSFWorkbook workbook = null;
// Open the workbook
inputFile = new File(fileName);
fis = new FileInputStream(inputFile);
workbook = new XSSFWorkbook(fis);
totalsheet = workbook.getNumberOfSheets();
sheetName = new String[totalsheet];
while (i < totalsheet) {
sheetName[i] = workbook.getSheetName(i);
i++;
}
return sheetName;
}
public int getNumberOfSheet(String fileName) throws FileNotFoundException, IOException {
int totalsheet = 0;
File inputFile = null;
FileInputStream fis = null;
XSSFWorkbook workbook = null;
XSSFSheet sheet = null;
int lastRowNum = 0;
// Open the workbook
inputFile = new File(fileName);
fis = new FileInputStream(inputFile);
workbook = new XSSFWorkbook(fis);
totalsheet = workbook.getNumberOfSheets();
return totalsheet;
}
public String[][] getSheetData(String fileName, int sheetIndex) throws FileNotFoundException, IOException, InvalidFormatException {
String[][] data = null;
int i = 0;
int j = 0;Cell cell=null;
long emptyrowcount = 0;
InputStream inputStream = new FileInputStream(
fileName);
// Create a workbook object.
Workbook wb = WorkbookFactory.create(inputStream);
wb.setMissingCellPolicy(Row.CREATE_NULL_AS_BLANK);
Sheet sheet = wb.getSheetAt(sheetIndex);
// Iterate over all the row and cells
int noOfColumns = getNumberOfColumn(fileName, sheetIndex);
System.out.println("noOfColumns::" + noOfColumns);
int noOfRows = getNumberOfRow(fileName, sheetIndex) + 1;
System.out.println("noOfRows::" + noOfRows);
data = new String[noOfRows][noOfColumns];
for (int k = 0; k < noOfRows; k++) {
Row row = sheet.getRow(k);
if (row == null) {
} else {
j = 0;
for (int l = 0; l < noOfColumns; l++) {
// Cell cell = cit.next();
cell = row.getCell(j);
if (cell.getCellType() == cell.CELL_TYPE_BLANK) {
cell = row.getCell(j, Row.CREATE_NULL_AS_BLANK);
}
data[i][j] = getCellValueAsString(cell);
j++;
}
i++;
}
}
return data;
}
/**
* This method for the type of data in the cell, extracts the data and
* returns it as a string.
*/
public static String getCellValueAsString(Cell cell) {
String strCellValue = null;
if (cell != null) {
switch (cell.getCellType()) {
case Cell.CELL_TYPE_STRING:
strCellValue = cell.toString();
break;
case Cell.CELL_TYPE_NUMERIC:
if (DateUtil.isCellDateFormatted(cell)) {
SimpleDateFormat dateFormat = new SimpleDateFormat(
"dd/MM/yyyy");
strCellValue = dateFormat.format(cell.getDateCellValue());
} else {
Double value = cell.getNumericCellValue();
Long longValue = value.longValue();
strCellValue = new String(longValue.toString());
}
break;
case Cell.CELL_TYPE_BOOLEAN:
strCellValue = new String(new Boolean(
cell.getBooleanCellValue()).toString());
break;
case Cell.CELL_TYPE_BLANK:
strCellValue = "";
break;
}
}
return strCellValue;
}
public static void main(String s[]) {
try {
readXLSXFile readXLSxFile = new readXLSXFile();
String[][] sheetData = readXLSxFile.getSheetData("F:/work.xlsx", 0);
int columnLength = 0;
columnLength = readXLSxFile.getNumberOfColumn("F:/work.xlsx", 0);
int rowLength = 0;
rowLength = readXLSxFile.getNumberOfRow("F:/work.xlsx", 0);
int h = 0;
int j = 0;
while (j < rowLength) {
h = 0;
while (h < columnLength) {
System.out.print("\t " + sheetData[j][h]);
h++;
}
System.out.println("");
j++;
}
} catch (InvalidFormatException ex) {
Logger.getLogger(readXLSFile.class.getName()).log(Level.SEVERE, null, ex);
} catch (FileNotFoundException ex) {
Logger.getLogger(readXLSFile.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(readXLSFile.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
Please help me how to handle null row in excel sheet?

If you fetch a row, and get back null, then that means there is no data stored in the file for that row - it's completely blank.
POI by default gives you what's in the file. With Cells, you can set a MissingCellPolicy to control how missing and blank cells are handled. There's some examples of using this in the Apache POI docs. With rows, they're either there or not, so you need to check for nulls when fetching a row.

In case your .xlsx file contains any of the formatting for the blank cells, the poi reading is not treating it as null, however if you want to print it's value, it will give NullPointerException. To understand it I have created a sheet and mark the first columns boundary with to "All Border" for 10 rows, but not given any value to it. now applying following piece of code is showing output sheet.lastRowNum() as 10, while the RowCountWithNullValue is 990, and RowCountWithoutNullValue is 10. However the sheet is completely blank. If you uncomment the print statement, it will show NullPointerException.
public class Rough {
public static void main(String args[]) throws IOException{
public static void main(String args[]) throws IOException{
FileInputStream fin = new FileInputStream(AddressOfxlsxFile);
XSSFWorkbook wb = new XSSFWorkbook(fin);
XSSFSheet sheet = wb.getSheetAt(1);
int RowCountWithNullValue=0, RowCountWithoutNullValue=0;
for (int i=0;i<1000;i++){
if (sheet.getRow(i)==null)
RowCountWithNullValue++;
else{
RowCountWithoutNullValue++;
// System.out.println(sheet.getRow(0).getCell(0));
}
}
System.out.println(sheet.getLastRowNum());
System.out.println(RowCountWithNullValue+","+RowCountWithoutNullValue);
}
}
I am not sure if the same is happening on your end or not, but if you are saying your file contain 500 row, but it show 105667 row, this may be one of the cause.

Related

Read Excel horizontally with TestNG

I can read an Excel sheet vertically and the pass the values to TestNG dataprovide.
This makes TestNG executes each row.
public Object[][] getData(ITestContext context) throws IOException {
Object[][] obj = null;
try {
File file = new File(context.getCurrentXmlTest().getParameter("resource"));
FileInputStream fis = new FileInputStream(file);
XSSFWorkbook wb = new XSSFWorkbook(fis);
XSSFSheet sheet = wb.getSheetAt(0);
wb.close();
int rowCount= sheet.getLastRowNum();
int colCount = sheet.getRow(0).getLastCellNum();
obj = new Object[rowCount][1];
DataFormatter df = new DataFormatter();
for (int i = 0; i < rowCount; i++) {
Map<String, String> datamap= new HashMap<String, String>();
for (int j = 0; j < colCount; j++) {
String a = df.formatCellValue(sheet.getRow(0).getCell(j));
String b = df.formatCellValue(sheet.getRow(i+1).getCell(j));
if(!b.equals(""))
datamap.put(a, b);
if(b.equals("[PWD]")) {
b=PropertiesFile.getProperty("psw");
byte[] byteArray = Base64.decodeBase64(b.getBytes());
String decodedString = new String(byteArray);
b=decodedString;
datamap.put(a, b);
}
}
obj[i][0] =datamap;
}
}catch(Exception e){
e.printStackTrace();
}
return obj;
}
I'd like to read it horizontally, so that TestNG executes each column (the first column would be the keys column), how can i do?
Thanks
You can transpose your sheet and work with former columns which are now the rows: How to transpose sheet with POI SS/XSSF?
Or just change your loop nesting so that you go by columns in the outer loops. Not by rows.
I think i solved it
public Object[][] getData(ITestContext context) throws IOException {
Object[][] obj = null;
try {
File file = new File(context.getCurrentXmlTest().getParameter("resource"));
FileInputStream fis = new FileInputStream(file);
XSSFWorkbook wb = new XSSFWorkbook(fis);
XSSFSheet sheet = wb.getSheetAt(0);
wb.close();
int rowCount= sheet.getLastRowNum();
int colCount = sheet.getRow(0).getLastCellNum();
obj = new Object[colCount-1][1];
DataFormatter df = new DataFormatter();
for (int i = 0; i < colCount-1; i++) {
Map<String, String> datamap= new HashMap<String, String>();
for (int j = 0; j < rowCount+1; j++) {
String a = df.formatCellValue(sheet.getRow(j).getCell(0));
String b = df.formatCellValue(sheet.getRow(j).getCell(i+1));
System.out.println(a+ " "+b);
if(!b.equals(""))
datamap.put(a, b);
if(b.equals("[PWD]")) {
b=PropertiesFile.getProperty("psw");
byte[] byteArray = Base64.decodeBase64(b.getBytes());
String decodedString = new String(byteArray);
b=decodedString;
datamap.put(a, b);
}
}
obj[i][0] =datamap;
}
}catch(Exception e){
e.printStackTrace();
}
return obj;
}

How to Delete or Remove empty rows in Excel sheet using java

Input Excel file.....
25010082512 25002207512 1044 1044 NGN NGN
36620841728 36617009228 1066 1066 NGN NGN
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
public class RemoveEmptyCellInExcel {
//shifting empty columns
public static void shift(File f){
File F=f;
HSSFWorkbook workbook = null;
HSSFSheet sheet=null;
Boolean isRowEmpty=false;
try{
FileInputStream is=new FileInputStream(F);
workbook= new HSSFWorkbook(is);
sheet = workbook.getSheetAt(0);
//sheet.setDisplayGridlines(false);
for(int i = 3; i < sheet.getLastRowNum(); i++){
if(sheet.getRow(i)==null){
sheet.shiftRows(i + 1, sheet.getLastRowNum(), -1);
i--;
continue;
}
for(int j=0; j<sheet.getRow(i).getLastCellNum();j++){
if(sheet.getRow(i).getCell(j).toString().trim().equals(""))
{
isRowEmpty=true;
}else {
isRowEmpty=false;
break;
}
}
if(isRowEmpty==true){
sheet.shiftRows(i+ 1, sheet.getLastRowNum(), -1);
i--;
}
}
//Writing output to the same file.
FileOutputStream fileOut = new FileOutputStream("--------");
workbook.write(fileOut);
fileOut.close();
System.out.println("Successfully wrote the content in the file");
}
catch(Exception e){
e.printStackTrace();
}
}
public static void main(String args[]) {
//Input file path
File f=new File("------------");
RemoveEmptyCellInExcel.shift(f);
}
}
I need following output.....
25010082512 25002207512 1044 1044 NGN NGN
36620841728 36617009228 1066 1066 NGN NGN
your code is over complicated try to simplifie try using apach csv :
public static void shift(File file){
final FileReader fr = new FileReader(file.getPath());
final File out = new File(path);
try(CSVPrinter printer = new CSVPrinter(out, CSVFormat.DEFAULT
.withDelimiter(' ')) {
final Iterable<CSVRecord> records =
CSVFormat.DEFAULT.withDelimiter(' ').parse(fr);
for (final CSVRecord record : records) {
printer.printRecord(record.get(0),record.get(1), ...,
record.get(record.size()-1));
}
Try to use this code:
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Iterator;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
public class RemoveEmptyCellInExcel {
//shifting empty columns
public static void shift(File f){
HSSFWorkbook workbook;
HSSFSheet sheet;
int firstColumn;
int endColumn;
boolean isRowEmpty = true;
try{
FileInputStream is=new FileInputStream(f);
workbook= new HSSFWorkbook(is);
sheet = workbook.getSheetAt(0);
//sheet.setDisplayGridlines(false);
//block to set column bounds
Iterator<Row> iter = sheet.rowIterator();
firstColumn = (iter.hasNext() ? Integer.MAX_VALUE : 0);
endColumn = 0;
while (iter.hasNext()) {
Row row = iter.next();
short firstCell = row.getFirstCellNum();
if (firstCell >= 0) {
firstColumn = Math.min(firstColumn, firstCell);
endColumn = Math.max(endColumn, row.getLastCellNum());
}
}
// main logic block
for (int i = 0; i< sheet.getLastRowNum(); i++) {
if (sheet.getRow(i) != null) {
isRowEmpty = true;
Row row = sheet.getRow(i);
for (int j = firstColumn; j < endColumn; j++) {
if (j >= row.getFirstCellNum() && j < row.getLastCellNum()) {
Cell cell = row.getCell(j);
if (cell != null) {
if (!cell.getStringCellValue().equals("")) {
isRowEmpty = false;
break;
}
}
}
}
//if empty
if (isRowEmpty) {
System.out.println("Found empty row on: " + row.getRowNum());
sheet.shiftRows(row.getRowNum() + 1, sheet.getLastRowNum(), -1);
i--;
}
}
// if row is null
else{
sheet.shiftRows(i + 1, sheet.getLastRowNum(), -1);
i--;
}
}
//Writing output to the same file.
FileOutputStream fileOut = new FileOutputStream("Test.xls");
workbook.write(fileOut);
fileOut.close();
System.out.println("Successfully wrote the content in the file");
}
catch(Exception e){
e.printStackTrace();
}
}
public static void main(String[] args) {
//Input file path
File f=new File("Test.xls");
RemoveEmptyCellInExcel.shift(f);
}
}

Writing to Excel with Java

I've searched around forums without prevail, so here I am.
I'm attempting to implement a method which writes to Excel, but every time it just corrupts the file. Here's a snippet of my code:
public static XSSFSheet printToExcel (ArrayList<String> al, Workbook wb, XSSFSheet s, FileWriter fw) throws FileNotFoundException, IOException {
int numOfRows = s.getPhysicalNumberOfRows();
for (int i = 0; i <= numOfRows; i++ ) {
XSSFRow row = s.createRow(i);
XSSFCell cell = row.createCell(i);
String text = al.get(i);
cell.setCellValue(text);
fw.write(text);
fw.close();
}
return s;
}
Hopefully you all can help me. I'm mostly self-taught so any advice will be appreciated.
This is an example which I wrote in another context but it will help you to start
import org.apache.poi.common.usermodel.HyperlinkType;
import org.apache.poi.ss.usermodel.FillPatternType;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.Hyperlink;
import org.apache.poi.xssf.usermodel.*;
import java.awt.*;
import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class WriteExcelBasic {
public static void main(String[] args) throws IOException {
String excelFileName = "/Users/home/Test3.xlsx";
FileOutputStream fos = new FileOutputStream(excelFileName);
XSSFWorkbook wb = new XSSFWorkbook();
XSSFCellStyle style = wb.createCellStyle();
XSSFSheet sheet = wb.createSheet("sheet");
XSSFFont urlFont = wb.createFont();// changed lines
urlFont.setFontHeight((short)(9*20));
// urlFont.setUnderline((byte)10);
//urlFont.setBold(true);
urlFont.setFontName("Arial");
urlFont.setItalic(true);
urlFont.setColor(new XSSFColor(Color.BLUE)); // changed lines
style.setFont(urlFont);
for (int r = 0; r < 3; r++) {
XSSFRow row = sheet.createRow(r);
for (int c = 0; c < 3; c++) {
XSSFCell cell = row.createCell(c);
Hyperlink link = wb.getCreationHelper().createHyperlink(HyperlinkType.URL);
String ss = "http://news.google.com/news/headlines?ned=us&hl=en";
//String ss = "swasdqde";
link.setAddress(ss);
cell.setHyperlink(link);
cell.setCellValue(ss);
if(r == 1) {
System.out.println("In yellow");
style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
style.setFillForegroundColor(new XSSFColor(Color.YELLOW));
} else {
System.out.println("In red");
style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
style.setFillForegroundColor(new XSSFColor(Color.RED));
}
cell.setCellStyle(style);
}
}
try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
wb.write(baos);
byte[] myByteArray = baos.toByteArray();
fos.write(myByteArray);
fos.flush();
}
finally {
wb.close();
fos.close();
}
}
}

converting csv to excel through java

I have to convert CSV to XLS format through Java POI since I am doing some manipulations with XLS sheets through POI. Below is my code:
File file = new File("C:\\abc.csv");
FileInputStream fin = null;
fin = new FileInputStream(file);
HSSFWorkbook workbook = new HSSFWorkbook(fin);
HSSFSheet firstSheet1 = workbook.getSheetAt(0);
Now I want to write a fuctions, lets say method name is convertcsvtoexcel which will accept the file obj and in return it will be give me converted XLS file that file will be stored in my C: drive with the name abcout.xls and later on I will be passing it to workbook as shown. I have tried the following code. Please advise how I can custoise it to make it fittable for my piece of code.
ArrayList arList = null;
ArrayList al = null;
String fName = "test.csv";
String thisLine;
int count = 0;
FileInputStream file = null;
file = new FileInputStream(new File("C:\\abc.csv"));
//FileInputStream fis = new FileInputStream(file);
DataInputStream myInput = new DataInputStream(file);
int i = 0;
arList = new ArrayList();
while ((thisLine = myInput.readLine()) != null) {
al = new ArrayList();
String strar[] = thisLine.split(",");
for (int j = 0; j < strar.length; j++) {
al.add(strar[j]);
}
arList.add(al);
System.out.println();
i++;
}
try {
HSSFWorkbook hwb = new HSSFWorkbook();
HSSFSheet sheet = hwb.createSheet("new sheet");
for (int k = 0; k < arList.size(); k++) {
ArrayList ardata = (ArrayList) arList.get(k);
HSSFRow row = sheet.createRow((short) 0 + k);
for (int p = 0; p < ardata.size(); p++) {
HSSFCell cell = row.createCell((short) p);
String data = ardata.get(p).toString();
if (data.startsWith("=")) {
cell.setCellType(Cell.CELL_TYPE_STRING);
data = data.replaceAll("\"", "");
data = data.replaceAll("=", "");
cell.setCellValue(data);
} else if (data.startsWith("\"")) {
data = data.replaceAll("\"", "");
cell.setCellType(Cell.CELL_TYPE_STRING);
cell.setCellValue(data);
} else {
data = data.replaceAll("\"", "");
cell.setCellType(Cell.CELL_TYPE_NUMERIC);
cell.setCellValue(data);
}
//*/
// cell.setCellValue(ardata.get(p).toString());
}
System.out.println();
}
FileOutputStream fileOut = new FileOutputStream("C:\\abcout.xls");
hwb.write(fileOut);
fileOut.close();
System.out.println("Your excel file has been generated");
} catch (Exception ex) {
ex.printStackTrace();
} //main method end
public static void csvToXLSX() {
try {
String csvFileAddress = "test.csv"; //csv file address
String xlsxFileAddress = "test.xlsx"; //xlsx file address
XSSFWorkbook workBook = new XSSFWorkbook();
XSSFSheet sheet = workBook.createSheet("sheet1");
String currentLine=null;
int RowNum=0;
BufferedReader br = new BufferedReader(new FileReader(csvFileAddress));
while ((currentLine = br.readLine()) != null) {
String str[] = currentLine.split(",");
RowNum++;
XSSFRow currentRow=sheet.createRow(RowNum);
for(int i=0;i<str.length;i++){
currentRow.createCell(i).setCellValue(str[i]);
}
}
FileOutputStream fileOutputStream = new FileOutputStream(xlsxFileAddress);
workBook.write(fileOutputStream);
fileOutputStream.close();
System.out.println("Done");
} catch (Exception ex) {
System.out.println(ex.getMessage()+"Exception in try");
}
}

How to recover formula cell cache result from excel file

I'm trying to retrieve the cached result of a formula cell in excel, the entire column is comprised of formula cells and i want to store the cached results of the columns' cells in an arraylist, but i get the error.
Apologies, i pasted the wrong code earlier, its fixed now.
public static void main(String[] args) throws Exception {
String filename = "C:/Users/L30902/Desktop/eclipse folder/FeaturesTest/student.xlsx";
FileInputStream fis = null;
int cellvalue = 0;
try {
fis = new FileInputStream(filename);
XSSFWorkbook workbook = new XSSFWorkbook(fis);
XSSFSheet sheet = workbook.getSheetAt(0);
Iterator rowIter = sheet.rowIterator();
while (rowIter.hasNext()) {
XSSFRow myRow = (XSSFRow) rowIter.next();
Iterator cellIter = myRow.cellIterator();
Vector<String> cellStoreVector = new Vector<String>();
while (cellIter.hasNext()) {
XSSFCell myCell = (XSSFCell) cellIter.next();
try {
cellvalue = myCell.getCachedFormulaResultType();
} catch (Exception e) {
}
cellStoreVector.addElement(Integer.toString(cellvalue));
}
String secondcolumnValue = null;
int i = 0;
secondcolumnValue = cellStoreVector.get(i).toString();
insertQuery(secondcolumnValue);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fis != null) {
fis.close();
}
}
// showExelData(sheetData);
}
private static void insertQuery(String secondcolumnvalue) {
System.out.println(secondcolumnvalue);
}
I should return values like 500, 33 but i only return 0, 0

Categories