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.

DrawingBorders.java 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /*
  2. * ====================================================================
  3. * Licensed to the Apache Software Foundation (ASF) under one or more
  4. * contributor license agreements. See the NOTICE file distributed with
  5. * this work for additional information regarding copyright ownership.
  6. * The ASF licenses this file to You under the Apache License, Version 2.0
  7. * (the "License"); you may not use this file except in compliance with
  8. * the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. * ====================================================================
  18. */
  19. package org.apache.poi.examples.ss;
  20. import java.io.FileOutputStream;
  21. import java.io.IOException;
  22. import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  23. import org.apache.poi.ss.usermodel.BorderExtent;
  24. import org.apache.poi.ss.usermodel.BorderStyle;
  25. import org.apache.poi.ss.usermodel.Cell;
  26. import org.apache.poi.ss.usermodel.IndexedColors;
  27. import org.apache.poi.ss.usermodel.Row;
  28. import org.apache.poi.ss.usermodel.Sheet;
  29. import org.apache.poi.ss.usermodel.Workbook;
  30. import org.apache.poi.ss.util.CellRangeAddress;
  31. import org.apache.poi.ss.util.PropertyTemplate;
  32. import org.apache.poi.xssf.usermodel.XSSFWorkbook;
  33. /**
  34. * Excel Border Drawing - examples
  35. *
  36. * <p>
  37. * Partly based on the code snippets from
  38. * org.apache.poi.ss.examples.ConditionalFormats
  39. * </p>
  40. */
  41. @SuppressWarnings({"java:S106","java:S4823"})
  42. public final class DrawingBorders {
  43. private DrawingBorders() {}
  44. public static void main(String[] args) throws IOException {
  45. try (Workbook wb = (args.length > 0 && args[0].equals("-xls"))
  46. ? new HSSFWorkbook() : new XSSFWorkbook()) {
  47. // add a sheet, and put some values into it
  48. Sheet sh1 = wb.createSheet("Sheet1");
  49. Row r = sh1.createRow(0);
  50. Cell c = r.createCell(1);
  51. c.setCellValue("All Borders Medium Width");
  52. r = sh1.createRow(4);
  53. c = r.createCell(1);
  54. c.setCellValue("Medium Outside / Thin Inside Borders");
  55. r = sh1.createRow(8);
  56. c = r.createCell(1);
  57. c.setCellValue("Colored Borders");
  58. // draw borders (three 3x3 grids)
  59. PropertyTemplate pt = new PropertyTemplate();
  60. // #1) these borders will all be medium in default color
  61. pt.drawBorders(new CellRangeAddress(1, 3, 1, 3),
  62. BorderStyle.MEDIUM, BorderExtent.ALL);
  63. // #2) these cells will have medium outside borders and thin inside borders
  64. pt.drawBorders(new CellRangeAddress(5, 7, 1, 3),
  65. BorderStyle.MEDIUM, BorderExtent.OUTSIDE);
  66. pt.drawBorders(new CellRangeAddress(5, 7, 1, 3), BorderStyle.THIN,
  67. BorderExtent.INSIDE);
  68. // #3) these cells will all be medium weight with different colors for the
  69. // outside, inside horizontal, and inside vertical borders. The center
  70. // cell will have no borders.
  71. pt.drawBorders(new CellRangeAddress(9, 11, 1, 3),
  72. BorderStyle.MEDIUM, IndexedColors.RED.getIndex(),
  73. BorderExtent.OUTSIDE);
  74. pt.drawBorders(new CellRangeAddress(9, 11, 1, 3),
  75. BorderStyle.MEDIUM, IndexedColors.BLUE.getIndex(),
  76. BorderExtent.INSIDE_VERTICAL);
  77. pt.drawBorders(new CellRangeAddress(9, 11, 1, 3),
  78. BorderStyle.MEDIUM, IndexedColors.GREEN.getIndex(),
  79. BorderExtent.INSIDE_HORIZONTAL);
  80. pt.drawBorders(new CellRangeAddress(10, 10, 2, 2),
  81. BorderStyle.NONE,
  82. BorderExtent.ALL);
  83. // apply borders to sheet
  84. pt.applyBorders(sh1);
  85. // add another sheet and apply the borders to it
  86. Sheet sh2 = wb.createSheet("Sheet2");
  87. pt.applyBorders(sh2);
  88. // Write the output to a file
  89. String file = "db-poi.xls" + (wb instanceof XSSFWorkbook ? "x" : "");
  90. try (FileOutputStream out = new FileOutputStream(file)) {
  91. wb.write(out);
  92. }
  93. System.out.println("Generated: " + file);
  94. }
  95. }
  96. }