Search in sources :

Example 6 with RichTextString

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

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

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

the class TestUnfixedBugs method testBug57294.

// When this is fixed, the test case should go to BaseTestXCell with 
// adjustments to use _testDataProvider to also verify this for XSSF
@Test
public void testBug57294() throws IOException {
    Workbook wb = SXSSFITestDataProvider.instance.createWorkbook();
    Sheet sheet = wb.createSheet();
    Row row = sheet.createRow(0);
    Cell cell = row.createCell(0);
    RichTextString str = new XSSFRichTextString("Test rich text string");
    str.applyFont(2, 4, (short) 0);
    assertEquals(3, str.numFormattingRuns());
    cell.setCellValue(str);
    Workbook wbBack = SXSSFITestDataProvider.instance.writeOutAndReadBack(wb);
    wb.close();
    // re-read after serializing and reading back
    Cell cellBack = wbBack.getSheetAt(0).getRow(0).getCell(0);
    assertNotNull(cellBack);
    RichTextString strBack = cellBack.getRichStringCellValue();
    assertNotNull(strBack);
    assertEquals(3, strBack.numFormattingRuns());
    assertEquals(0, strBack.getIndexOfFormattingRun(0));
    assertEquals(2, strBack.getIndexOfFormattingRun(1));
    assertEquals(4, strBack.getIndexOfFormattingRun(2));
    wbBack.close();
}
Also used : CTRow(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRow) Row(org.apache.poi.ss.usermodel.Row) RichTextString(org.apache.poi.ss.usermodel.RichTextString) Sheet(org.apache.poi.ss.usermodel.Sheet) Cell(org.apache.poi.ss.usermodel.Cell) SXSSFWorkbook(org.apache.poi.xssf.streaming.SXSSFWorkbook) Workbook(org.apache.poi.ss.usermodel.Workbook) Test(org.junit.Test)

Example 9 with RichTextString

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

the class TestXSSFCell method test47278.

/**
     *  Bug 47278 -  xsi:nil attribute for <t> tag caused Excel 2007 to fail to open workbook
     */
@Test
public void test47278() throws IOException {
    XSSFWorkbook wb = (XSSFWorkbook) _testDataProvider.createWorkbook();
    Sheet sheet = wb.createSheet();
    Row row = sheet.createRow(0);
    SharedStringsTable sst = wb.getSharedStringSource();
    assertEquals(0, sst.getCount());
    //case 1. cell.setCellValue(new XSSFRichTextString((String)null));
    Cell cell_0 = row.createCell(0);
    RichTextString str = new XSSFRichTextString((String) null);
    assertNull(str.getString());
    cell_0.setCellValue(str);
    assertEquals(0, sst.getCount());
    assertEquals(CellType.BLANK, cell_0.getCellTypeEnum());
    //case 2. cell.setCellValue((String)null);
    Cell cell_1 = row.createCell(1);
    cell_1.setCellValue((String) null);
    assertEquals(0, sst.getCount());
    assertEquals(CellType.BLANK, cell_1.getCellTypeEnum());
    wb.close();
}
Also used : SharedStringsTable(org.apache.poi.xssf.model.SharedStringsTable) Row(org.apache.poi.ss.usermodel.Row) RichTextString(org.apache.poi.ss.usermodel.RichTextString) Sheet(org.apache.poi.ss.usermodel.Sheet) CTCell(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCell) Cell(org.apache.poi.ss.usermodel.Cell) BaseTestXCell(org.apache.poi.ss.usermodel.BaseTestXCell) Test(org.junit.Test)

Example 10 with RichTextString

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

the class TestHSSFComment method insertComment.

private Comment insertComment(Drawing<?> drawing, Cell cell, String message) {
    CreationHelper factory = cell.getSheet().getWorkbook().getCreationHelper();
    ClientAnchor anchor = factory.createClientAnchor();
    anchor.setCol1(cell.getColumnIndex());
    anchor.setCol2(cell.getColumnIndex() + 1);
    anchor.setRow1(cell.getRowIndex());
    anchor.setRow2(cell.getRowIndex() + 1);
    anchor.setDx1(100);
    anchor.setDx2(100);
    anchor.setDy1(100);
    anchor.setDy2(100);
    Comment comment = drawing.createCellComment(anchor);
    RichTextString str = factory.createRichTextString(message);
    comment.setString(str);
    comment.setAuthor("fanfy");
    cell.setCellComment(comment);
    return comment;
}
Also used : BaseTestCellComment(org.apache.poi.ss.usermodel.BaseTestCellComment) Comment(org.apache.poi.ss.usermodel.Comment) ClientAnchor(org.apache.poi.ss.usermodel.ClientAnchor) CreationHelper(org.apache.poi.ss.usermodel.CreationHelper) RichTextString(org.apache.poi.ss.usermodel.RichTextString)

Aggregations

RichTextString (org.apache.poi.ss.usermodel.RichTextString)11 Cell (org.apache.poi.ss.usermodel.Cell)7 Sheet (org.apache.poi.ss.usermodel.Sheet)7 Row (org.apache.poi.ss.usermodel.Row)6 Workbook (org.apache.poi.ss.usermodel.Workbook)5 FileOutputStream (java.io.FileOutputStream)4 CreationHelper (org.apache.poi.ss.usermodel.CreationHelper)4 Test (org.junit.Test)4 ClientAnchor (org.apache.poi.ss.usermodel.ClientAnchor)3 Comment (org.apache.poi.ss.usermodel.Comment)3 Font (org.apache.poi.ss.usermodel.Font)3 OutputStream (java.io.OutputStream)2 ArrayList (java.util.ArrayList)2 BaseTestCellComment (org.apache.poi.ss.usermodel.BaseTestCellComment)2 CellStyle (org.apache.poi.ss.usermodel.CellStyle)2 CellType (org.apache.poi.ss.usermodel.CellType)2 SXSSFWorkbook (org.apache.poi.xssf.streaming.SXSSFWorkbook)2 XSSFWorkbook (org.apache.poi.xssf.usermodel.XSSFWorkbook)2 SpreadsheetUtils.getAdjustedList (com.supercilex.robotscouter.data.client.spreadsheet.SpreadsheetUtils.getAdjustedList)1 SpreadsheetUtils.getStringForCell (com.supercilex.robotscouter.data.client.spreadsheet.SpreadsheetUtils.getStringForCell)1