You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

LoanCalculator.java 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. /* ====================================================================
  2. Licensed to the Apache Software Foundation (ASF) under one or more
  3. contributor license agreements. See the NOTICE file distributed with
  4. this work for additional information regarding copyright ownership.
  5. The ASF licenses this file to You under the Apache License, Version 2.0
  6. (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. ==================================================================== */
  15. package org.apache.poi.examples.ss;
  16. import java.io.FileOutputStream;
  17. import java.util.HashMap;
  18. import java.util.Map;
  19. import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  20. import org.apache.poi.ss.usermodel.BorderStyle;
  21. import org.apache.poi.ss.usermodel.Cell;
  22. import org.apache.poi.ss.usermodel.CellStyle;
  23. import org.apache.poi.ss.usermodel.FillPatternType;
  24. import org.apache.poi.ss.usermodel.Font;
  25. import org.apache.poi.ss.usermodel.HorizontalAlignment;
  26. import org.apache.poi.ss.usermodel.IndexedColors;
  27. import org.apache.poi.ss.usermodel.Name;
  28. import org.apache.poi.ss.usermodel.PrintSetup;
  29. import org.apache.poi.ss.usermodel.Row;
  30. import org.apache.poi.ss.usermodel.Sheet;
  31. import org.apache.poi.ss.usermodel.Workbook;
  32. import org.apache.poi.ss.util.CellRangeAddress;
  33. import org.apache.poi.xssf.usermodel.XSSFWorkbook;
  34. /**
  35. * Simple Loan Calculator. Demonstrates advance usage of cell formulas and named ranges.
  36. *
  37. * Usage:
  38. * LoanCalculator -xls|xlsx
  39. *
  40. * @author Yegor Kozlov
  41. */
  42. @SuppressWarnings({"java:S106","java:S4823","java:S1192"})
  43. public final class LoanCalculator {
  44. private LoanCalculator() {}
  45. public static void main(String[] args) throws Exception {
  46. Workbook wb;
  47. if(args.length > 0 && args[0].equals("-xls")) wb = new HSSFWorkbook();
  48. else wb = new XSSFWorkbook();
  49. Map<String, CellStyle> styles = createStyles(wb);
  50. Sheet sheet = wb.createSheet("Loan Calculator");
  51. sheet.setPrintGridlines(false);
  52. sheet.setDisplayGridlines(false);
  53. PrintSetup printSetup = sheet.getPrintSetup();
  54. printSetup.setLandscape(true);
  55. sheet.setFitToPage(true);
  56. sheet.setHorizontallyCenter(true);
  57. sheet.setColumnWidth(0, 3*256);
  58. sheet.setColumnWidth(1, 3*256);
  59. sheet.setColumnWidth(2, 11*256);
  60. sheet.setColumnWidth(3, 14*256);
  61. sheet.setColumnWidth(4, 14*256);
  62. sheet.setColumnWidth(5, 14*256);
  63. sheet.setColumnWidth(6, 14*256);
  64. createNames(wb);
  65. Row titleRow = sheet.createRow(0);
  66. titleRow.setHeightInPoints(35);
  67. for (int i = 1; i <= 7; i++) {
  68. titleRow.createCell(i).setCellStyle(styles.get("title"));
  69. }
  70. Cell titleCell = titleRow.getCell(2);
  71. titleCell.setCellValue("Simple Loan Calculator");
  72. sheet.addMergedRegion(CellRangeAddress.valueOf("$C$1:$H$1"));
  73. Row row = sheet.createRow(2);
  74. Cell cell = row.createCell(4);
  75. cell.setCellValue("Enter values");
  76. cell.setCellStyle(styles.get("item_right"));
  77. row = sheet.createRow(3);
  78. cell = row.createCell(2);
  79. cell.setCellValue("Loan amount");
  80. cell.setCellStyle(styles.get("item_left"));
  81. cell = row.createCell(4);
  82. cell.setCellStyle(styles.get("input_$"));
  83. cell.setAsActiveCell();
  84. row = sheet.createRow(4);
  85. cell = row.createCell(2);
  86. cell.setCellValue("Annual interest rate");
  87. cell.setCellStyle(styles.get("item_left"));
  88. cell = row.createCell(4);
  89. cell.setCellStyle(styles.get("input_%"));
  90. row = sheet.createRow(5);
  91. cell = row.createCell(2);
  92. cell.setCellValue("Loan period in years");
  93. cell.setCellStyle(styles.get("item_left"));
  94. cell = row.createCell(4);
  95. cell.setCellStyle(styles.get("input_i"));
  96. row = sheet.createRow(6);
  97. cell = row.createCell(2);
  98. cell.setCellValue("Start date of loan");
  99. cell.setCellStyle(styles.get("item_left"));
  100. cell = row.createCell(4);
  101. cell.setCellStyle(styles.get("input_d"));
  102. row = sheet.createRow(8);
  103. cell = row.createCell(2);
  104. cell.setCellValue("Monthly payment");
  105. cell.setCellStyle(styles.get("item_left"));
  106. cell = row.createCell(4);
  107. cell.setCellFormula("IF(Values_Entered,Monthly_Payment,\"\")");
  108. cell.setCellStyle(styles.get("formula_$"));
  109. row = sheet.createRow(9);
  110. cell = row.createCell(2);
  111. cell.setCellValue("Number of payments");
  112. cell.setCellStyle(styles.get("item_left"));
  113. cell = row.createCell(4);
  114. cell.setCellFormula("IF(Values_Entered,Loan_Years*12,\"\")");
  115. cell.setCellStyle(styles.get("formula_i"));
  116. row = sheet.createRow(10);
  117. cell = row.createCell(2);
  118. cell.setCellValue("Total interest");
  119. cell.setCellStyle(styles.get("item_left"));
  120. cell = row.createCell(4);
  121. cell.setCellFormula("IF(Values_Entered,Total_Cost-Loan_Amount,\"\")");
  122. cell.setCellStyle(styles.get("formula_$"));
  123. row = sheet.createRow(11);
  124. cell = row.createCell(2);
  125. cell.setCellValue("Total cost of loan");
  126. cell.setCellStyle(styles.get("item_left"));
  127. cell = row.createCell(4);
  128. cell.setCellFormula("IF(Values_Entered,Monthly_Payment*Number_of_Payments,\"\")");
  129. cell.setCellStyle(styles.get("formula_$"));
  130. // Write the output to a file
  131. String file = "loan-calculator.xls";
  132. if(wb instanceof XSSFWorkbook) file += "x";
  133. FileOutputStream out = new FileOutputStream(file);
  134. wb.write(out);
  135. out.close();
  136. }
  137. /**
  138. * cell styles used for formatting calendar sheets
  139. */
  140. private static Map<String, CellStyle> createStyles(Workbook wb){
  141. Map<String, CellStyle> styles = new HashMap<>();
  142. CellStyle style;
  143. Font titleFont = wb.createFont();
  144. titleFont.setFontHeightInPoints((short)14);
  145. titleFont.setFontName("Trebuchet MS");
  146. style = wb.createCellStyle();
  147. style.setFont(titleFont);
  148. style.setBorderBottom(BorderStyle.DOTTED);
  149. style.setBottomBorderColor(IndexedColors.GREY_40_PERCENT.getIndex());
  150. styles.put("title", style);
  151. Font itemFont = wb.createFont();
  152. itemFont.setFontHeightInPoints((short)9);
  153. itemFont.setFontName("Trebuchet MS");
  154. style = wb.createCellStyle();
  155. style.setAlignment(HorizontalAlignment.LEFT);
  156. style.setFont(itemFont);
  157. styles.put("item_left", style);
  158. style = wb.createCellStyle();
  159. style.setAlignment(HorizontalAlignment.RIGHT);
  160. style.setFont(itemFont);
  161. styles.put("item_right", style);
  162. style = wb.createCellStyle();
  163. style.setAlignment(HorizontalAlignment.RIGHT);
  164. style.setFont(itemFont);
  165. style.setBorderRight(BorderStyle.DOTTED);
  166. style.setRightBorderColor(IndexedColors.GREY_40_PERCENT.getIndex());
  167. style.setBorderBottom(BorderStyle.DOTTED);
  168. style.setBottomBorderColor(IndexedColors.GREY_40_PERCENT.getIndex());
  169. style.setBorderLeft(BorderStyle.DOTTED);
  170. style.setLeftBorderColor(IndexedColors.GREY_40_PERCENT.getIndex());
  171. style.setBorderTop(BorderStyle.DOTTED);
  172. style.setTopBorderColor(IndexedColors.GREY_40_PERCENT.getIndex());
  173. style.setDataFormat(wb.createDataFormat().getFormat("_($* #,##0.00_);_($* (#,##0.00);_($* \"-\"??_);_(@_)"));
  174. styles.put("input_$", style);
  175. style = wb.createCellStyle();
  176. style.setAlignment(HorizontalAlignment.RIGHT);
  177. style.setFont(itemFont);
  178. style.setBorderRight(BorderStyle.DOTTED);
  179. style.setRightBorderColor(IndexedColors.GREY_40_PERCENT.getIndex());
  180. style.setBorderBottom(BorderStyle.DOTTED);
  181. style.setBottomBorderColor(IndexedColors.GREY_40_PERCENT.getIndex());
  182. style.setBorderLeft(BorderStyle.DOTTED);
  183. style.setLeftBorderColor(IndexedColors.GREY_40_PERCENT.getIndex());
  184. style.setBorderTop(BorderStyle.DOTTED);
  185. style.setTopBorderColor(IndexedColors.GREY_40_PERCENT.getIndex());
  186. style.setDataFormat(wb.createDataFormat().getFormat("0.000%"));
  187. styles.put("input_%", style);
  188. style = wb.createCellStyle();
  189. style.setAlignment(HorizontalAlignment.RIGHT);
  190. style.setFont(itemFont);
  191. style.setBorderRight(BorderStyle.DOTTED);
  192. style.setRightBorderColor(IndexedColors.GREY_40_PERCENT.getIndex());
  193. style.setBorderBottom(BorderStyle.DOTTED);
  194. style.setBottomBorderColor(IndexedColors.GREY_40_PERCENT.getIndex());
  195. style.setBorderLeft(BorderStyle.DOTTED);
  196. style.setLeftBorderColor(IndexedColors.GREY_40_PERCENT.getIndex());
  197. style.setBorderTop(BorderStyle.DOTTED);
  198. style.setTopBorderColor(IndexedColors.GREY_40_PERCENT.getIndex());
  199. style.setDataFormat(wb.createDataFormat().getFormat("0"));
  200. styles.put("input_i", style);
  201. style = wb.createCellStyle();
  202. style.setAlignment(HorizontalAlignment.CENTER);
  203. style.setFont(itemFont);
  204. style.setDataFormat(wb.createDataFormat().getFormat("m/d/yy"));
  205. styles.put("input_d", style);
  206. style = wb.createCellStyle();
  207. style.setAlignment(HorizontalAlignment.RIGHT);
  208. style.setFont(itemFont);
  209. style.setBorderRight(BorderStyle.DOTTED);
  210. style.setRightBorderColor(IndexedColors.GREY_40_PERCENT.getIndex());
  211. style.setBorderBottom(BorderStyle.DOTTED);
  212. style.setBottomBorderColor(IndexedColors.GREY_40_PERCENT.getIndex());
  213. style.setBorderLeft(BorderStyle.DOTTED);
  214. style.setLeftBorderColor(IndexedColors.GREY_40_PERCENT.getIndex());
  215. style.setBorderTop(BorderStyle.DOTTED);
  216. style.setTopBorderColor(IndexedColors.GREY_40_PERCENT.getIndex());
  217. style.setDataFormat(wb.createDataFormat().getFormat("$##,##0.00"));
  218. style.setBorderBottom(BorderStyle.DOTTED);
  219. style.setBottomBorderColor(IndexedColors.GREY_40_PERCENT.getIndex());
  220. style.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
  221. style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
  222. styles.put("formula_$", style);
  223. style = wb.createCellStyle();
  224. style.setAlignment(HorizontalAlignment.RIGHT);
  225. style.setFont(itemFont);
  226. style.setBorderRight(BorderStyle.DOTTED);
  227. style.setRightBorderColor(IndexedColors.GREY_40_PERCENT.getIndex());
  228. style.setBorderBottom(BorderStyle.DOTTED);
  229. style.setBottomBorderColor(IndexedColors.GREY_40_PERCENT.getIndex());
  230. style.setBorderLeft(BorderStyle.DOTTED);
  231. style.setLeftBorderColor(IndexedColors.GREY_40_PERCENT.getIndex());
  232. style.setBorderTop(BorderStyle.DOTTED);
  233. style.setTopBorderColor(IndexedColors.GREY_40_PERCENT.getIndex());
  234. style.setDataFormat(wb.createDataFormat().getFormat("0"));
  235. style.setBorderBottom(BorderStyle.DOTTED);
  236. style.setBottomBorderColor(IndexedColors.GREY_40_PERCENT.getIndex());
  237. style.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
  238. style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
  239. styles.put("formula_i", style);
  240. return styles;
  241. }
  242. //define named ranges for the inputs and formulas
  243. public static void createNames(Workbook wb){
  244. Name name;
  245. name = wb.createName();
  246. name.setNameName("Interest_Rate");
  247. name.setRefersToFormula("'Loan Calculator'!$E$5");
  248. name = wb.createName();
  249. name.setNameName("Loan_Amount");
  250. name.setRefersToFormula("'Loan Calculator'!$E$4");
  251. name = wb.createName();
  252. name.setNameName("Loan_Start");
  253. name.setRefersToFormula("'Loan Calculator'!$E$7");
  254. name = wb.createName();
  255. name.setNameName("Loan_Years");
  256. name.setRefersToFormula("'Loan Calculator'!$E$6");
  257. name = wb.createName();
  258. name.setNameName("Number_of_Payments");
  259. name.setRefersToFormula("'Loan Calculator'!$E$10");
  260. name = wb.createName();
  261. name.setNameName("Monthly_Payment");
  262. name.setRefersToFormula("-PMT(Interest_Rate/12,Number_of_Payments,Loan_Amount)");
  263. name = wb.createName();
  264. name.setNameName("Total_Cost");
  265. name.setRefersToFormula("'Loan Calculator'!$E$12");
  266. name = wb.createName();
  267. name.setNameName("Total_Interest");
  268. name.setRefersToFormula("'Loan Calculator'!$E$11");
  269. name = wb.createName();
  270. name.setNameName("Values_Entered");
  271. name.setRefersToFormula("IF(Loan_Amount*Interest_Rate*Loan_Years*Loan_Start>0,1,0)");
  272. }
  273. }