Search in sources :

Example 21 with CreationHelper

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

the class CellComments method main.

public static void main(String[] args) throws IOException {
    Workbook wb = new XSSFWorkbook();
    CreationHelper factory = wb.getCreationHelper();
    Sheet sheet = wb.createSheet();
    Cell cell1 = sheet.createRow(3).createCell(5);
    cell1.setCellValue("F4");
    Drawing<?> drawing = sheet.createDrawingPatriarch();
    ClientAnchor anchor = factory.createClientAnchor();
    Comment comment1 = drawing.createCellComment(anchor);
    RichTextString str1 = factory.createRichTextString("Hello, World!");
    comment1.setString(str1);
    comment1.setAuthor("Apache POI");
    cell1.setCellComment(comment1);
    Cell cell2 = sheet.createRow(2).createCell(2);
    cell2.setCellValue("C3");
    Comment comment2 = drawing.createCellComment(anchor);
    RichTextString str2 = factory.createRichTextString("XSSF can set cell comments");
    //apply custom font to the text in the comment
    Font font = wb.createFont();
    font.setFontName("Arial");
    font.setFontHeightInPoints((short) 14);
    font.setBold(true);
    font.setColor(IndexedColors.RED.getIndex());
    str2.applyFont(font);
    comment2.setString(str2);
    comment2.setAuthor("Apache POI");
    comment2.setAddress(new CellAddress("C3"));
    String fname = "comments.xlsx";
    FileOutputStream out = new FileOutputStream(fname);
    wb.write(out);
    out.close();
    wb.close();
}
Also used : Comment(org.apache.poi.ss.usermodel.Comment) CreationHelper(org.apache.poi.ss.usermodel.CreationHelper) RichTextString(org.apache.poi.ss.usermodel.RichTextString) RichTextString(org.apache.poi.ss.usermodel.RichTextString) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) Workbook(org.apache.poi.ss.usermodel.Workbook) Font(org.apache.poi.ss.usermodel.Font) CellAddress(org.apache.poi.ss.util.CellAddress) ClientAnchor(org.apache.poi.ss.usermodel.ClientAnchor) FileOutputStream(java.io.FileOutputStream) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) Sheet(org.apache.poi.ss.usermodel.Sheet) Cell(org.apache.poi.ss.usermodel.Cell)

Example 22 with CreationHelper

use of org.apache.poi.ss.usermodel.CreationHelper 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 23 with CreationHelper

use of org.apache.poi.ss.usermodel.CreationHelper 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 24 with CreationHelper

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

the class TestCommentsTable method bug54920.

@Test
public void bug54920() throws IOException {
    final Workbook workbook = new XSSFWorkbook();
    final Sheet sheet = workbook.createSheet("sheet01");
    // create anchor
    CreationHelper helper = sheet.getWorkbook().getCreationHelper();
    ClientAnchor anchor = helper.createClientAnchor();
    // place comment in A1
    // NOTE - only occurs if a comment is placed in A1 first
    Cell A1 = getCell(sheet, 0, 0);
    //Cell A1 = getCell(sheet, 2, 2);
    Drawing<?> drawing = sheet.createDrawingPatriarch();
    setComment(sheet, A1, drawing, "for A1", helper, anchor);
    // find comment in A1 before we set the comment in B2
    Comment commentA1 = A1.getCellComment();
    assertNotNull("Should still find the previous comment in A1, but had null", commentA1);
    assertEquals("should find correct comment in A1, but had null: " + commentA1, "for A1", commentA1.getString().getString());
    // place comment in B2, according to Bug 54920 this removes the comment in A1!
    Cell B2 = getCell(sheet, 1, 1);
    setComment(sheet, B2, drawing, "for B2", helper, anchor);
    // find comment in A1
    Comment commentB2 = B2.getCellComment();
    assertEquals("should find correct comment in B2, but had null: " + commentB2, "for B2", commentB2.getString().getString());
    // find comment in A1
    commentA1 = A1.getCellComment();
    assertNotNull("Should still find the previous comment in A1, but had null", commentA1);
    assertEquals("should find correct comment in A1, but had null: " + commentA1, "for A1", commentA1.getString().getString());
    workbook.close();
}
Also used : Comment(org.apache.poi.ss.usermodel.Comment) CTComment(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTComment) ClientAnchor(org.apache.poi.ss.usermodel.ClientAnchor) XSSFClientAnchor(org.apache.poi.xssf.usermodel.XSSFClientAnchor) CreationHelper(org.apache.poi.ss.usermodel.CreationHelper) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) Sheet(org.apache.poi.ss.usermodel.Sheet) XSSFSheet(org.apache.poi.xssf.usermodel.XSSFSheet) Cell(org.apache.poi.ss.usermodel.Cell) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) Workbook(org.apache.poi.ss.usermodel.Workbook) Test(org.junit.Test)

Example 25 with CreationHelper

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

the class TestXSSFSheet method testInsertCommentsToClonedSheet.

/**
     * See bug #52425
     */
@Test
public void testInsertCommentsToClonedSheet() {
    Workbook wb = XSSFTestDataSamples.openSampleWorkbook("52425.xlsx");
    CreationHelper helper = wb.getCreationHelper();
    Sheet sheet2 = wb.createSheet("Sheet 2");
    Sheet sheet3 = wb.cloneSheet(0);
    wb.setSheetName(2, "Sheet 3");
    // Adding Comment to new created Sheet 2
    addComments(helper, sheet2);
    // Adding Comment to cloned Sheet 3
    addComments(helper, sheet3);
}
Also used : CreationHelper(org.apache.poi.ss.usermodel.CreationHelper) BaseTestXSheet(org.apache.poi.ss.usermodel.BaseTestXSheet) Sheet(org.apache.poi.ss.usermodel.Sheet) XSSFTestDataSamples.openSampleWorkbook(org.apache.poi.xssf.XSSFTestDataSamples.openSampleWorkbook) SXSSFWorkbook(org.apache.poi.xssf.streaming.SXSSFWorkbook) Workbook(org.apache.poi.ss.usermodel.Workbook) Test(org.junit.Test)

Aggregations

CreationHelper (org.apache.poi.ss.usermodel.CreationHelper)37 Sheet (org.apache.poi.ss.usermodel.Sheet)21 Cell (org.apache.poi.ss.usermodel.Cell)19 Workbook (org.apache.poi.ss.usermodel.Workbook)19 Row (org.apache.poi.ss.usermodel.Row)17 CellStyle (org.apache.poi.ss.usermodel.CellStyle)15 XSSFWorkbook (org.apache.poi.xssf.usermodel.XSSFWorkbook)15 Font (org.apache.poi.ss.usermodel.Font)14 ClientAnchor (org.apache.poi.ss.usermodel.ClientAnchor)12 FileOutputStream (java.io.FileOutputStream)10 RichTextString (org.apache.poi.ss.usermodel.RichTextString)10 Test (org.junit.Test)9 Comment (org.apache.poi.ss.usermodel.Comment)8 Hyperlink (org.apache.poi.ss.usermodel.Hyperlink)8 IOException (java.io.IOException)7 Drawing (org.apache.poi.ss.usermodel.Drawing)5 OutputStream (java.io.OutputStream)4 ArrayList (java.util.ArrayList)4 SXSSFWorkbook (org.apache.poi.xssf.streaming.SXSSFWorkbook)4 HSSFWorkbook (org.apache.poi.hssf.usermodel.HSSFWorkbook)3