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