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.

CalendarDemo.java 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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.Calendar;
  18. import java.util.HashMap;
  19. import java.util.Map;
  20. import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  21. import org.apache.poi.ss.usermodel.BorderStyle;
  22. import org.apache.poi.ss.usermodel.Cell;
  23. import org.apache.poi.ss.usermodel.CellStyle;
  24. import org.apache.poi.ss.usermodel.FillPatternType;
  25. import org.apache.poi.ss.usermodel.Font;
  26. import org.apache.poi.ss.usermodel.HorizontalAlignment;
  27. import org.apache.poi.ss.usermodel.IndexedColors;
  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.VerticalAlignment;
  32. import org.apache.poi.ss.usermodel.Workbook;
  33. import org.apache.poi.ss.util.CellRangeAddress;
  34. import org.apache.poi.util.LocaleUtil;
  35. import org.apache.poi.xssf.usermodel.XSSFWorkbook;
  36. /**
  37. * A monthly calendar created using Apache POI. Each month is on a separate sheet.
  38. * <pre>
  39. * Usage:
  40. * CalendarDemo -xls|xlsx <year>
  41. * </pre>
  42. *
  43. * @author Yegor Kozlov
  44. */
  45. @SuppressWarnings({"java:S106","java:S4823","java:S1192"})
  46. public final class CalendarDemo {
  47. private static final String[] days = {
  48. "Sunday", "Monday", "Tuesday",
  49. "Wednesday", "Thursday", "Friday", "Saturday"};
  50. private static final String[] months = {
  51. "January", "February", "March","April", "May", "June","July", "August",
  52. "September","October", "November", "December"};
  53. private CalendarDemo() {}
  54. public static void main(String[] args) throws Exception {
  55. Calendar calendar = LocaleUtil.getLocaleCalendar();
  56. boolean xlsx = true;
  57. for (String arg : args) {
  58. if (arg.charAt(0) == '-') {
  59. xlsx = arg.equals("-xlsx");
  60. } else {
  61. calendar.set(Calendar.YEAR, Integer.parseInt(arg));
  62. }
  63. }
  64. int year = calendar.get(Calendar.YEAR);
  65. try (Workbook wb = xlsx ? new XSSFWorkbook() : new HSSFWorkbook()) {
  66. Map<String, CellStyle> styles = createStyles(wb);
  67. for (int month = 0; month < 12; month++) {
  68. calendar.set(Calendar.MONTH, month);
  69. calendar.set(Calendar.DAY_OF_MONTH, 1);
  70. //create a sheet for each month
  71. Sheet sheet = wb.createSheet(months[month]);
  72. //turn off gridlines
  73. sheet.setDisplayGridlines(false);
  74. sheet.setPrintGridlines(false);
  75. sheet.setFitToPage(true);
  76. sheet.setHorizontallyCenter(true);
  77. PrintSetup printSetup = sheet.getPrintSetup();
  78. printSetup.setLandscape(true);
  79. //the following three statements are required only for HSSF
  80. sheet.setAutobreaks(true);
  81. printSetup.setFitHeight((short) 1);
  82. printSetup.setFitWidth((short) 1);
  83. //the header row: centered text in 48pt font
  84. Row headerRow = sheet.createRow(0);
  85. headerRow.setHeightInPoints(80);
  86. Cell titleCell = headerRow.createCell(0);
  87. titleCell.setCellValue(months[month] + " " + year);
  88. titleCell.setCellStyle(styles.get("title"));
  89. sheet.addMergedRegion(CellRangeAddress.valueOf("$A$1:$N$1"));
  90. //header with month titles
  91. Row monthRow = sheet.createRow(1);
  92. for (int i = 0; i < days.length; i++) {
  93. //set column widths, the width is measured in units of 1/256th of a character width
  94. sheet.setColumnWidth(i * 2, 5 * 256); //the column is 5 characters wide
  95. sheet.setColumnWidth(i * 2 + 1, 13 * 256); //the column is 13 characters wide
  96. sheet.addMergedRegion(new CellRangeAddress(1, 1, i * 2, i * 2 + 1));
  97. Cell monthCell = monthRow.createCell(i * 2);
  98. monthCell.setCellValue(days[i]);
  99. monthCell.setCellStyle(styles.get("month"));
  100. }
  101. int cnt = 1, day = 1;
  102. int rownum = 2;
  103. for (int j = 0; j < 6; j++) {
  104. Row row = sheet.createRow(rownum++);
  105. row.setHeightInPoints(100);
  106. for (int i = 0; i < days.length; i++) {
  107. Cell dayCell_1 = row.createCell(i * 2);
  108. Cell dayCell_2 = row.createCell(i * 2 + 1);
  109. int day_of_week = calendar.get(Calendar.DAY_OF_WEEK);
  110. if (cnt >= day_of_week && calendar.get(Calendar.MONTH) == month) {
  111. dayCell_1.setCellValue(day);
  112. calendar.set(Calendar.DAY_OF_MONTH, ++day);
  113. if (i == 0 || i == days.length - 1) {
  114. dayCell_1.setCellStyle(styles.get("weekend_left"));
  115. dayCell_2.setCellStyle(styles.get("weekend_right"));
  116. } else {
  117. dayCell_1.setCellStyle(styles.get("workday_left"));
  118. dayCell_2.setCellStyle(styles.get("workday_right"));
  119. }
  120. } else {
  121. dayCell_1.setCellStyle(styles.get("grey_left"));
  122. dayCell_2.setCellStyle(styles.get("grey_right"));
  123. }
  124. cnt++;
  125. }
  126. if (calendar.get(Calendar.MONTH) > month) break;
  127. }
  128. }
  129. // Write the output to a file
  130. String file = "calendar.xls";
  131. if (wb instanceof XSSFWorkbook) file += "x";
  132. try (FileOutputStream out = new FileOutputStream(file)) {
  133. wb.write(out);
  134. }
  135. }
  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. short borderColor = IndexedColors.GREY_50_PERCENT.getIndex();
  143. CellStyle style;
  144. Font titleFont = wb.createFont();
  145. titleFont.setFontHeightInPoints((short)48);
  146. titleFont.setColor(IndexedColors.DARK_BLUE.getIndex());
  147. style = wb.createCellStyle();
  148. style.setAlignment(HorizontalAlignment.CENTER);
  149. style.setVerticalAlignment(VerticalAlignment.CENTER);
  150. style.setFont(titleFont);
  151. styles.put("title", style);
  152. Font monthFont = wb.createFont();
  153. monthFont.setFontHeightInPoints((short)12);
  154. monthFont.setColor(IndexedColors.WHITE.getIndex());
  155. monthFont.setBold(true);
  156. style = wb.createCellStyle();
  157. style.setAlignment(HorizontalAlignment.CENTER);
  158. style.setVerticalAlignment(VerticalAlignment.CENTER);
  159. style.setFillForegroundColor(IndexedColors.DARK_BLUE.getIndex());
  160. style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
  161. style.setFont(monthFont);
  162. styles.put("month", style);
  163. Font dayFont = wb.createFont();
  164. dayFont.setFontHeightInPoints((short)14);
  165. dayFont.setBold(true);
  166. style = wb.createCellStyle();
  167. style.setAlignment(HorizontalAlignment.LEFT);
  168. style.setVerticalAlignment(VerticalAlignment.TOP);
  169. style.setFillForegroundColor(IndexedColors.LIGHT_CORNFLOWER_BLUE.getIndex());
  170. style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
  171. style.setBorderLeft(BorderStyle.THIN);
  172. style.setLeftBorderColor(borderColor);
  173. style.setBorderBottom(BorderStyle.THIN);
  174. style.setBottomBorderColor(borderColor);
  175. style.setFont(dayFont);
  176. styles.put("weekend_left", style);
  177. style = wb.createCellStyle();
  178. style.setAlignment(HorizontalAlignment.CENTER);
  179. style.setVerticalAlignment(VerticalAlignment.TOP);
  180. style.setFillForegroundColor(IndexedColors.LIGHT_CORNFLOWER_BLUE.getIndex());
  181. style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
  182. style.setBorderRight(BorderStyle.THIN);
  183. style.setRightBorderColor(borderColor);
  184. style.setBorderBottom(BorderStyle.THIN);
  185. style.setBottomBorderColor(borderColor);
  186. styles.put("weekend_right", style);
  187. style = wb.createCellStyle();
  188. style.setAlignment(HorizontalAlignment.LEFT);
  189. style.setVerticalAlignment(VerticalAlignment.TOP);
  190. style.setBorderLeft(BorderStyle.THIN);
  191. style.setFillForegroundColor(IndexedColors.WHITE.getIndex());
  192. style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
  193. style.setLeftBorderColor(borderColor);
  194. style.setBorderBottom(BorderStyle.THIN);
  195. style.setBottomBorderColor(borderColor);
  196. style.setFont(dayFont);
  197. styles.put("workday_left", style);
  198. style = wb.createCellStyle();
  199. style.setAlignment(HorizontalAlignment.CENTER);
  200. style.setVerticalAlignment(VerticalAlignment.TOP);
  201. style.setFillForegroundColor(IndexedColors.WHITE.getIndex());
  202. style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
  203. style.setBorderRight(BorderStyle.THIN);
  204. style.setRightBorderColor(borderColor);
  205. style.setBorderBottom(BorderStyle.THIN);
  206. style.setBottomBorderColor(borderColor);
  207. styles.put("workday_right", style);
  208. style = wb.createCellStyle();
  209. style.setBorderLeft(BorderStyle.THIN);
  210. style.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
  211. style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
  212. style.setBorderBottom(BorderStyle.THIN);
  213. style.setBottomBorderColor(borderColor);
  214. styles.put("grey_left", style);
  215. style = wb.createCellStyle();
  216. style.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
  217. style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
  218. style.setBorderRight(BorderStyle.THIN);
  219. style.setRightBorderColor(borderColor);
  220. style.setBorderBottom(BorderStyle.THIN);
  221. style.setBottomBorderColor(borderColor);
  222. styles.put("grey_right", style);
  223. return styles;
  224. }
  225. }