Note that there are some explanatory texts on larger screens.

plurals
  1. POsum up the Excel column values using Java
    primarykey
    data
    text
    <p>I have a requirement to sum up the Excel Column(1) values based on the Row data found.</p> <p>My excel file is as follows:</p> <pre><code> 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 </code></pre> <p>First I need to look at Column(0), all the rows: </p> <pre><code>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) </code></pre> <p>I should save this result in some variable and record this value in some other cell.</p> <p>Note: Program should not consider value = PATH DOES NOT EXIST, even though row is having the data (SUB / ADD).</p> <p>To some extent I have written the code. it is as follows:</p> <pre><code>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&lt;Row&gt; rowIterator = sheet.iterator(); while(rowIterator.hasNext()) { Row row = rowIterator.next(); Iterator&lt;Cell&gt; 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(); } } } </code></pre> <p>Output I am getting is as follows :</p> <p>-600.00<br> 300.00<br> 400.00<br> 500.00<br> -700.00<br> 900.00 </p> <p>The above values are in string format, I want to sum up these values. Please help me! </p> <p>I have converted above values to Flaot like this: </p> <pre><code>Float foo = Float.parseFloat(s[1].trim()); </code></pre> <p>output I got is:</p> <p>-600.0<br> 300.0<br> 400.0 </p> <p>I want to get two decimal digits and sumup these values. I could not able to sumup the values.</p> <p>Is it like this</p> <pre><code>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&lt;Row&gt; rowIterator = sheet.iterator(); while(rowIterator.hasNext()) { Row row = rowIterator.next(); Iterator&lt;Cell&gt; 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&lt;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(); } } } </code></pre> <p>Help me out in this.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload