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.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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.ss.examples;
  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.BorderStyle;
  24. import org.apache.poi.ss.usermodel.Cell;
  25. import org.apache.poi.ss.usermodel.IndexedColors;
  26. import org.apache.poi.ss.usermodel.Row;
  27. import org.apache.poi.ss.usermodel.Sheet;
  28. import org.apache.poi.ss.usermodel.Workbook;
  29. import org.apache.poi.ss.util.BorderPropertyTemplate;
  30. import org.apache.poi.ss.util.BorderPropertyTemplate.BorderExtent;
  31. import org.apache.poi.ss.util.CellRangeAddress;
  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. public class DrawingBorders {
  42. public static void main(String[] args) throws IOException {
  43. Workbook wb;
  44. if (args.length > 0 && args[0].equals("-xls")) {
  45. wb = new HSSFWorkbook();
  46. } else {
  47. wb = new XSSFWorkbook();
  48. }
  49. // add a sheet, and put some values into it
  50. Sheet sh1 = wb.createSheet("Sheet1");
  51. Row r = sh1.createRow(0);
  52. Cell c = r.createCell(1);
  53. c.setCellValue("All Borders Medium Width");
  54. r = sh1.createRow(4);
  55. c = r.createCell(1);
  56. c.setCellValue("Medium Outside / Thin Inside Borders");
  57. r = sh1.createRow(8);
  58. c = r.createCell(1);
  59. c.setCellValue("Colored Borders");
  60. CellRangeAddress b2d4 = CellRangeAddress.valueOf("B2:D4");
  61. CellRangeAddress b6d8 = CellRangeAddress.valueOf("B6:D8");
  62. CellRangeAddress b10d12 = CellRangeAddress.valueOf("B10:D12");
  63. CellRangeAddress c11 = CellRangeAddress.valueOf("C11:C11");
  64. short red = IndexedColors.RED.getIndex();
  65. short green = IndexedColors.GREEN.getIndex();
  66. short blue = IndexedColors.BLUE.getIndex();
  67. // draw borders (three 3x3 grids)
  68. BorderPropertyTemplate pt = new BorderPropertyTemplate();
  69. // #1) these borders will all be medium in default color
  70. pt.drawBorders(b2d4, BorderStyle.MEDIUM, BorderExtent.ALL);
  71. // #2) these cells will have medium outside borders and thin inside borders
  72. pt.drawBorders(b6d8, BorderStyle.MEDIUM, BorderExtent.OUTSIDE);
  73. pt.drawBorders(b6d8, BorderStyle.THIN, BorderExtent.INSIDE);
  74. // #3) these cells will all be medium weight with different colors for the
  75. // outside, inside horizontal, and inside vertical borders. The center
  76. // cell will have no borders.
  77. pt.drawColoredBorders(b10d12, BorderStyle.MEDIUM, red, BorderExtent.OUTSIDE);
  78. pt.drawColoredBorders(b10d12, BorderStyle.MEDIUM, blue, BorderExtent.INSIDE_VERTICAL);
  79. pt.drawColoredBorders(b10d12, BorderStyle.MEDIUM, green, BorderExtent.INSIDE_HORIZONTAL);
  80. pt.drawBorders(c11, BorderStyle.NONE, BorderExtent.ALL);
  81. // apply borders to sheet
  82. pt.applyBorders(sh1);
  83. // add another sheet and apply the borders to it
  84. Sheet sh2 = wb.createSheet("Sheet2");
  85. pt.applyBorders(sh2);
  86. // Write the output to a file
  87. String file = "DrawingBorders-poi.xls";
  88. if (wb instanceof XSSFWorkbook)
  89. file += "x";
  90. FileOutputStream out = new FileOutputStream(file);
  91. wb.write(out);
  92. out.close();
  93. wb.close();
  94. System.out.println("Generated: " + file);
  95. }
  96. }