Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.PrintWriter;
- import java.util.Map;
- import java.util.TreeMap;
- 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;
- public class Probl11_Excel_ {
- public static void main(String[] args) {
- Map<String,Double> officeAndIncome = new TreeMap<>();
- try(InputStream file = new FileInputStream(new File("Incomes-Report.xlsx"));
- XSSFWorkbook workbook = new XSSFWorkbook(file)) {
- XSSFSheet sheet = workbook.getSheetAt(0);
- java.util.Iterator<Row> rowIterator = sheet.iterator();
- while(rowIterator.hasNext()) {
- Row row = rowIterator.next();
- java.util.Iterator<Cell> cellIterator = row.cellIterator();
- int cellNum = 0;
- while(cellIterator.hasNext()) {
- String office = "no office";
- if (cellNum == 0) {
- Cell cell = cellIterator.next();
- office = cell.getStringCellValue();
- boolean hasThisOffice = false;
- for(String off:officeAndIncome.keySet()){
- if ((off).equals(office)) {
- hasThisOffice = true;
- }
- }
- if (!hasThisOffice) {
- officeAndIncome.put(office,0.0);
- }
- }
- if (cellNum == 5) {
- Cell cell = cellIterator.next();
- double income = cell.getNumericCellValue();
- for(String off:officeAndIncome.keySet()){
- if ((off).equals(office)) {
- income+=officeAndIncome.get(off);
- }
- }
- officeAndIncome.put(office,income);
- }
- }
- }
- file.close();
- PrintWriter writer = new PrintWriter("output.txt", "UTF-8");
- for(String office : officeAndIncome.keySet()){
- String line = office + " -> " + officeAndIncome.get(office) + "\n";
- writer.println(line);
- }
- writer.close();
- } catch (FileNotFoundException e) {
- System.out.println("File Not Found.");
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement