use of org.apache.poi.ss.usermodel.Comment in project poi by apache.
the class TestXSSFSheet method addComments.
private void addComments(CreationHelper helper, Sheet sheet) {
Drawing<?> drawing = sheet.createDrawingPatriarch();
for (int i = 0; i < 2; i++) {
ClientAnchor anchor = helper.createClientAnchor();
anchor.setCol1(0);
anchor.setRow1(0 + i);
anchor.setCol2(2);
anchor.setRow2(3 + i);
Comment comment = drawing.createCellComment(anchor);
comment.setString(helper.createRichTextString("BugTesting"));
Row row = sheet.getRow(0 + i);
if (row == null) {
row = sheet.createRow(0 + i);
}
Cell cell = row.getCell(0);
if (cell == null) {
cell = row.createCell(0);
}
cell.setCellComment(comment);
}
}
use of org.apache.poi.ss.usermodel.Comment in project poi by apache.
the class XSSFExcelExtractor method getText.
/**
* Retrieves the text contents of the file
*/
public String getText() {
DataFormatter formatter;
if (locale == null) {
formatter = new DataFormatter();
} else {
formatter = new DataFormatter(locale);
}
StringBuffer text = new StringBuffer();
for (Sheet sh : workbook) {
XSSFSheet sheet = (XSSFSheet) sh;
if (includeSheetNames) {
text.append(sheet.getSheetName()).append("\n");
}
// Header(s), if present
if (includeHeadersFooters) {
text.append(extractHeaderFooter(sheet.getFirstHeader()));
text.append(extractHeaderFooter(sheet.getOddHeader()));
text.append(extractHeaderFooter(sheet.getEvenHeader()));
}
// Rows and cells
for (Object rawR : sheet) {
Row row = (Row) rawR;
for (Iterator<Cell> ri = row.cellIterator(); ri.hasNext(); ) {
Cell cell = ri.next();
// Is it a formula one?
if (cell.getCellTypeEnum() == CellType.FORMULA) {
if (formulasNotResults) {
String contents = cell.getCellFormula();
checkMaxTextSize(text, contents);
text.append(contents);
} else {
if (cell.getCachedFormulaResultTypeEnum() == CellType.STRING) {
handleStringCell(text, cell);
} else {
handleNonStringCell(text, cell, formatter);
}
}
} else if (cell.getCellTypeEnum() == CellType.STRING) {
handleStringCell(text, cell);
} else {
handleNonStringCell(text, cell, formatter);
}
// Output the comment, if requested and exists
Comment comment = cell.getCellComment();
if (includeCellComments && comment != null) {
// Replace any newlines with spaces, otherwise it
// breaks the output
String commentText = comment.getString().getString().replace('\n', ' ');
checkMaxTextSize(text, commentText);
text.append(" Comment by ").append(comment.getAuthor()).append(": ").append(commentText);
}
if (ri.hasNext()) {
text.append("\t");
}
}
text.append("\n");
}
// add textboxes
if (includeTextBoxes) {
XSSFDrawing drawing = sheet.getDrawingPatriarch();
if (drawing != null) {
for (XSSFShape shape : drawing.getShapes()) {
if (shape instanceof XSSFSimpleShape) {
String boxText = ((XSSFSimpleShape) shape).getText();
if (boxText.length() > 0) {
text.append(boxText);
text.append('\n');
}
}
}
}
}
// Finally footer(s), if present
if (includeHeadersFooters) {
text.append(extractHeaderFooter(sheet.getFirstFooter()));
text.append(extractHeaderFooter(sheet.getOddFooter()));
text.append(extractHeaderFooter(sheet.getEvenFooter()));
}
}
return text.toString();
}
use of org.apache.poi.ss.usermodel.Comment in project poi by apache.
the class TestHSSFComment method testBug56380InsertTooManyComments.
@Test
public void testBug56380InsertTooManyComments() throws Exception {
HSSFWorkbook workbook = new HSSFWorkbook();
try {
Sheet sheet = workbook.createSheet();
Drawing<?> drawing = sheet.createDrawingPatriarch();
String comment = "c";
for (int rowNum = 0; rowNum < 258; rowNum++) {
sheet.createRow(rowNum);
}
// should still work, for some reason DrawingManager2.allocateShapeId() skips the first 1024...
for (int count = 1025; count < 65535; count++) {
int rowNum = count / 255;
int cellNum = count % 255;
Cell cell = sheet.getRow(rowNum).createCell(cellNum);
try {
Comment commentObj = insertComment(drawing, cell, comment + cellNum);
assertEquals(count, ((HSSFComment) commentObj).getNoteRecord().getShapeId());
} catch (IllegalArgumentException e) {
throw new IllegalArgumentException("While adding shape number " + count, e);
}
}
// this should now fail to insert
Row row = sheet.createRow(257);
Cell cell = row.createCell(0);
insertComment(drawing, cell, comment + 0);
} finally {
workbook.close();
}
}
use of org.apache.poi.ss.usermodel.Comment 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