Search in sources :

Example 16 with Font

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

the class CreateCell method main.

public static void main(String[] args) throws IOException {
    //or new HSSFWorkbook();
    Workbook wb = new XSSFWorkbook();
    CreationHelper creationHelper = wb.getCreationHelper();
    Sheet sheet = wb.createSheet("new sheet");
    // Create a row and put some cells in it. Rows are 0 based.
    Row row = sheet.createRow((short) 0);
    // Create a cell and put a value in it.
    Cell cell = row.createCell((short) 0);
    cell.setCellValue(1);
    //numeric value
    row.createCell(1).setCellValue(1.2);
    //plain string value
    row.createCell(2).setCellValue("This is a string cell");
    //rich text string
    RichTextString str = creationHelper.createRichTextString("Apache");
    Font font = wb.createFont();
    font.setItalic(true);
    font.setUnderline(Font.U_SINGLE);
    str.applyFont(font);
    row.createCell(3).setCellValue(str);
    //boolean value
    row.createCell(4).setCellValue(true);
    //formula
    row.createCell(5).setCellFormula("SUM(A1:B1)");
    //date
    CellStyle style = wb.createCellStyle();
    style.setDataFormat(creationHelper.createDataFormat().getFormat("m/d/yy h:mm"));
    cell = row.createCell(6);
    cell.setCellValue(new Date());
    cell.setCellStyle(style);
    //hyperlink
    row.createCell(7).setCellFormula("SUM(A1:B1)");
    cell.setCellFormula("HYPERLINK(\"http://google.com\",\"Google\")");
    // Write the output to a file
    FileOutputStream fileOut = new FileOutputStream("ooxml-cell.xlsx");
    wb.write(fileOut);
    fileOut.close();
    wb.close();
}
Also used : CreationHelper(org.apache.poi.ss.usermodel.CreationHelper) FileOutputStream(java.io.FileOutputStream) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) Row(org.apache.poi.ss.usermodel.Row) CellStyle(org.apache.poi.ss.usermodel.CellStyle) RichTextString(org.apache.poi.ss.usermodel.RichTextString) 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) Date(java.util.Date)

Example 17 with Font

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

the class HyperlinkExample method main.

public static void main(String[] args) throws IOException {
    //or new HSSFWorkbook();
    Workbook wb = new XSSFWorkbook();
    CreationHelper createHelper = wb.getCreationHelper();
    //cell style for hyperlinks
    //by default hyperlinks are blue and underlined
    CellStyle hlink_style = wb.createCellStyle();
    Font hlink_font = wb.createFont();
    hlink_font.setUnderline(Font.U_SINGLE);
    hlink_font.setColor(IndexedColors.BLUE.getIndex());
    hlink_style.setFont(hlink_font);
    Cell cell;
    Sheet sheet = wb.createSheet("Hyperlinks");
    //URL
    cell = sheet.createRow(0).createCell(0);
    cell.setCellValue("URL Link");
    Hyperlink link = createHelper.createHyperlink(HyperlinkType.URL);
    link.setAddress("http://poi.apache.org/");
    cell.setHyperlink(link);
    cell.setCellStyle(hlink_style);
    //link to a file in the current directory
    cell = sheet.createRow(1).createCell(0);
    cell.setCellValue("File Link");
    link = createHelper.createHyperlink(HyperlinkType.FILE);
    link.setAddress("link1.xls");
    cell.setHyperlink(link);
    cell.setCellStyle(hlink_style);
    //e-mail link
    cell = sheet.createRow(2).createCell(0);
    cell.setCellValue("Email Link");
    link = createHelper.createHyperlink(HyperlinkType.EMAIL);
    //note, if subject contains white spaces, make sure they are url-encoded
    link.setAddress("mailto:poi@apache.org?subject=Hyperlinks");
    cell.setHyperlink(link);
    cell.setCellStyle(hlink_style);
    //link to a place in this workbook
    //create a target sheet and cell
    Sheet sheet2 = wb.createSheet("Target Sheet");
    sheet2.createRow(0).createCell(0).setCellValue("Target Cell");
    cell = sheet.createRow(3).createCell(0);
    cell.setCellValue("Worksheet Link");
    Hyperlink link2 = createHelper.createHyperlink(HyperlinkType.DOCUMENT);
    link2.setAddress("'Target Sheet'!A1");
    cell.setHyperlink(link2);
    cell.setCellStyle(hlink_style);
    FileOutputStream out = new FileOutputStream("hyperinks.xlsx");
    wb.write(out);
    out.close();
    wb.close();
}
Also used : CreationHelper(org.apache.poi.ss.usermodel.CreationHelper) FileOutputStream(java.io.FileOutputStream) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) CellStyle(org.apache.poi.ss.usermodel.CellStyle) Cell(org.apache.poi.ss.usermodel.Cell) Sheet(org.apache.poi.ss.usermodel.Sheet) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) Workbook(org.apache.poi.ss.usermodel.Workbook) Font(org.apache.poi.ss.usermodel.Font) Hyperlink(org.apache.poi.ss.usermodel.Hyperlink)

Example 18 with Font

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

the class TestSXSSFSheetAutoSizeColumn 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());
    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 19 with Font

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

the class TestCellStyle method test56959.

public void test56959() throws IOException {
    Workbook wb = new HSSFWorkbook();
    Sheet sheet = wb.createSheet("somesheet");
    Row row = sheet.createRow(0);
    // Create a new font and alter it.
    Font font = wb.createFont();
    font.setFontHeightInPoints((short) 24);
    font.setFontName("Courier New");
    font.setItalic(true);
    font.setStrikeout(true);
    font.setColor(Font.COLOR_RED);
    CellStyle style = wb.createCellStyle();
    style.setBorderBottom(BorderStyle.DOTTED);
    style.setFont(font);
    Cell cell = row.createCell(0);
    cell.setCellStyle(style);
    cell.setCellValue("testtext");
    Cell newCell = row.createCell(1);
    newCell.setCellStyle(style);
    newCell.setCellValue("2testtext2");
    CellStyle newStyle = newCell.getCellStyle();
    assertEquals(BorderStyle.DOTTED, newStyle.getBorderBottomEnum());
    assertEquals(Font.COLOR_RED, ((HSSFCellStyle) newStyle).getFont(wb).getColor());
//        OutputStream out = new FileOutputStream("/tmp/56959.xls");
//        try {
//            wb.write(out);
//        } finally {
//            out.close();
//        }
}
Also used : Row(org.apache.poi.ss.usermodel.Row) CellStyle(org.apache.poi.ss.usermodel.CellStyle) Sheet(org.apache.poi.ss.usermodel.Sheet) Cell(org.apache.poi.ss.usermodel.Cell) Workbook(org.apache.poi.ss.usermodel.Workbook) Font(org.apache.poi.ss.usermodel.Font)

Example 20 with Font

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

the class TestXSSFFont method testCanComputeWidthInvalidFont.

// store test from TestSheetUtil here as it uses XSSF
@Test
public void testCanComputeWidthInvalidFont() throws IOException {
    Font font = new XSSFFont(CTFont.Factory.newInstance());
    font.setFontName("some non existing font name");
    // Even with invalid fonts we still get back useful data most of the time... 
    SheetUtil.canComputeColumnWidth(font);
}
Also used : BaseTestFont(org.apache.poi.ss.usermodel.BaseTestFont) Font(org.apache.poi.ss.usermodel.Font) CTFont(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTFont) Test(org.junit.Test)

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