Search in sources :

Example 6 with Font

use of org.apache.poi.ss.usermodel.Font in project poi by apache.

the class WorkingWithFonts method main.

public static void main(String[] args) throws IOException {
    //or new HSSFWorkbook();
    Workbook wb = new XSSFWorkbook();
    Sheet sheet = wb.createSheet("Fonts");
    Font font0 = wb.createFont();
    font0.setColor(IndexedColors.BROWN.getIndex());
    CellStyle style0 = wb.createCellStyle();
    style0.setFont(font0);
    Font font1 = wb.createFont();
    font1.setFontHeightInPoints((short) 14);
    font1.setFontName("Courier New");
    font1.setColor(IndexedColors.RED.getIndex());
    CellStyle style1 = wb.createCellStyle();
    style1.setFont(font1);
    Font font2 = wb.createFont();
    font2.setFontHeightInPoints((short) 16);
    font2.setFontName("Arial");
    font2.setColor(IndexedColors.GREEN.getIndex());
    CellStyle style2 = wb.createCellStyle();
    style2.setFont(font2);
    Font font3 = wb.createFont();
    font3.setFontHeightInPoints((short) 18);
    font3.setFontName("Times New Roman");
    font3.setColor(IndexedColors.LAVENDER.getIndex());
    CellStyle style3 = wb.createCellStyle();
    style3.setFont(font3);
    Font font4 = wb.createFont();
    font4.setFontHeightInPoints((short) 18);
    font4.setFontName("Wingdings");
    font4.setColor(IndexedColors.GOLD.getIndex());
    CellStyle style4 = wb.createCellStyle();
    style4.setFont(font4);
    Font font5 = wb.createFont();
    font5.setFontName("Symbol");
    CellStyle style5 = wb.createCellStyle();
    style5.setFont(font5);
    Cell cell0 = sheet.createRow(0).createCell(1);
    cell0.setCellValue("Default");
    cell0.setCellStyle(style0);
    Cell cell1 = sheet.createRow(1).createCell(1);
    cell1.setCellValue("Courier");
    cell1.setCellStyle(style1);
    Cell cell2 = sheet.createRow(2).createCell(1);
    cell2.setCellValue("Arial");
    cell2.setCellStyle(style2);
    Cell cell3 = sheet.createRow(3).createCell(1);
    cell3.setCellValue("Times New Roman");
    cell3.setCellStyle(style3);
    Cell cell4 = sheet.createRow(4).createCell(1);
    cell4.setCellValue("Wingdings");
    cell4.setCellStyle(style4);
    Cell cell5 = sheet.createRow(5).createCell(1);
    cell5.setCellValue("Symbol");
    cell5.setCellStyle(style5);
    // Write the output to a file
    FileOutputStream fileOut = new FileOutputStream("xssf-fonts.xlsx");
    wb.write(fileOut);
    fileOut.close();
    wb.close();
}
Also used : FileOutputStream(java.io.FileOutputStream) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) CellStyle(org.apache.poi.ss.usermodel.CellStyle) Sheet(org.apache.poi.ss.usermodel.Sheet) Cell(org.apache.poi.ss.usermodel.Cell) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) Workbook(org.apache.poi.ss.usermodel.Workbook) Font(org.apache.poi.ss.usermodel.Font)

Example 7 with Font

use of org.apache.poi.ss.usermodel.Font in project poi by apache.

the class TestAutoSizeColumnTracker method assumeRequiredFontsAreInstalled.

private static void assumeRequiredFontsAreInstalled(final Workbook workbook, final Cell cell) {
    // autoSize will fail if required fonts are not installed, skip this test then
    Font font = workbook.getFontAt(cell.getCellStyle().getFontIndex());
    System.out.println(font.getFontHeightInPoints());
    System.out.println(font.getFontName());
    Assume.assumeTrue("Cannot verify autoSizeColumn() because the necessary Fonts are not installed on this machine: " + font, SheetUtil.canComputeColumnWidth(font));
}
Also used : Font(org.apache.poi.ss.usermodel.Font)

Example 8 with Font

use of org.apache.poi.ss.usermodel.Font in project poi by apache.

the class TestXSSFWorkbook method getFontAt.

@Test
public void getFontAt() throws IOException {
    XSSFWorkbook workbook = new XSSFWorkbook();
    try {
        StylesTable styleSource = workbook.getStylesSource();
        short i = 0;
        //get default font
        Font fontAt = workbook.getFontAt(i);
        assertNotNull(fontAt);
        //get customized font
        XSSFFont customFont = new XSSFFont();
        customFont.setItalic(true);
        int x = styleSource.putFont(customFont);
        fontAt = workbook.getFontAt((short) x);
        assertNotNull(fontAt);
    } finally {
        workbook.close();
    }
}
Also used : StylesTable(org.apache.poi.xssf.model.StylesTable) Font(org.apache.poi.ss.usermodel.Font) Test(org.junit.Test)

Example 9 with Font

use of org.apache.poi.ss.usermodel.Font in project poi by apache.

the class TestXSSFCell method testCopyCellFrom_CellCopyPolicy_mergeHyperlink.

@Test
public final void testCopyCellFrom_CellCopyPolicy_mergeHyperlink() throws IOException {
    setUp_testCopyCellFrom_CellCopyPolicy();
    final Workbook wb = srcCell.getSheet().getWorkbook();
    final CreationHelper createHelper = wb.getCreationHelper();
    srcCell.setCellValue("URL LINK");
    Hyperlink link = createHelper.createHyperlink(HyperlinkType.URL);
    link.setAddress("http://poi.apache.org/");
    destCell.setHyperlink(link);
    // Set link cell style (optional)
    CellStyle hlinkStyle = wb.createCellStyle();
    Font hlinkFont = wb.createFont();
    hlinkFont.setUnderline(Font.U_SINGLE);
    hlinkFont.setColor(IndexedColors.BLUE.getIndex());
    hlinkStyle.setFont(hlinkFont);
    destCell.setCellStyle(hlinkStyle);
    // Pre-condition assumptions. This test is broken if either of these fail.
    assertSame("unit test assumes srcCell and destCell are on the same sheet", srcCell.getSheet(), destCell.getSheet());
    assertNull(srcCell.getHyperlink());
    // Merge hyperlink - since srcCell doesn't have a hyperlink, destCell's hyperlink is not overwritten (cleared).
    final CellCopyPolicy policy = new CellCopyPolicy.Builder().mergeHyperlink(true).copyHyperlink(false).build();
    destCell.copyCellFrom(srcCell, policy);
    assertNull(srcCell.getHyperlink());
    assertNotNull(destCell.getHyperlink());
    assertSame(link, destCell.getHyperlink());
    List<XSSFHyperlink> links;
    links = srcCell.getSheet().getHyperlinkList();
    assertEquals("number of hyperlinks on sheet", 1, links.size());
    assertEquals("source hyperlink", new CellReference(destCell).formatAsString(), links.get(0).getCellRef());
    // Merge destCell's hyperlink to srcCell. Since destCell does have a hyperlink, this should copy destCell's hyperlink to srcCell.
    srcCell.copyCellFrom(destCell, policy);
    assertNotNull(srcCell.getHyperlink());
    assertNotNull(destCell.getHyperlink());
    links = srcCell.getSheet().getHyperlinkList();
    assertEquals("number of hyperlinks on sheet", 2, links.size());
    assertEquals("dest hyperlink", new CellReference(destCell).formatAsString(), links.get(0).getCellRef());
    assertEquals("source hyperlink", new CellReference(srcCell).formatAsString(), links.get(1).getCellRef());
    wb.close();
}
Also used : CreationHelper(org.apache.poi.ss.usermodel.CreationHelper) CellStyle(org.apache.poi.ss.usermodel.CellStyle) CellReference(org.apache.poi.ss.util.CellReference) Workbook(org.apache.poi.ss.usermodel.Workbook) Font(org.apache.poi.ss.usermodel.Font) CellCopyPolicy(org.apache.poi.ss.usermodel.CellCopyPolicy) Hyperlink(org.apache.poi.ss.usermodel.Hyperlink) Test(org.junit.Test)

Example 10 with Font

use of org.apache.poi.ss.usermodel.Font in project tika by apache.

the class XLSXHREFFormatter method reset.

@Override
public void reset(XSSFWorkbook workbook) {
    this.workbook = workbook;
    style = workbook.createCellStyle();
    Font hlinkFont = workbook.createFont();
    hlinkFont.setUnderline(Font.U_SINGLE);
    hlinkFont.setColor(IndexedColors.BLUE.getIndex());
    style.setFont(hlinkFont);
    links = 0;
}
Also used : Font(org.apache.poi.ss.usermodel.Font)

Aggregations

Font (org.apache.poi.ss.usermodel.Font)25 CellStyle (org.apache.poi.ss.usermodel.CellStyle)13 Workbook (org.apache.poi.ss.usermodel.Workbook)13 Cell (org.apache.poi.ss.usermodel.Cell)11 Sheet (org.apache.poi.ss.usermodel.Sheet)11 Row (org.apache.poi.ss.usermodel.Row)8 CreationHelper (org.apache.poi.ss.usermodel.CreationHelper)7 XSSFWorkbook (org.apache.poi.xssf.usermodel.XSSFWorkbook)6 Test (org.junit.Test)6 FileOutputStream (java.io.FileOutputStream)4 CellReference (org.apache.poi.ss.util.CellReference)4 HSSFWorkbook (org.apache.poi.hssf.usermodel.HSSFWorkbook)3 Hyperlink (org.apache.poi.ss.usermodel.Hyperlink)3 RichTextString (org.apache.poi.ss.usermodel.RichTextString)3 IOException (java.io.IOException)2 OutputStream (java.io.OutputStream)2 AttributedString (java.text.AttributedString)2 DecimalFormat (java.text.DecimalFormat)2 HashMap (java.util.HashMap)2 CellCopyPolicy (org.apache.poi.ss.usermodel.CellCopyPolicy)2