I have a requirement to sum up the Excel Column(1) values based on the Row data found.
My excel file is as follows:
column(0) column(1)
Row[0] ECIN - INPUT VALUE (ADD) NetTradeAllowanceAmount = -600.00
Row[1] ECIN - INPUT VALUE (ADD) CashDownPayment = 300.00
Row[2] ECIN - INPUT VALUE (ADD) OtherDownPaymentAmount = PATH DOES NOT EXIST
Row[3] ECIN - INPUT VALUE (ADD) CashDownPayment = 400.00
Row[4] ECIN - INPUT VALUE (SUB) OtherDownPaymentAmount = PATH DOES NOT EXIST
Row[5] ECIN - INPUT VALUE (SUB) ManufacturerRebateAmount = 500.00
Row[6] ECIN - INPUT VALUE (SUB) DeferredDownPaymentAmount = -700.00
Row[7] ECIN - INPUT VALUE (SUB) DeferredDownPaymentAmount = 900.00
First I need to look at Column(0), all the rows:
1.add the column(1) values having rows (ADD) data. (eg: SUM= 300.00 + 400.00 - 600.00 = 700.00 - 600.00 = 100.00)
2.add the column(1) values having rows (SUB) data. (eg: SUM=500.00 - 700.00 + 900.00 = 1400.00 - 700.00 = 700.00)
3.then subtract above two SUMs. (eg: 100.00 - 700.00 = 600.00)
I should save this result in some variable and record this value in some other cell.
Note: Program should not consider value = PATH DOES NOT EXIST, even though row is having the data (SUB / ADD).
To some extent I have written the code. it is as follows:
import java.io.*;
import java.util.*;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.usermodel.*;
public class Hai
{
public static void main(String[] args)
{
try
{
FileInputStream file = new FileInputStream(new File("C:/Users/Excel.xls"));
HSSFWorkbook workbook = new HSSFWorkbook(file);
HSSFSheet sheet = workbook.getSheetAt(5);
Iterator<Row> rowIterator = sheet.iterator();
while(rowIterator.hasNext())
{
Row row = rowIterator.next();
Iterator<Cell> cellIterator = row.cellIterator();
while(cellIterator.hasNext())
{
Cell cell = cellIterator.next();
String Tag=cell.getStringCellValue().toString();
cell = row.getCell(0+1);
if(cell !=null)
if(Tag.contains("ADD"))
{
String Tag1=cell.getStringCellValue().toString();
String[] s= Tag1.split("=");
//System.out.println(s[1]);
if(!s[1].contains("PATH DOES NOT EXIST"))
{
System.out.println(s[1].trim());
}
}
else if(Tag.contains("SUB"))
{
String Tag1=cell.getStringCellValue().toString();
String[] s= Tag1.split("=");
if(!s[1].contains("PATH DOES NOT EXIST"))
{
System.out.println(s[1].trim());
}
}
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Output I am getting is as follows :
-600.00
300.00
400.00
500.00
-700.00
900.00
The above values are in string format, I want to sum up these values. Please help me!
I have converted above values to Flaot like this:
Float foo = Float.parseFloat(s[1].trim());
output I got is:
-600.0
300.0
400.0
I want to get two decimal digits and sumup these values. I could not able to sumup the values.
Is it like this
import java.io.*;
import java.util.*;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.usermodel.*;
public class Hai
{
public static double getSubstraction(double summ, String your)
{
if (your.contains("-"))
{
return main + Double.parseDouble(your.replace("-", ""));
}
else if (your.contains("+"))
{
return main - Double.parseDouble(your.replace("+", ""));
}
else
{
return main - Double.parseDouble(your);
}
}
public static double getSumm(double sub, String your)
{
if (your.contains("-"))
{
return main - Double.parseDouble(your.replace("-", ""));
}
else if (your.contains("+"))
{
return main + Double.parseDouble(your.replace("+", ""));
}
else
{
return main + Double.parseDouble(your);
}
}
public static void main(String[] args)
{
try
{
double summ, sub;
FileInputStream file = new FileInputStream(new File("C:/Users/Pradeep.HALCYONTEKDC/Desktop/19-04-2013.xls"));
HSSFWorkbook workbook = new HSSFWorkbook(file);
HSSFSheet sheet = workbook.getSheetAt(5);
Iterator<Row> rowIterator = sheet.iterator();
while(rowIterator.hasNext())
{
Row row = rowIterator.next();
Iterator<Cell> cellIterator = row.cellIterator();
while(cellIterator.hasNext())
{
Cell cell = cellIterator.next();
String Tag=cell.getStringCellValue().toString();
cell = row.getCell(0+1);
if(cell !=null)
if(Tag.contains("ADD"))
{
String Tag1=cell.getStringCellValue().toString();
String[] s= Tag1.split("=");
//System.out.println(s[1]);
if(!s[1].contains("PATH DOES NOT EXIST"))
{
getSumm() ;
Float foo = Float.parseFloat(s[1].trim());
System.out.println("1---- "+foo);
for(int i=0; i<5;i++)
{
foo+=foo;
//System.out.println(foo);
}
}
}
else if(Tag.contains("SUB"))
{
String Tag1=cell.getStringCellValue().toString();
String[] s= Tag1.split("=");
if(!s[1].contains("PATH DOES NOT EXIST"))
{
getSubstraction();
System.out.println(s[1].trim());
}
}
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Help me out in this.