Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package readfromexcel;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.io.IOException;
- import java.util.ArrayList;
- import java.util.Collections;
- import java.util.Iterator;
- import org.apache.poi.hssf.usermodel.HSSFSheet;
- 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;
- import org.apache.poi.ss.usermodel.*;
- public class SumIncomes {
- static public ArrayList<String> groupCities(ArrayList<String> pesho){
- ArrayList<String> newList = new ArrayList<String>();
- for (String string : pesho) {
- if (newList.indexOf(string) == -1) {
- newList.add(string);
- }
- }
- Collections.sort(newList);
- return newList;
- }
- static public double sumArray(double[] arr)
- {
- double total = 0.0;
- for (double d : arr) {
- total += d;
- }
- return total;
- }
- public static void main(String[] args)throws IOException {
- try {
- FileInputStream file = new FileInputStream(new File("C:\\Users\\Vera\\workspace\\HomeWorkLoopsMethodsClasses\\Incomes-Report.xlsx"));
- XSSFWorkbook newWorkbook = new XSSFWorkbook(file);
- XSSFSheet openSheet = newWorkbook.getSheetAt(0);
- Iterator<Row> rowIterator = openSheet.iterator();
- ArrayList<String> cities = new ArrayList<>();
- ArrayList<Double> incomes = new ArrayList<>();
- while (rowIterator.hasNext()) {
- Row currRow = rowIterator.next();
- Iterator<Cell> getCell = currRow.cellIterator();
- Cell currCell = getCell.next();
- if (!currCell.getStringCellValue().equals("Office")) {
- String office = currCell.getStringCellValue();
- cities.add(office);
- Cell cell = currRow.getCell(5);
- incomes.add(cell.getNumericCellValue());
- }
- }
- ArrayList<String> groupedCities = groupCities(cities);
- int i = 0;
- double[] groupedIncomes = new double[groupedCities.size()];
- for (String str : groupedCities) {
- double temp = 0.0;
- for (int j = 0; j < cities.size(); j++) {
- if (str.equals(cities.get(j))) {
- temp += incomes.get(j);
- }
- }
- groupedIncomes[i] = temp;
- i++;
- }
- for (int j = 0; j < groupedIncomes.length; j++) {
- System.out.printf("%1$s -----> %2$.2f %n", groupedCities.get(j), groupedIncomes[j]);
- }
- System.out.printf("Grand Total -----> %.2f", sumArray(groupedIncomes));
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement