Advertisement
DNNdrago

Excel

May 22nd, 2014
537
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.90 KB | None | 0 0
  1. import java.io.FileInputStream;
  2. import java.io.FileNotFoundException;
  3. import java.io.IOException;
  4. import java.io.InputStream;
  5. import java.text.DecimalFormat;
  6. import java.util.Locale;
  7. import java.util.Map.Entry;
  8. import java.util.SortedMap;
  9. import java.util.TreeMap;
  10.  
  11. import org.apache.poi.xssf.usermodel.XSSFCell;
  12. import org.apache.poi.xssf.usermodel.XSSFRow;
  13. import org.apache.poi.xssf.usermodel.XSSFSheet;
  14. import org.apache.poi.xssf.usermodel.XSSFWorkbook;
  15.  
  16.  
  17.  
  18. public class Excel {
  19.     public static void main(String args[]) {
  20.         Locale.setDefault(Locale.ROOT);
  21.         SortedMap<String, Double> report = new TreeMap<String, Double>();
  22.            
  23.         try {
  24.             InputStream file = new FileInputStream("Incomes-Report.xlsx");
  25.             try {
  26.                 XSSFWorkbook workbook = new XSSFWorkbook(file);
  27.                 XSSFSheet sheet = workbook.getSheetAt(0);
  28.                 int rows = sheet.getLastRowNum();
  29.                 int cols = sheet.getRow(0).getLastCellNum();
  30.  
  31.                 for (int i = 1; i <= rows; i++) {
  32.                     XSSFRow row = sheet.getRow(i);
  33.                     XSSFCell cell = row.getCell(0);
  34.                     String office = cell.toString();
  35.                     cell = row.getCell(5);
  36.                     Double totalIncome = cell.getNumericCellValue();
  37.                     if (report.containsKey(office)){
  38.                         report.put(office, report.get(office) + totalIncome);
  39.                     }
  40.                     else
  41.                         report.put(office, totalIncome);
  42.                 }
  43.             } catch (IOException e) {
  44.                 e.printStackTrace();
  45.             }
  46.         } catch (FileNotFoundException e) {
  47.             e.printStackTrace();
  48.         }
  49.  
  50.         DecimalFormat formatter = new DecimalFormat("#0.00");
  51.         double totalIncome = 0;
  52.        
  53.         for (Entry<String, Double> rep : report.entrySet()) {
  54.             String officeName = rep.getKey();
  55.             Double income = rep.getValue();
  56.             totalIncome += income;
  57.             System.out.println(officeName + " -> " + formatter.format(income));
  58.         }
  59.         System.out.println("Grand Total: -> " + formatter.format(totalIncome));
  60.     }
  61.  
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement