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