Search in sources :

Example 1 with RichTextString

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

the class SXSSFCell method getRichStringCellValue.

/**
     * Get the value of the cell as a XSSFRichTextString
     * <p>
     * For numeric cells we throw an exception. For blank cells we return an empty string.
     * For formula cells we return the pre-calculated value if a string, otherwise an exception.
     * </p>
     * @return the value of the cell as a XSSFRichTextString
     */
@Override
public RichTextString getRichStringCellValue() {
    CellType cellType = getCellTypeEnum();
    if (getCellTypeEnum() != CellType.STRING)
        throw typeMismatch(CellType.STRING, cellType, false);
    StringValue sval = (StringValue) _value;
    if (sval.isRichText())
        return ((RichTextValue) _value).getValue();
    else {
        String plainText = getStringCellValue();
        return getSheet().getWorkbook().getCreationHelper().createRichTextString(plainText);
    }
}
Also used : CellType(org.apache.poi.ss.usermodel.CellType) XSSFRichTextString(org.apache.poi.xssf.usermodel.XSSFRichTextString) RichTextString(org.apache.poi.ss.usermodel.RichTextString)

Example 2 with RichTextString

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

the class TestXSSFComment method testBug58175a.

@Ignore("Used for manual testing with opening the resulting Workbook in Excel")
@Test
public void testBug58175a() throws IOException {
    Workbook wb = new SXSSFWorkbook();
    try {
        Sheet sheet = wb.createSheet();
        Row row = sheet.createRow(1);
        Cell cell = row.createCell(3);
        cell.setCellValue("F4");
        Drawing<?> drawing = sheet.createDrawingPatriarch();
        CreationHelper factory = wb.getCreationHelper();
        // When the comment box is visible, have it show in a 1x3 space
        ClientAnchor anchor = factory.createClientAnchor();
        anchor.setCol1(cell.getColumnIndex());
        anchor.setCol2(cell.getColumnIndex() + 1);
        anchor.setRow1(row.getRowNum());
        anchor.setRow2(row.getRowNum() + 3);
        // Create the comment and set the text+author
        Comment comment = drawing.createCellComment(anchor);
        RichTextString str = factory.createRichTextString("Hello, World!");
        comment.setString(str);
        comment.setAuthor("Apache POI");
        /* fixed the problem as well 
             * comment.setColumn(cell.getColumnIndex());
             * comment.setRow(cell.getRowIndex());
             */
        // Assign the comment to the cell
        cell.setCellComment(comment);
        OutputStream out = new FileOutputStream("C:\\temp\\58175.xlsx");
        try {
            wb.write(out);
        } finally {
            out.close();
        }
    } finally {
        wb.close();
    }
}
Also used : BaseTestCellComment(org.apache.poi.ss.usermodel.BaseTestCellComment) Comment(org.apache.poi.ss.usermodel.Comment) CTComment(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTComment) ClientAnchor(org.apache.poi.ss.usermodel.ClientAnchor) SXSSFWorkbook(org.apache.poi.xssf.streaming.SXSSFWorkbook) CreationHelper(org.apache.poi.ss.usermodel.CreationHelper) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) Row(org.apache.poi.ss.usermodel.Row) RichTextString(org.apache.poi.ss.usermodel.RichTextString) HSSFRichTextString(org.apache.poi.hssf.usermodel.HSSFRichTextString) 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) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 3 with RichTextString

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

the class TestXSSFWorkbook method saveLoadNew.

/**
     * Tests that we can save, and then re-load a new document
     */
@Test
public void saveLoadNew() throws IOException, InvalidFormatException {
    XSSFWorkbook wb1 = new XSSFWorkbook();
    //check that the default date system is set to 1900
    CTWorkbookPr pr = wb1.getCTWorkbook().getWorkbookPr();
    assertNotNull(pr);
    assertTrue(pr.isSetDate1904());
    assertFalse("XSSF must use the 1900 date system", pr.getDate1904());
    Sheet sheet1 = wb1.createSheet("sheet1");
    Sheet sheet2 = wb1.createSheet("sheet2");
    wb1.createSheet("sheet3");
    RichTextString rts = wb1.getCreationHelper().createRichTextString("hello world");
    sheet1.createRow(0).createCell((short) 0).setCellValue(1.2);
    sheet1.createRow(1).createCell((short) 0).setCellValue(rts);
    sheet2.createRow(0);
    assertEquals(0, wb1.getSheetAt(0).getFirstRowNum());
    assertEquals(1, wb1.getSheetAt(0).getLastRowNum());
    assertEquals(0, wb1.getSheetAt(1).getFirstRowNum());
    assertEquals(0, wb1.getSheetAt(1).getLastRowNum());
    assertEquals(0, wb1.getSheetAt(2).getFirstRowNum());
    assertEquals(0, wb1.getSheetAt(2).getLastRowNum());
    File file = TempFile.createTempFile("poi-", ".xlsx");
    OutputStream out = new FileOutputStream(file);
    wb1.write(out);
    out.close();
    // Check the package contains what we'd expect it to
    OPCPackage pkg = OPCPackage.open(file.toString());
    PackagePart wbRelPart = pkg.getPart(PackagingURIHelper.createPartName("/xl/_rels/workbook.xml.rels"));
    assertNotNull(wbRelPart);
    assertTrue(wbRelPart.isRelationshipPart());
    assertEquals(ContentTypes.RELATIONSHIPS_PART, wbRelPart.getContentType());
    PackagePart wbPart = pkg.getPart(PackagingURIHelper.createPartName("/xl/workbook.xml"));
    // Links to the three sheets, shared strings and styles
    assertTrue(wbPart.hasRelationships());
    assertEquals(5, wbPart.getRelationships().size());
    wb1.close();
    // Load back the XSSFWorkbook
    @SuppressWarnings("resource") XSSFWorkbook wb2 = new XSSFWorkbook(pkg);
    assertEquals(3, wb2.getNumberOfSheets());
    assertNotNull(wb2.getSheetAt(0));
    assertNotNull(wb2.getSheetAt(1));
    assertNotNull(wb2.getSheetAt(2));
    assertNotNull(wb2.getSharedStringSource());
    assertNotNull(wb2.getStylesSource());
    assertEquals(0, wb2.getSheetAt(0).getFirstRowNum());
    assertEquals(1, wb2.getSheetAt(0).getLastRowNum());
    assertEquals(0, wb2.getSheetAt(1).getFirstRowNum());
    assertEquals(0, wb2.getSheetAt(1).getLastRowNum());
    assertEquals(0, wb2.getSheetAt(2).getFirstRowNum());
    assertEquals(0, wb2.getSheetAt(2).getLastRowNum());
    sheet1 = wb2.getSheetAt(0);
    assertEquals(1.2, sheet1.getRow(0).getCell(0).getNumericCellValue(), 0.0001);
    assertEquals("hello world", sheet1.getRow(1).getCell(0).getRichStringCellValue().getString());
    pkg.close();
}
Also used : ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) CTWorkbookPr(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTWorkbookPr) RichTextString(org.apache.poi.ss.usermodel.RichTextString) MemoryPackagePart(org.apache.poi.openxml4j.opc.internal.MemoryPackagePart) PackagePart(org.apache.poi.openxml4j.opc.PackagePart) Sheet(org.apache.poi.ss.usermodel.Sheet) TempFile(org.apache.poi.util.TempFile) File(java.io.File) OPCPackage(org.apache.poi.openxml4j.opc.OPCPackage) Test(org.junit.Test)

Example 4 with RichTextString

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

the class Excel2007Writer method witerTmxTU.

public void witerTmxTU(List<TmxTU> tus, boolean writeFullText) throws IOException {
    if (null == tus || tus.isEmpty()) {
        return;
    }
    TmxTU tu = null;
    List<TmxSegement> temp = new ArrayList<TmxSegement>(5);
    int i = 0;
    for (; i < tus.size(); i++) {
        temp.clear();
        tu = tus.get(i);
        if (tu.getSource() != null) {
            temp.add(tu.getSource());
        }
        if (tu.getSegments() != null) {
            temp.addAll(tu.getSegments());
        }
        Row row = sh.createRow((lastIndex + i));
        // 为每一行添加数据
        for (TmxSegement segment : temp) {
            int cellIndex = getLangIndex(rowHeader, segment);
            if (-1 == cellIndex) {
                cellIndex = addLangCell(rowHeader, segment);
            }
            RichTextString createRichTextString = null;
            if (writeFullText) {
                createRichTextString = createHelper.createRichTextString(segment.getFullText());
            } else {
                createRichTextString = createHelper.createRichTextString(segment.getPureText());
            }
            Cell createCell = row.createCell(cellIndex);
            createCell.setCellStyle(getWrapedCell());
            createCell.setCellValue(createRichTextString);
        }
    }
    lastIndex = lastIndex + i;
    // :使用固定的列宽
    // 设置宽度:此处比较耗时
    // for(Cell cell : rowHeader){
    // sheet.autoSizeColumn(cell.getColumnIndex());
    // }
    tus.clear();
}
Also used : TmxTU(net.heartsome.cat.common.bean.TmxTU) ArrayList(java.util.ArrayList) Row(org.apache.poi.ss.usermodel.Row) RichTextString(org.apache.poi.ss.usermodel.RichTextString) TmxSegement(net.heartsome.cat.common.bean.TmxSegement) Cell(org.apache.poi.ss.usermodel.Cell)

Example 5 with RichTextString

use of org.apache.poi.ss.usermodel.RichTextString in project Robot-Scouter by SUPERCILEX.

the class SpreadsheetExporter method setRowValue.

private void setRowValue(int column, Metric metric, Row row) {
    row.getCell(0, MissingCellPolicy.CREATE_NULL_AS_BLANK).setCellValue(metric.getName());
    Cell valueCell = row.getCell(column, MissingCellPolicy.CREATE_NULL_AS_BLANK);
    switch(metric.getType()) {
        case BOOLEAN:
            valueCell.setCellValue((boolean) metric.getValue());
            break;
        case NUMBER:
            Metric.Number numberMetric = (Metric.Number) metric;
            valueCell.setCellValue(numberMetric.getValue());
            String unit = numberMetric.getUnit();
            if (!TextUtils.isEmpty(unit)) {
                mCache.setCellFormat(valueCell, "#0\"" + unit + "\"");
            }
            break;
        case LIST:
            Metric.List listMetric = (Metric.List) metric;
            String selectedItem = listMetric.getValue().get(listMetric.getSelectedValueKey());
            valueCell.setCellValue(selectedItem);
            break;
        case TEXT:
            RichTextString note = mCache.getCreationHelper().createRichTextString(String.valueOf(metric.getValue()));
            valueCell.setCellValue(note);
            break;
        case STOPWATCH:
            List<? extends Long> cycles = ((Metric.Stopwatch) metric).getValue();
            long sum = 0;
            for (Long duration : cycles) sum += duration;
            long average = cycles.isEmpty() ? 0 : sum / cycles.size();
            valueCell.setCellValue(TimeUnit.MILLISECONDS.toSeconds(average));
            mCache.setCellFormat(valueCell, "#0\"s\"");
            break;
        case HEADER:
            // Headers are skipped because they don't contain any data
            break;
        default:
            throw new IllegalStateException();
    }
}
Also used : RichTextString(org.apache.poi.ss.usermodel.RichTextString) RichTextString(org.apache.poi.ss.usermodel.RichTextString) Metric(com.supercilex.robotscouter.data.model.Metric) ArrayList(java.util.ArrayList) SpreadsheetUtils.getAdjustedList(com.supercilex.robotscouter.data.client.spreadsheet.SpreadsheetUtils.getAdjustedList) List(java.util.List) Cell(org.apache.poi.ss.usermodel.Cell) SpreadsheetUtils.getStringForCell(com.supercilex.robotscouter.data.client.spreadsheet.SpreadsheetUtils.getStringForCell)

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