Advertisement
Guest User

Problem11_Excel

a guest
Jan 31st, 2015
265
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.65 KB | None | 0 0
  1. import java.io.File;
  2. import java.io.FileInputStream;
  3. import java.io.FileNotFoundException;
  4. import java.io.IOException;
  5. import java.io.InputStream;
  6. import java.io.PrintWriter;
  7. import java.util.Map;
  8. import java.util.TreeMap;
  9.  
  10. import org.apache.poi.ss.usermodel.Cell;
  11. import org.apache.poi.ss.usermodel.Row;
  12. import org.apache.poi.xssf.usermodel.XSSFSheet;
  13. import org.apache.poi.xssf.usermodel.XSSFWorkbook;
  14.  
  15. public class Probl11_Excel_ {
  16.     public static void main(String[] args) {
  17.          Map<String,Double> officeAndIncome = new TreeMap<>();     
  18.          try(InputStream file = new FileInputStream(new File("Incomes-Report.xlsx"));
  19.             XSSFWorkbook workbook = new XSSFWorkbook(file)) {                          
  20.                 XSSFSheet sheet = workbook.getSheetAt(0);
  21.                 java.util.Iterator<Row> rowIterator = sheet.iterator();            
  22.                 while(rowIterator.hasNext()) {
  23.                     Row row = rowIterator.next();                  
  24.                     java.util.Iterator<Cell> cellIterator = row.cellIterator();
  25.                     int cellNum = 0;
  26.                     while(cellIterator.hasNext()) {
  27.                         String office = "no office";
  28.                         if (cellNum == 0) {
  29.                             Cell cell = cellIterator.next();
  30.                             office = cell.getStringCellValue();
  31.                             boolean hasThisOffice = false;
  32.                             for(String off:officeAndIncome.keySet()){
  33.                                 if ((off).equals(office)) {
  34.                                     hasThisOffice = true;
  35.                         }
  36.                             }
  37.                             if (!hasThisOffice) {
  38.                                 officeAndIncome.put(office,0.0);
  39.                     }                          
  40.                         }
  41.                         if (cellNum == 5) {
  42.                             Cell cell = cellIterator.next();
  43.                             double income = cell.getNumericCellValue();
  44.                             for(String off:officeAndIncome.keySet()){
  45.                                 if ((off).equals(office)) {
  46.                             income+=officeAndIncome.get(off);
  47.                         }
  48.                             }
  49.                             officeAndIncome.put(office,income);
  50.                 }                                        
  51.                     }                
  52.                 }
  53.                 file.close();              
  54.                 PrintWriter writer = new PrintWriter("output.txt", "UTF-8");
  55.                 for(String office : officeAndIncome.keySet()){
  56.                     String line = office + " -> " + officeAndIncome.get(office) + "\n";
  57.                     writer.println(line);
  58.             }
  59.                 writer.close();              
  60.  
  61.          } catch (FileNotFoundException e) {
  62.                 System.out.println("File Not Found.");
  63.             } catch (IOException e) {
  64.                 e.printStackTrace();
  65.             }
  66.     }
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement