Advertisement
Guest User

ExcelTable

a guest
May 18th, 2014
287
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.54 KB | None | 0 0
  1. package readfromexcel;
  2.  
  3. import java.io.File;
  4. import java.io.FileInputStream;
  5. import java.io.FileNotFoundException;
  6. import java.io.IOException;
  7. import java.util.ArrayList;
  8. import java.util.Collections;
  9. import java.util.Iterator;
  10.  
  11.  
  12. import org.apache.poi.hssf.usermodel.HSSFSheet;
  13. import org.apache.poi.ss.usermodel.Cell;
  14. import org.apache.poi.ss.usermodel.Row;
  15. import org.apache.poi.xssf.usermodel.XSSFSheet;
  16. import org.apache.poi.xssf.usermodel.XSSFWorkbook;
  17. import org.apache.poi.ss.usermodel.*;
  18.  
  19. public class SumIncomes {
  20.  
  21.     static public ArrayList<String> groupCities(ArrayList<String> pesho){
  22.         ArrayList<String> newList = new ArrayList<String>();
  23.        
  24.         for (String string : pesho) {
  25.             if (newList.indexOf(string) == -1) {
  26.                 newList.add(string);
  27.             }
  28.         }
  29.         Collections.sort(newList);
  30.         return newList;
  31.     }
  32.     static public double sumArray(double[] arr)
  33.     {
  34.         double total = 0.0;
  35.         for (double d : arr) {
  36.             total += d;
  37.         }
  38.         return total;
  39.     }
  40.     public static void main(String[] args)throws IOException {
  41.         try {
  42.         FileInputStream file = new FileInputStream(new File("C:\\Users\\Vera\\workspace\\HomeWorkLoopsMethodsClasses\\Incomes-Report.xlsx"));
  43.         XSSFWorkbook newWorkbook = new XSSFWorkbook(file);
  44.         XSSFSheet openSheet = newWorkbook.getSheetAt(0);
  45.         Iterator<Row> rowIterator = openSheet.iterator();
  46.         ArrayList<String> cities = new ArrayList<>();
  47.         ArrayList<Double> incomes = new ArrayList<>();
  48.         while (rowIterator.hasNext()) {
  49.             Row currRow = rowIterator.next();
  50.             Iterator<Cell> getCell = currRow.cellIterator();
  51.             Cell currCell = getCell.next();
  52.             if (!currCell.getStringCellValue().equals("Office")) {
  53.                 String office = currCell.getStringCellValue();
  54.                 cities.add(office);
  55.                 Cell cell = currRow.getCell(5);                
  56.                 incomes.add(cell.getNumericCellValue());
  57.                
  58.                
  59.             }
  60.         }
  61.        
  62.         ArrayList<String> groupedCities = groupCities(cities);
  63.         int i = 0;
  64.         double[] groupedIncomes = new double[groupedCities.size()];
  65.        
  66.        
  67.         for (String str : groupedCities) {
  68.             double temp = 0.0;
  69.             for (int j = 0; j < cities.size(); j++) {
  70.                 if (str.equals(cities.get(j))) {
  71.                     temp += incomes.get(j);
  72.                 }
  73.             }
  74.             groupedIncomes[i] = temp;
  75.             i++;
  76.         }
  77.        
  78.         for (int j = 0; j < groupedIncomes.length; j++) {
  79.             System.out.printf("%1$s -----> %2$.2f %n", groupedCities.get(j), groupedIncomes[j]);
  80.         }
  81.         System.out.printf("Grand Total -----> %.2f", sumArray(groupedIncomes));
  82.         } catch (FileNotFoundException e) {
  83.                 e.printStackTrace();
  84.          } catch (IOException e) {
  85.             e.printStackTrace();
  86.         }
  87.     }
  88.  
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement