Java Code Examples for org.apache.poi.hssf.usermodel.HSSFRichTextString

The following examples show how to use org.apache.poi.hssf.usermodel.HSSFRichTextString. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: autopoi   Source File: CellValueHelper.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * 03版本复杂数据
 * 
 * @param rich
 * @return
 */
private String getHSSFRichString(HSSFRichTextString rich) {
	int nums = rich.numFormattingRuns();
	StringBuilder sb = new StringBuilder();
	String text = rich.toString();
	int currentIndex = 0;
	sb.append(text.substring(0, rich.getIndexOfFormattingRun(0)));
	for (int i = 0; i < nums; i++) {
		sb.append("<span ");
		sb.append("class='font_" + rich.getFontOfFormattingRun(i));
		sb.append("_");
		sb.append(cssRandom);
		sb.append("'>");
		currentIndex = rich.getIndexOfFormattingRun(i);
		if (i < nums - 1) {
			sb.append(XmlEscapers.xmlContentEscaper().escape(text.substring(currentIndex, rich.getIndexOfFormattingRun(i + 1))));
		} else {
			sb.append(XmlEscapers.xmlContentEscaper().escape(text.substring(currentIndex, text.length())));
		}
		sb.append("</span>");
	}
	return sb.toString();
}
 
Example 2
Source Project: jeasypoi   Source File: CellValueHelper.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * 03版本复杂数据
 * 
 * @param rich
 * @return
 */
private String getHSSFRichString(HSSFRichTextString rich) {
	int nums = rich.numFormattingRuns();
	StringBuilder sb = new StringBuilder();
	String text = rich.toString();
	int currentIndex = 0;
	sb.append(text.substring(0, rich.getIndexOfFormattingRun(0)));
	for (int i = 0; i < nums; i++) {
		sb.append("<span ");
		sb.append("class='font_" + rich.getFontOfFormattingRun(i));
		sb.append("_");
		sb.append(cssRandom);
		sb.append("'>");
		currentIndex = rich.getIndexOfFormattingRun(i);
		if (i < nums - 1) {
			sb.append(XmlEscapers.xmlContentEscaper().escape(text.substring(currentIndex, rich.getIndexOfFormattingRun(i + 1))));
		} else {
			sb.append(XmlEscapers.xmlContentEscaper().escape(text.substring(currentIndex, text.length())));
		}
		sb.append("</span>");
	}
	return sb.toString();
}
 
Example 3
Source Project: easypoi   Source File: CellValueHelper.java    License: Apache License 2.0 6 votes vote down vote up
public String getHtmlValue(Cell cell) {
    if (Cell.CELL_TYPE_BOOLEAN == cell.getCellType()
        || Cell.CELL_TYPE_NUMERIC == cell.getCellType()) {
        cell.setCellType(Cell.CELL_TYPE_STRING);
        return cell.getStringCellValue();
    } else if (Cell.CELL_TYPE_STRING == cell.getCellType()) {
        if (cell.getRichStringCellValue().numFormattingRuns() == 0) {
            return XmlEscapers.xmlContentEscaper().escape(cell.getStringCellValue());
        } else if (is07) {
            return getXSSFRichString((XSSFRichTextString) cell.getRichStringCellValue());
        } else {
            return getHSSFRichString((HSSFRichTextString) cell.getRichStringCellValue());
        }
    }
    return "";
}
 
Example 4
Source Project: easypoi   Source File: CellValueHelper.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * 03版本复杂数据
 * @param rich
 * @return
 */
private String getHSSFRichString(HSSFRichTextString rich) {
    int nums = rich.numFormattingRuns();
    StringBuilder sb = new StringBuilder();
    String text = rich.toString();
    int currentIndex = 0;
    sb.append(text.substring(0, rich.getIndexOfFormattingRun(0)));
    for (int i = 0; i < nums; i++) {
        sb.append("<span ");
        sb.append("class='font_" + rich.getFontOfFormattingRun(i));
        sb.append("_");
        sb.append(cssRandom);
        sb.append("'>");
        currentIndex = rich.getIndexOfFormattingRun(i);
        if (i < nums - 1) {
            sb.append(XmlEscapers.xmlContentEscaper().escape(
                text.substring(currentIndex, rich.getIndexOfFormattingRun(i + 1))));
        } else {
            sb.append(XmlEscapers.xmlContentEscaper().escape(
                text.substring(currentIndex, text.length())));
        }
        sb.append("</span>");
    }
    return sb.toString();
}
 
Example 5
protected HSSFRichTextString getRichTextString(JRStyledText styledText, short forecolor, JRFont defaultFont, Locale locale)
{
	String text = styledText.getText();
	HSSFRichTextString richTextStr = new HSSFRichTextString(text);
	int runLimit = 0;
	AttributedCharacterIterator iterator = styledText.getAttributedString().getIterator();

	while(runLimit < styledText.length() && (runLimit = iterator.getRunLimit()) <= styledText.length())
	{
		Map<Attribute,Object> attributes = iterator.getAttributes();
		JRFont runFont = attributes.isEmpty()? defaultFont : new JRBaseFont(attributes);
		short runForecolor = attributes.get(TextAttribute.FOREGROUND) != null ? 
				getWorkbookColor((Color)attributes.get(TextAttribute.FOREGROUND)).getIndex() :
				forecolor;
		HSSFFont font = getLoadedFont(runFont, runForecolor, attributes, locale);
		richTextStr.applyFont(iterator.getIndex(), runLimit, font);
		iterator.setIndex(runLimit);
	}
	return richTextStr;
}
 
Example 6
protected HSSFRichTextString getRichTextString(JRStyledText styledText, short forecolor, JRFont defaultFont, Locale locale) {
	String text = styledText.getText();
	HSSFRichTextString richTextStr = new HSSFRichTextString(text);
	int runLimit = 0;
	AttributedCharacterIterator iterator = styledText.getAttributedString().getIterator();

	while(runLimit < styledText.length() && (runLimit = iterator.getRunLimit()) <= styledText.length()) {
		Map<Attribute,Object> attributes = iterator.getAttributes();
		JRFont runFont = attributes.isEmpty()? defaultFont : new JRBaseFont(attributes);
		short runForecolor = attributes.get(TextAttribute.FOREGROUND) != null  
			? getWorkbookColor((Color)attributes.get(TextAttribute.FOREGROUND)).getIndex() 
			: forecolor;
		HSSFFont font = getLoadedFont(runFont, runForecolor, attributes, locale);
		richTextStr.applyFont(iterator.getIndex(), runLimit, font);
		iterator.setIndex(runLimit);
	}
	return richTextStr;
}
 
Example 7
Source Project: ermasterr   Source File: POIUtils.java    License: Apache License 2.0 6 votes vote down vote up
public static Integer findColumn(final HSSFRow row, final String str) {
    for (int colNum = row.getFirstCellNum(); colNum <= row.getLastCellNum(); colNum++) {
        final HSSFCell cell = row.getCell(colNum);

        if (cell == null) {
            continue;
        }

        if (cell.getCellType() == Cell.CELL_TYPE_STRING) {
            final HSSFRichTextString cellValue = cell.getRichStringCellValue();

            if (str.equals(cellValue.getString())) {
                return Integer.valueOf(colNum);
            }
        }
    }

    return null;
}
 
Example 8
Source Project: ermasterr   Source File: POIUtils.java    License: Apache License 2.0 6 votes vote down vote up
public static Integer findMatchColumn(final HSSFRow row, final String str) {
    for (int colNum = row.getFirstCellNum(); colNum <= row.getLastCellNum(); colNum++) {
        final HSSFCell cell = row.getCell(colNum);

        if (cell == null) {
            continue;
        }

        if (cell.getCellType() != Cell.CELL_TYPE_STRING) {
            continue;
        }

        final HSSFRichTextString cellValue = cell.getRichStringCellValue();

        if (cellValue.getString().matches(str)) {
            return Integer.valueOf(colNum);
        }
    }

    return null;
}
 
Example 9
Source Project: ermasterr   Source File: POIUtils.java    License: Apache License 2.0 6 votes vote down vote up
public static CellLocation findCell(final HSSFSheet sheet, final String str, final int colNum) {
    for (int rowNum = sheet.getFirstRowNum(); rowNum < sheet.getLastRowNum() + 1; rowNum++) {
        final HSSFRow row = sheet.getRow(rowNum);
        if (row == null) {
            continue;
        }

        final HSSFCell cell = row.getCell(colNum);

        if (cell == null) {
            continue;
        }
        final HSSFRichTextString cellValue = cell.getRichStringCellValue();

        if (!Check.isEmpty(cellValue.getString())) {
            if (cellValue.getString().equals(str)) {
                return new CellLocation(rowNum, (short) colNum);
            }
        }
    }

    return null;
}
 
Example 10
Source Project: ermasterr   Source File: POIUtils.java    License: Apache License 2.0 6 votes vote down vote up
public static String getCellValue(final HSSFSheet sheet, final int r, final int c) {
    final HSSFRow row = sheet.getRow(r);

    if (row == null) {
        return null;
    }

    final HSSFCell cell = row.getCell(c);

    if (cell == null) {
        return null;
    }

    final HSSFRichTextString cellValue = cell.getRichStringCellValue();

    return cellValue.toString();
}
 
Example 11
Source Project: ermasterr   Source File: AbstractSheetGenerator.java    License: Apache License 2.0 6 votes vote down vote up
protected void setColumnData(final Map<String, String> keywordsValueMap, final ColumnTemplate columnTemplate, final HSSFRow row, final NormalColumn normalColumn, final TableView tableView, final int order) {

        for (final int columnNum : columnTemplate.columnTemplateMap.keySet()) {
            final HSSFCell cell = row.createCell(columnNum);
            final String template = columnTemplate.columnTemplateMap.get(columnNum);

            String value = null;
            if (KEYWORD_ORDER.equals(template)) {
                value = String.valueOf(order);

            } else {
                value = getColumnValue(keywordsValueMap, normalColumn, tableView, template);
            }

            try {
                final double num = Double.parseDouble(value);
                cell.setCellValue(num);

            } catch (final NumberFormatException e) {
                final HSSFRichTextString text = new HSSFRichTextString(value);
                cell.setCellValue(text);
            }
        }
    }
 
Example 12
Source Project: ermasterr   Source File: DBUnitXLSTestDataCreator.java    License: Apache License 2.0 6 votes vote down vote up
@Override
protected void writeDirectTestData(final ERTable table, final Map<NormalColumn, String> data, final String database) {
    final HSSFRow row = sheet.createRow(rowNum++);

    int col = 0;

    for (final NormalColumn column : table.getExpandedColumns()) {
        final HSSFCell cell = row.createCell(col++);

        final String value = Format.null2blank(data.get(column));

        if (value == null || "null".equals(value.toLowerCase())) {

        } else {
            cell.setCellValue(new HSSFRichTextString(value));
        }
    }
}
 
Example 13
Source Project: jeewx   Source File: ExcelExportOfTemplateUtil.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * 创建文本类型的Cell
 * 
 * @param row
 * @param index
 * @param text
 * @param style
 * @param entity
 * @param workbook
 */
private static void createStringCell(Row row, int index, String text,
		ExcelExportEntity entity, Workbook workbook) {
	Cell cell = row.createCell(index);
	switch (entity.getType()) {
	case 1:
		RichTextString Rtext = workbook instanceof HSSFWorkbook ? new HSSFRichTextString(
				text) : new XSSFRichTextString(text);
		cell.setCellValue(Rtext);
		break;
	case 2:
		cell.setCellType(Cell.CELL_TYPE_FORMULA);
		cell.setCellFormula(entity.getCellFormula());
		break;
	}
}
 
Example 14
Source Project: poi   Source File: TestExportExcel.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void exportExcelWithStyle() {
	try {
		String filePath = TestUtil.DOC_PATH + File.separator
				+ Globals.EXPORT_PRODUCT;
		OutputStream os = new FileOutputStream(filePath);
		HSSFWorkbook wb = new HSSFWorkbook();
		HSSFSheet sheet = wb.createSheet(Globals.SHEETNAME);
		HSSFRichTextString richString = new HSSFRichTextString(
				TestUtil.RICH_TEXT_STRING);
		HSSFFont font = wb.createFont();
		font.setColor(IndexedColors.BLUE.index);
		richString.applyFont(font);
		sheet.createRow(0).createCell(0).setCellValue(richString);
		wb.write(os);
		os.close();
	} catch (Exception e) {
		e.printStackTrace();
	}
}
 
Example 15
Source Project: nextreports-server   Source File: XlsResource.java    License: Apache License 2.0 6 votes vote down vote up
@Override
protected void printHeader(List<String> header, ByteArrayOutputStream out) {
	wb = new HSSFWorkbook();
       sheet = wb.createSheet("NextReports");

       HSSFRow headerRow = sheet.createRow(0);
       int col = 0;        
	if (header != null) {
		for (String s : header) {
			HSSFCell cell = headerRow.createCell(col);
			cell.setCellType(HSSFCell.CELL_TYPE_STRING);
			if (s == null) {
				s = "";
			}
			cell.setCellValue(new HSSFRichTextString(s));
			col++;
		}
	}		
}
 
Example 16
Source Project: ermaster-b   Source File: POIUtils.java    License: Apache License 2.0 6 votes vote down vote up
public static Integer findColumn(HSSFRow row, String str) {
	for (int colNum = row.getFirstCellNum(); colNum <= row.getLastCellNum(); colNum++) {
		HSSFCell cell = row.getCell(colNum);

		if (cell == null) {
			continue;
		}

		if (cell.getCellType() == HSSFCell.CELL_TYPE_STRING) {
			HSSFRichTextString cellValue = cell.getRichStringCellValue();

			if (str.equals(cellValue.getString())) {
				return Integer.valueOf(colNum);
			}
		}
	}

	return null;
}
 
Example 17
Source Project: ermaster-b   Source File: POIUtils.java    License: Apache License 2.0 6 votes vote down vote up
public static Integer findMatchColumn(HSSFRow row, String str) {
	for (int colNum = row.getFirstCellNum(); colNum <= row.getLastCellNum(); colNum++) {
		HSSFCell cell = row.getCell(colNum);

		if (cell == null) {
			continue;
		}

		if (cell.getCellType() != HSSFCell.CELL_TYPE_STRING) {
			continue;
		}

		HSSFRichTextString cellValue = cell.getRichStringCellValue();

		if (cellValue.getString().matches(str)) {
			return Integer.valueOf(colNum);
		}
	}

	return null;
}
 
Example 18
Source Project: ermaster-b   Source File: POIUtils.java    License: Apache License 2.0 6 votes vote down vote up
public static CellLocation findCell(HSSFSheet sheet, String str, int colNum) {
	for (int rowNum = sheet.getFirstRowNum(); rowNum < sheet
			.getLastRowNum() + 1; rowNum++) {
		HSSFRow row = sheet.getRow(rowNum);
		if (row == null) {
			continue;
		}

		HSSFCell cell = row.getCell(colNum);

		if (cell == null) {
			continue;
		}
		HSSFRichTextString cellValue = cell.getRichStringCellValue();

		if (!Check.isEmpty(cellValue.getString())) {
			if (cellValue.getString().equals(str)) {
				return new CellLocation(rowNum, (short) colNum);
			}
		}
	}

	return null;
}
 
Example 19
Source Project: ermaster-b   Source File: POIUtils.java    License: Apache License 2.0 6 votes vote down vote up
public static String getCellValue(HSSFSheet sheet, int r, int c) {
	HSSFRow row = sheet.getRow(r);
	
	if (row == null) {
		return null;
	}
	
	HSSFCell cell = row.getCell(c);
	
	if (cell == null) {
		return null;
	}
	
	HSSFRichTextString cellValue = cell.getRichStringCellValue();

	return cellValue.toString();
}
 
Example 20
Source Project: ermaster-b   Source File: DBUnitXLSTestDataCreator.java    License: Apache License 2.0 6 votes vote down vote up
@Override
protected void writeDirectTestData(ERTable table,
		Map<NormalColumn, String> data, String database) {
	HSSFRow row = this.sheet.createRow(this.rowNum++);

	int col = 0;

	for (NormalColumn column : table.getExpandedColumns()) {
		HSSFCell cell = row.createCell(col++);

		String value = Format.null2blank(data.get(column));

		if (value == null || "null".equals(value.toLowerCase())) {

		} else {
			cell.setCellValue(new HSSFRichTextString(value));
		}
	}
}
 
Example 21
Source Project: youkefu   Source File: ExcelExporterProcess.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * 构建头部
 */
private void createHead(){
	titleRow = sheet.createRow(rowNum);
	if(table!=null && table.getTableproperty()!=null){
		for(TableProperties tp : table.getTableproperty()){
			Cell cell2 = titleRow.createCell(table.getTableproperty().indexOf(tp)); 
			cell2.setCellStyle(firstStyle); 
			cell2.setCellValue(new HSSFRichTextString(tp.getName()));
		}
	}
	rowNum ++ ;
}
 
Example 22
Source Project: autopoi   Source File: CellValueHelper.java    License: Apache License 2.0 5 votes vote down vote up
public String getHtmlValue(Cell cell) {
	if (Cell.CELL_TYPE_BOOLEAN == cell.getCellType() || Cell.CELL_TYPE_NUMERIC == cell.getCellType()) {
		cell.setCellType(Cell.CELL_TYPE_STRING);
		return cell.getStringCellValue();
	} else if (Cell.CELL_TYPE_STRING == cell.getCellType()) {
		if (cell.getRichStringCellValue().numFormattingRuns() == 0) {
			return XmlEscapers.xmlContentEscaper().escape(cell.getStringCellValue());
		} else if (is07) {
			return getXSSFRichString((XSSFRichTextString) cell.getRichStringCellValue());
		} else {
			return getHSSFRichString((HSSFRichTextString) cell.getRichStringCellValue());
		}
	}
	return "";
}
 
Example 23
Source Project: jeasypoi   Source File: CellValueHelper.java    License: Apache License 2.0 5 votes vote down vote up
public String getHtmlValue(Cell cell) {
	if (Cell.CELL_TYPE_BOOLEAN == cell.getCellType() || Cell.CELL_TYPE_NUMERIC == cell.getCellType()) {
		cell.setCellType(Cell.CELL_TYPE_STRING);
		return cell.getStringCellValue();
	} else if (Cell.CELL_TYPE_STRING == cell.getCellType()) {
		if (cell.getRichStringCellValue().numFormattingRuns() == 0) {
			return XmlEscapers.xmlContentEscaper().escape(cell.getStringCellValue());
		} else if (is07) {
			return getXSSFRichString((XSSFRichTextString) cell.getRichStringCellValue());
		} else {
			return getHSSFRichString((HSSFRichTextString) cell.getRichStringCellValue());
		}
	}
	return "";
}
 
Example 24
Source Project: lams   Source File: TextObjectRecord.java    License: GNU General Public License v2.0 5 votes vote down vote up
private static void processFontRuns(RecordInputStream in, HSSFRichTextString str,
		int formattingRunDataLength) {
	if (formattingRunDataLength % FORMAT_RUN_ENCODED_SIZE != 0) {
		throw new RecordFormatException("Bad format run data length " + formattingRunDataLength
				+ ")");
	}
	int nRuns = formattingRunDataLength / FORMAT_RUN_ENCODED_SIZE;
	for (int i = 0; i < nRuns; i++) {
		short index = in.readShort();
		short iFont = in.readShort();
		in.readInt(); // skip reserved.
		str.applyFont(index, str.length(), iFont);
	}
}
 
Example 25
Source Project: lams   Source File: TextObjectRecord.java    License: GNU General Public License v2.0 5 votes vote down vote up
private static void writeFormatData(ContinuableRecordOutput out , HSSFRichTextString str) {
	int nRuns = str.numFormattingRuns();
	for (int i = 0; i < nRuns; i++) {
		out.writeShort(str.getIndexOfFormattingRun(i));
		int fontIndex = str.getFontOfFormattingRun(i);
		out.writeShort(fontIndex == HSSFRichTextString.NO_FONT ? 0 : fontIndex);
		out.writeInt(0); // skip reserved
	}
	out.writeShort(str.length());
	out.writeShort(0);
	out.writeInt(0); // skip reserved
}
 
Example 26
Source Project: myexcel   Source File: AbstractExcelFactory.java    License: Apache License 2.0 5 votes vote down vote up
private void doSetInnerSpan(Cell cell, Td td) {
    if (td.getFonts() == null || td.getFonts().isEmpty()) {
        return;
    }
    RichTextString richText = isHssf ? new HSSFRichTextString(td.getContent()) : new XSSFRichTextString(td.getContent());
    for (com.github.liaochong.myexcel.core.parser.Font font : td.getFonts()) {
        Font f = FontStyle.getFont(font.getStyle(), fontMap, () -> workbook.createFont(), customColor);
        richText.applyFont(font.getStartIndex(), font.getEndIndex(), f);
    }
    cell.setCellValue(richText);
}
 
Example 27
/**
 * Writes the header column names
 */
@Override
protected void writeReportHeader() throws JRException {
	row = sheet.createRow(0);
	for(int i = 0; i< columnNames.size(); i++) {
		String columnName = columnNames.get(i);
		cell = row.createCell(i);
		cell.setCellType(CellType.STRING);
		cell.setCellValue(new HSSFRichTextString(columnName));
	}
}
 
Example 28
Source Project: ermasterr   Source File: POIUtils.java    License: Apache License 2.0 5 votes vote down vote up
public static String getCellValue(final HSSFSheet sheet, final CellLocation location) {
    final HSSFRow row = sheet.getRow(location.r);
    final HSSFCell cell = row.getCell(location.c);

    final HSSFRichTextString cellValue = cell.getRichStringCellValue();

    return cellValue.toString();
}
 
Example 29
Source Project: ermasterr   Source File: POIUtils.java    License: Apache License 2.0 5 votes vote down vote up
public static void setCellValue(final HSSFSheet sheet, final CellLocation location, final String value) {
    final HSSFRow row = sheet.getRow(location.r);
    final HSSFCell cell = row.getCell(location.c);

    final HSSFRichTextString text = new HSSFRichTextString(value);
    cell.setCellValue(text);
}
 
Example 30
Source Project: ermasterr   Source File: DBUnitXLSTestDataCreator.java    License: Apache License 2.0 5 votes vote down vote up
@Override
protected void writeTableHeader(final ERDiagram diagram, final ERTable table) {
    final String sheetName = table.getPhysicalName();
    sheet = workbook.createSheet(sheetName);

    rowNum = 0;
    final HSSFRow row = sheet.createRow(rowNum++);

    int col = 0;

    for (final NormalColumn column : table.getExpandedColumns()) {
        final HSSFCell cell = row.createCell(col++);
        cell.setCellValue(new HSSFRichTextString(column.getPhysicalName()));
    }
}