Advertisement
IRusev

Excel reader

Sep 11th, 2014
326
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.71 KB | None | 0 0
  1. import java.io.FileInputStream;
  2. import java.io.IOException;
  3. import java.io.InputStream;
  4. import java.util.ArrayList;
  5. import java.util.Map.Entry;
  6. import java.util.TreeMap;
  7.  
  8. import org.apache.poi.xssf.usermodel.XSSFCell;
  9. import org.apache.poi.xssf.usermodel.XSSFRow;
  10. import org.apache.poi.xssf.usermodel.XSSFSheet;
  11. import org.apache.poi.xssf.usermodel.XSSFWorkbook;
  12.  
  13. public class Excel {
  14.  
  15. public static void main(String[] args) throws IOException {
  16.  
  17. ArrayList<String> offices = new ArrayList<String>();
  18. ArrayList<Double> incomes = new ArrayList<Double>();
  19.  
  20. InputStream myxls = new FileInputStream("Incomes-Report.xlsx");
  21. XSSFWorkbook wb = new XSSFWorkbook(myxls);
  22.  
  23. XSSFSheet sheet = wb.getSheetAt(0);
  24.  
  25. for (int i = 1; i <= 11; i++) {
  26.  
  27. XSSFRow rowOffice = sheet.getRow(i);
  28. XSSFCell cellOffice = rowOffice.getCell(0);
  29.  
  30. XSSFRow rowIncomes = sheet.getRow(i);
  31. XSSFCell cellIncomes = rowIncomes.getCell(3);
  32.  
  33. XSSFRow rowVat = sheet.getRow(i);
  34. XSSFCell cellVat = rowVat.getCell(4);
  35.  
  36. offices.add(cellOffice.getStringCellValue().trim());
  37. incomes.add(cellIncomes.getNumericCellValue()
  38. + cellVat.getNumericCellValue());
  39. }
  40.  
  41. TreeMap<String, Double> total = new TreeMap<String, Double>();
  42.  
  43. double grandTotal = 0.0d;
  44. for (int i = 0; i < offices.size(); i++) {
  45.  
  46. Double temp = total.get(offices.get(i));
  47. if (temp == null) {
  48. temp = 0.0d;
  49. }
  50. total.put(offices.get(i), temp + incomes.get(i));
  51. grandTotal += incomes.get(i);
  52. }
  53.  
  54. for (Entry<String, Double> wordEntry : total.entrySet()) {
  55.  
  56. System.out.printf("%s Total -> %.2f%n", wordEntry.getKey(), wordEntry.getValue());
  57.  
  58. }
  59. System.out.printf("Grand Total -> %.2f", grandTotal);
  60. }
  61.  
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement