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();
}
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();
}
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));
}
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();
// }
}
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);
}
Aggregations