在Aspose-Cells中的一个单元格中应用多个样式

我想在一个单元格中添加多个样式。

EX: 想要 一些帮助

现在我一次没有全文。 我一次只有一个块(比如“I”,“Want”,“Some”,“Help”和相关的样式)。 但我需要设置格式化在一个单元格中的整个string。

我如何使用Aspose.cells和Java来做到这一点?

您可以获取所选字符的FontSetting对象,然后更改样式。 Aspose文档的参考文献是http://goo.gl/GhtDDy

在API中编辑 setValue()方法将设置完整的值。 在你的情况下,你有相关风格的块。 理想情况下,应该有像appendValue(String,Style)这样的方法,但是这种方法在Aspose.Cells库中不存在。 请在Aspose论坛中请求此function。

检查下面的方法,你可以有有限的风格,只有字体设置适用于你的scheme,与当前的API。

我假设你有一个string的数组列表(数值块)和样式的数组列表(每个块的关联样式)。 分隔符可以是一个空格。

public static void main(String[] args) throws Exception { // Instantiating a Workbook object Workbook workbook = new Workbook(); // Accessing the added worksheet in the Excel file Worksheet worksheet = workbook.getWorksheets().get(0); Cells cells = worksheet.getCells(); ArrayList<String> values = new ArrayList<String>(); ArrayList<Style> styles = new ArrayList<Style>(); // Separator character String separator = " "; // I values.add("I"); styles.add(new Style()); styles.get(0).getFont().setBold(true); // Want values.add("Want"); styles.add(new Style()); styles.get(1).getFont().setBold(false); // Some values.add("Some"); styles.add(new Style()); styles.get(2).getFont().setBold(true); // Help values.add("Help"); styles.add(new Style()); styles.get(3).getFont().setBold(false); // Get cell A1 Cell cell = cells.get("A1"); appendValuesWithStyles(cell, values, styles, separator); workbook.save(Common.DATA_DIR + "cellstyle.xlsx"); } private static void appendValuesWithStyles(Cell cell, ArrayList<String> values, ArrayList<Style> styles, String separator) { // Lets combine all chunks, because we can only use setValue() String allCharacters = ""; // First set the whole value in cell int iValue = 0; for (String value : values) { allCharacters = allCharacters + value; if (iValue < values.size()) allCharacters = allCharacters + separator; iValue++; } // Set the value once cell.setValue(allCharacters); // Now set the styles int startIndex = 0, valueLength = 0; for (int iStyle = 0 ; iStyle < styles.size() ; iStyle++) { // Get the associated value and the style. String value = values.get(iStyle); Style style = styles.get(iStyle); // We need the start character and length of string to set the style valueLength = value.length(); cell.characters(startIndex, valueLength).getFont().setBold(style.getFont().isBold()); // Increment the start index startIndex = startIndex + valueLength + separator.length(); } }