Advertisement
coasterka

#3ReadExcelFile

May 20th, 2014
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.79 KB | None | 0 0
  1. import java.text.DecimalFormat;
  2. import java.util.List;
  3. import java.io.FileInputStream;
  4. import java.io.FileNotFoundException;
  5. import java.io.IOException;
  6. import java.io.InputStream;
  7. import java.util.ArrayList;
  8. import java.util.Locale;
  9. import java.util.Map;
  10. import java.util.TreeMap;
  11.  
  12. import org.apache.poi.xssf.usermodel.XSSFCell;
  13. import org.apache.poi.xssf.usermodel.XSSFRow;
  14. import org.apache.poi.xssf.usermodel.XSSFSheet;
  15. import org.apache.poi.xssf.usermodel.XSSFWorkbook;
  16.  
  17.  
  18. public class Excel {
  19.  
  20.     public static void main(String[] args) throws IOException, FileNotFoundException {
  21.         readXLSXFile();
  22.     }
  23.  
  24.     private static void readXLSXFile() throws IOException, FileNotFoundException {
  25.        
  26.         Locale.setDefault(Locale.ROOT);
  27.        
  28.         InputStream excelFileToRead = new FileInputStream("Incomes-Report.xlsx");
  29.         XSSFWorkbook wb = new XSSFWorkbook(excelFileToRead);
  30.         XSSFSheet sheet = wb.getSheetAt(0);
  31.         int rowNum = sheet.getLastRowNum();
  32.         int colNum = sheet.getRow(0).getLastCellNum();
  33.         List<String> offices = new ArrayList<>();
  34.         List<Double> incomes = new ArrayList<Double>();
  35.         TreeMap<String, List<Double>> officeNameIncome = new TreeMap<>();
  36.        
  37.         for (int i = 1; i <= rowNum; i++) {
  38.            
  39.             XSSFRow row = sheet.getRow(i);
  40.            
  41.             for (int j = 0; j < colNum; j++) {
  42.                
  43.                 XSSFCell cell = row.getCell(j);
  44.                
  45.                 if (cell.getCellType() == XSSFCell.CELL_TYPE_STRING
  46.                         && j == 0) {
  47.                    
  48.                     offices.add(cell.getStringCellValue());
  49.                        
  50.                 }
  51.                
  52.                 if (cell.getCellType() == XSSFCell.CELL_TYPE_FORMULA
  53.                         && j == colNum - 1) {
  54.                    
  55.                     incomes.add(cell.getNumericCellValue());
  56.                    
  57.                 }
  58.             }
  59.         }
  60.        
  61.         String officeName = "";
  62.         Double officeIncome = 0.0;
  63.        
  64.         for (int i = 0; i < offices.size(); i++) {
  65.            
  66.             officeName = offices.get(i);
  67.             officeIncome = incomes.get(i);
  68.            
  69.             List<Double> tempIncomeList = new ArrayList<Double>();
  70.            
  71.             if (officeNameIncome.containsKey(officeName)) {
  72.  
  73.                 officeNameIncome.get(officeName).add(officeIncome);
  74.                
  75.             }
  76.            
  77.             else {
  78.                
  79.                 tempIncomeList.add(officeIncome);
  80.                 officeNameIncome.put(officeName, tempIncomeList);
  81.                
  82.             }
  83.         }
  84.        
  85.         List<String> treeMapKeys = new ArrayList<>();
  86.         DecimalFormat df = new DecimalFormat("#.00");
  87.         double grandTotal = 0.0;
  88.        
  89.         treeMapKeys.addAll(officeNameIncome.keySet());
  90.        
  91.         for (Map.Entry<String, List<Double>> office : officeNameIncome.entrySet()) {
  92.        
  93.             String key = office.getKey();
  94.             List<Double> value = office.getValue();
  95.            
  96.             double tempSum = 0.0;          
  97.            
  98.             for (int i = 0; i < value.size(); i++) {
  99.                
  100.                 tempSum += value.get(i);
  101.                
  102.             }          
  103.            
  104.             grandTotal += tempSum;
  105.            
  106.             System.out.println(key + " Total -> " + df.format(tempSum));
  107.            
  108.         }
  109.        
  110.         System.out.println("Grand Total -> " + df.format(grandTotal));
  111.        
  112.     }
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement