Search in sources :

Example 6 with HSSFRichTextString

use of org.apache.poi.hssf.usermodel.HSSFRichTextString in project poi by apache.

the class TestSheet method testCreateAggregate.

@Test
public void testCreateAggregate() {
    String msoDrawingRecord1 = "0F 00 02 F0 20 01 00 00 10 00 08 F0 08 00 00 00 \n" + "03 00 00 00 02 04 00 00 0F 00 03 F0 08 01 00 00 \n" + "0F 00 04 F0 28 00 00 00 01 00 09 F0 10 00 00 00 \n" + "00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 \n" + "02 00 0A F0 08 00 00 00 00 04 00 00 05 00 00 00 \n" + "0F 00 04 F0 64 00 00 00 42 01 0A F0 08 00 00 00 \n" + "01 04 00 00 00 0A 00 00 73 00 0B F0 2A 00 00 00 \n" + "BF 00 08 00 08 00 44 01 04 00 00 00 7F 01 00 00 \n" + "01 00 BF 01 00 00 11 00 C0 01 40 00 00 08 FF 01 \n" + "10 00 10 00 BF 03 00 00 08 00 00 00 10 F0 12 00 \n" + "00 00 00 00 01 00 54 00 05 00 45 00 01 00 88 03 \n" + "05 00 94 00 00 00 11 F0 00 00 00 00";
    String msoDrawingRecord2 = "0F 00 04 F0 64 00 00 00 42 01 0A F0 08 00 00 00 " + "02 04 00 00 80 0A 00 00 73 00 0B F0 2A 00 00 00 " + "BF 00 08 00 08 00 44 01 04 00 00 00 7F 01 00 00 " + "01 00 BF 01 00 00 11 00 C0 01 40 00 00 08 FF 01 " + "10 00 10 00 BF 03 00 00 08 00 00 00 10 F0 12 00 " + "00 00 00 00 01 00 8D 03 05 00 E4 00 03 00 4D 03 " + "0B 00 0C 00 00 00 11 F0 00 00 00 00";
    DrawingRecord d1 = new DrawingRecord();
    d1.setData(HexRead.readFromString(msoDrawingRecord1));
    ObjRecord r1 = new ObjRecord();
    DrawingRecord d2 = new DrawingRecord();
    d2.setData(HexRead.readFromString(msoDrawingRecord2));
    TextObjectRecord r2 = new TextObjectRecord();
    r2.setStr(new HSSFRichTextString("Aggregated"));
    NoteRecord n2 = new NoteRecord();
    List<Record> recordStream = new ArrayList<Record>();
    recordStream.add(InternalSheet.createBOF());
    recordStream.add(d1);
    recordStream.add(r1);
    recordStream.add(createWindow2Record());
    recordStream.add(EOFRecord.instance);
    confirmAggregatedRecords(recordStream);
    recordStream = new ArrayList<Record>();
    recordStream.add(InternalSheet.createBOF());
    recordStream.add(d1);
    recordStream.add(r1);
    recordStream.add(d2);
    recordStream.add(r2);
    recordStream.add(createWindow2Record());
    recordStream.add(EOFRecord.instance);
    confirmAggregatedRecords(recordStream);
    recordStream = new ArrayList<Record>();
    recordStream.add(InternalSheet.createBOF());
    recordStream.add(d1);
    recordStream.add(r1);
    recordStream.add(d2);
    recordStream.add(r2);
    recordStream.add(n2);
    recordStream.add(createWindow2Record());
    recordStream.add(EOFRecord.instance);
    confirmAggregatedRecords(recordStream);
}
Also used : TextObjectRecord(org.apache.poi.hssf.record.TextObjectRecord) HSSFRichTextString(org.apache.poi.hssf.usermodel.HSSFRichTextString) NoteRecord(org.apache.poi.hssf.record.NoteRecord) ArrayList(java.util.ArrayList) ObjRecord(org.apache.poi.hssf.record.ObjRecord) StringRecord(org.apache.poi.hssf.record.StringRecord) GutsRecord(org.apache.poi.hssf.record.GutsRecord) MergeCellsRecord(org.apache.poi.hssf.record.MergeCellsRecord) DrawingRecord(org.apache.poi.hssf.record.DrawingRecord) NoteRecord(org.apache.poi.hssf.record.NoteRecord) Record(org.apache.poi.hssf.record.Record) ObjRecord(org.apache.poi.hssf.record.ObjRecord) EOFRecord(org.apache.poi.hssf.record.EOFRecord) RowRecord(org.apache.poi.hssf.record.RowRecord) ColumnInfoRecord(org.apache.poi.hssf.record.ColumnInfoRecord) WindowTwoRecord(org.apache.poi.hssf.record.WindowTwoRecord) EscherDggRecord(org.apache.poi.ddf.EscherDggRecord) TextObjectRecord(org.apache.poi.hssf.record.TextObjectRecord) IndexRecord(org.apache.poi.hssf.record.IndexRecord) BOFRecord(org.apache.poi.hssf.record.BOFRecord) NumberRecord(org.apache.poi.hssf.record.NumberRecord) MulBlankRecord(org.apache.poi.hssf.record.MulBlankRecord) UncalcedRecord(org.apache.poi.hssf.record.UncalcedRecord) DimensionsRecord(org.apache.poi.hssf.record.DimensionsRecord) BlankRecord(org.apache.poi.hssf.record.BlankRecord) FormulaRecord(org.apache.poi.hssf.record.FormulaRecord) HSSFRichTextString(org.apache.poi.hssf.usermodel.HSSFRichTextString) DrawingRecord(org.apache.poi.hssf.record.DrawingRecord) Test(org.junit.Test)

Example 7 with HSSFRichTextString

use of org.apache.poi.hssf.usermodel.HSSFRichTextString in project poi by apache.

the class NumberComparingSpreadsheetGenerator method writeHeaderCell.

private static void writeHeaderCell(HSSFRow row, int i, String text, HSSFCellStyle style) {
    HSSFCell cell = row.createCell(i);
    cell.setCellValue(new HSSFRichTextString(text));
    cell.setCellStyle(style);
}
Also used : HSSFRichTextString(org.apache.poi.hssf.usermodel.HSSFRichTextString) HSSFCell(org.apache.poi.hssf.usermodel.HSSFCell)

Example 8 with HSSFRichTextString

use of org.apache.poi.hssf.usermodel.HSSFRichTextString in project poi by apache.

the class ExcelToFoConverter method processCell.

protected boolean processCell(HSSFWorkbook workbook, HSSFCell cell, Element tableCellElement, int normalWidthPx, int maxSpannedWidthPx, float normalHeightPt) {
    final HSSFCellStyle cellStyle = cell.getCellStyle();
    String value;
    switch(cell.getCellTypeEnum()) {
        case STRING:
            // XXX: enrich
            value = cell.getRichStringCellValue().getString();
            break;
        case FORMULA:
            switch(cell.getCachedFormulaResultTypeEnum()) {
                case STRING:
                    HSSFRichTextString str = cell.getRichStringCellValue();
                    if (str != null && str.length() > 0) {
                        value = (str.toString());
                    } else {
                        value = ExcelToHtmlUtils.EMPTY;
                    }
                    break;
                case NUMERIC:
                    double nValue = cell.getNumericCellValue();
                    short df = cellStyle.getDataFormat();
                    String dfs = cellStyle.getDataFormatString();
                    value = _formatter.formatRawCellContents(nValue, df, dfs);
                    break;
                case BOOLEAN:
                    value = Boolean.toString(cell.getBooleanCellValue());
                    break;
                case ERROR:
                    value = ErrorEval.getText(cell.getErrorCellValue());
                    break;
                default:
                    logger.log(POILogger.WARN, "Unexpected cell cachedFormulaResultType (" + cell.getCachedFormulaResultTypeEnum() + ")");
                    value = ExcelToHtmlUtils.EMPTY;
                    break;
            }
            break;
        case BLANK:
            value = ExcelToHtmlUtils.EMPTY;
            break;
        case NUMERIC:
            value = _formatter.formatCellValue(cell);
            break;
        case BOOLEAN:
            value = Boolean.toString(cell.getBooleanCellValue());
            break;
        case ERROR:
            value = ErrorEval.getText(cell.getErrorCellValue());
            break;
        default:
            logger.log(POILogger.WARN, "Unexpected cell type (" + cell.getCellTypeEnum() + ")");
            return true;
    }
    final boolean noText = ExcelToHtmlUtils.isEmpty(value);
    final boolean wrapInDivs = !noText && !cellStyle.getWrapText();
    final boolean emptyStyle = isEmptyStyle(cellStyle);
    if (!emptyStyle && noText) {
        /*
             * if cell style is defined (like borders, etc.) but cell text
             * is empty, add "&nbsp;" to output, so browser won't collapse
             * and ignore cell
             */
        value = " ";
    }
    if (isOutputLeadingSpacesAsNonBreaking() && value.startsWith(" ")) {
        StringBuilder builder = new StringBuilder();
        for (int c = 0; c < value.length(); c++) {
            if (value.charAt(c) != ' ') {
                break;
            }
            builder.append(' ');
        }
        if (value.length() != builder.length()) {
            builder.append(value.substring(builder.length()));
        }
        value = builder.toString();
    }
    Text text = foDocumentFacade.createText(value);
    Element block = foDocumentFacade.createBlock();
    if (wrapInDivs) {
        block.setAttribute("absolute-position", "fixed");
        block.setAttribute("left", "0px");
        block.setAttribute("top", "0px");
        block.setAttribute("bottom", "0px");
        block.setAttribute("min-width", normalWidthPx + "px");
        if (maxSpannedWidthPx != Integer.MAX_VALUE) {
            block.setAttribute("max-width", maxSpannedWidthPx + "px");
        }
        block.setAttribute("overflow", "hidden");
        block.setAttribute("height", normalHeightPt + "pt");
        block.setAttribute("keep-together.within-line", "always");
        block.setAttribute("wrap-option", "no-wrap");
    }
    processCellStyle(workbook, cell.getCellStyle(), tableCellElement, block);
    block.appendChild(text);
    tableCellElement.appendChild(block);
    return ExcelToHtmlUtils.isEmpty(value) && emptyStyle;
}
Also used : HSSFCellStyle(org.apache.poi.hssf.usermodel.HSSFCellStyle) HSSFRichTextString(org.apache.poi.hssf.usermodel.HSSFRichTextString) Element(org.w3c.dom.Element) Text(org.w3c.dom.Text) HSSFRichTextString(org.apache.poi.hssf.usermodel.HSSFRichTextString)

Example 9 with HSSFRichTextString

use of org.apache.poi.hssf.usermodel.HSSFRichTextString in project poi by apache.

the class TestDrawingShapes method testTextForSimpleShape.

@Test
public void testTextForSimpleShape() throws IOException {
    HSSFWorkbook wb1 = new HSSFWorkbook();
    HSSFSheet sheet = wb1.createSheet();
    HSSFPatriarch patriarch = sheet.createDrawingPatriarch();
    HSSFSimpleShape shape = patriarch.createSimpleShape(new HSSFClientAnchor());
    shape.setShapeType(HSSFSimpleShape.OBJECT_TYPE_RECTANGLE);
    EscherAggregate agg = HSSFTestHelper.getEscherAggregate(patriarch);
    assertEquals(agg.getShapeToObjMapping().size(), 2);
    HSSFWorkbook wb2 = HSSFTestDataSamples.writeOutAndReadBack(wb1);
    wb1.close();
    sheet = wb2.getSheetAt(0);
    patriarch = sheet.getDrawingPatriarch();
    shape = (HSSFSimpleShape) patriarch.getChildren().get(0);
    agg = HSSFTestHelper.getEscherAggregate(patriarch);
    assertEquals(agg.getShapeToObjMapping().size(), 2);
    shape.setString(new HSSFRichTextString("string1"));
    assertEquals(shape.getString().getString(), "string1");
    assertNotNull(HSSFTestHelper.getEscherContainer(shape).getChildById(EscherTextboxRecord.RECORD_ID));
    assertEquals(agg.getShapeToObjMapping().size(), 2);
    HSSFWorkbook wb3 = HSSFTestDataSamples.writeOutAndReadBack(wb2);
    wb2.close();
    HSSFWorkbook wb4 = HSSFTestDataSamples.writeOutAndReadBack(wb3);
    wb3.close();
    sheet = wb4.getSheetAt(0);
    patriarch = sheet.getDrawingPatriarch();
    shape = (HSSFSimpleShape) patriarch.getChildren().get(0);
    assertNotNull(HSSFTestHelper.getTextObjRecord(shape));
    assertEquals(shape.getString().getString(), "string1");
    assertNotNull(HSSFTestHelper.getEscherContainer(shape).getChildById(EscherTextboxRecord.RECORD_ID));
    assertEquals(agg.getShapeToObjMapping().size(), 2);
    wb4.close();
}
Also used : HSSFPatriarch(org.apache.poi.hssf.usermodel.HSSFPatriarch) EscherAggregate(org.apache.poi.hssf.record.EscherAggregate) HSSFRichTextString(org.apache.poi.hssf.usermodel.HSSFRichTextString) HSSFSimpleShape(org.apache.poi.hssf.usermodel.HSSFSimpleShape) HSSFClientAnchor(org.apache.poi.hssf.usermodel.HSSFClientAnchor) HSSFSheet(org.apache.poi.hssf.usermodel.HSSFSheet) HSSFWorkbook(org.apache.poi.hssf.usermodel.HSSFWorkbook) Test(org.junit.Test)

Example 10 with HSSFRichTextString

use of org.apache.poi.hssf.usermodel.HSSFRichTextString in project poi by apache.

the class TestTextObjectRecord method testClone.

/**
     * Test cloning
     */
public void testClone() {
    String text = "Hello, World";
    HSSFRichTextString str = new HSSFRichTextString(text);
    TextObjectRecord obj = new TextObjectRecord();
    obj.setStr(str);
    TextObjectRecord cloned = (TextObjectRecord) obj.clone();
    assertEquals(obj.getRecordSize(), cloned.getRecordSize());
    assertEquals(obj.getHorizontalTextAlignment(), cloned.getHorizontalTextAlignment());
    assertEquals(obj.getStr().getString(), cloned.getStr().getString());
    //finally check that the serialized data is the same
    byte[] src = obj.serialize();
    byte[] cln = cloned.serialize();
    assertArrayEquals(src, cln);
}
Also used : HSSFRichTextString(org.apache.poi.hssf.usermodel.HSSFRichTextString) HSSFRichTextString(org.apache.poi.hssf.usermodel.HSSFRichTextString)

Aggregations

HSSFRichTextString (org.apache.poi.hssf.usermodel.HSSFRichTextString)38 HSSFCellStyle (org.apache.poi.hssf.usermodel.HSSFCellStyle)17 HSSFCell (org.apache.poi.hssf.usermodel.HSSFCell)16 HSSFRow (org.apache.poi.hssf.usermodel.HSSFRow)9 HSSFSheet (org.apache.poi.hssf.usermodel.HSSFSheet)8 HSSFWorkbook (org.apache.poi.hssf.usermodel.HSSFWorkbook)7 HSSFClientAnchor (org.apache.poi.hssf.usermodel.HSSFClientAnchor)5 HSSFFont (org.apache.poi.hssf.usermodel.HSSFFont)5 HSSFPatriarch (org.apache.poi.hssf.usermodel.HSSFPatriarch)5 Test (org.junit.Test)5 HSSFSimpleShape (org.apache.poi.hssf.usermodel.HSSFSimpleShape)3 FileOutputStream (java.io.FileOutputStream)2 BigDecimal (java.math.BigDecimal)2 HSSFComment (org.apache.poi.hssf.usermodel.HSSFComment)2 Region (org.apache.poi.hssf.util.Region)2 Element (org.w3c.dom.Element)2 Text (org.w3c.dom.Text)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1