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.

AligningCells.java 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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.io.IOException;
  18. import java.io.OutputStream;
  19. import org.apache.poi.ss.usermodel.Cell;
  20. import org.apache.poi.ss.usermodel.CellStyle;
  21. import org.apache.poi.ss.usermodel.CreationHelper;
  22. import org.apache.poi.ss.usermodel.HorizontalAlignment;
  23. import org.apache.poi.ss.usermodel.Row;
  24. import org.apache.poi.ss.usermodel.Sheet;
  25. import org.apache.poi.ss.usermodel.VerticalAlignment;
  26. import org.apache.poi.ss.usermodel.Workbook;
  27. import org.apache.poi.xssf.usermodel.XSSFWorkbook;
  28. /**
  29. * Shows how various alignment options work.
  30. */
  31. public class AligningCells {
  32. public static void main(String[] args) throws IOException {
  33. try (Workbook wb = new XSSFWorkbook()) { //or new HSSFWorkbook();
  34. Sheet sheet = wb.createSheet();
  35. Row row = sheet.createRow(2);
  36. row.setHeightInPoints(30);
  37. for (int i = 0; i < 8; i++) {
  38. //column width is set in units of 1/256th of a character width
  39. sheet.setColumnWidth(i, 256 * 15);
  40. }
  41. createCell(wb, row, 0, HorizontalAlignment.CENTER, VerticalAlignment.BOTTOM);
  42. createCell(wb, row, 1, HorizontalAlignment.CENTER_SELECTION, VerticalAlignment.BOTTOM);
  43. createCell(wb, row, 2, HorizontalAlignment.FILL, VerticalAlignment.CENTER);
  44. createCell(wb, row, 3, HorizontalAlignment.GENERAL, VerticalAlignment.CENTER);
  45. createCell(wb, row, 4, HorizontalAlignment.JUSTIFY, VerticalAlignment.JUSTIFY);
  46. createCell(wb, row, 5, HorizontalAlignment.LEFT, VerticalAlignment.TOP);
  47. createCell(wb, row, 6, HorizontalAlignment.RIGHT, VerticalAlignment.TOP);
  48. // Write the output to a file
  49. try (OutputStream fileOut = new FileOutputStream("ss-example-align.xlsx")) {
  50. wb.write(fileOut);
  51. }
  52. }
  53. }
  54. /**
  55. * Creates a cell and aligns it a certain way.
  56. *
  57. * @param wb the workbook
  58. * @param row the row to create the cell in
  59. * @param column the column number to create the cell in
  60. * @param halign the horizontal alignment for the cell.
  61. */
  62. private static void createCell(Workbook wb, Row row, int column, HorizontalAlignment halign, VerticalAlignment valign) {
  63. CreationHelper ch = wb.getCreationHelper();
  64. Cell cell = row.createCell(column);
  65. cell.setCellValue(ch.createRichTextString("Align It"));
  66. CellStyle cellStyle = wb.createCellStyle();
  67. cellStyle.setAlignment(halign);
  68. cellStyle.setVerticalAlignment(valign);
  69. cell.setCellStyle(cellStyle);
  70. }
  71. }