【问题标题】:How to set bold text within spreadsheet cell using C# and LibreOffice/OpenOffice?如何使用 C# 和 LibreOffice/OpenOffice 在电子表格单元格中设置粗体文本?
【发布时间】:2013-12-18 22:55:26
【问题描述】:

我正在将我们的一个内部应用程序转换为使用 OpenOffice/LibreOffice 以及 MsOffice。

在使用 C#/.NET 的 Excel 电子表格中,仅将单元格中的部分文本设置为粗体相当简单。它看起来像这样:

sheet.Range["A1"].Characters[startIndex, length].Font.Bold = true;

您如何在 OO/LO 中进行等效操作?

【问题讨论】:

    标签: c# libreoffice openoffice-calc


    【解决方案1】:

    你看过 XML 文件格式吗? http://www.openoffice.org/xml/xml_specification.pdf

    我不确定打开 Excel,但在 MS-Excel 中,您可以定义字体并附加它。

    var fonts = new Fonts();
                var font = new DocumentFormat.OpenXml.Spreadsheet.Font();
                var fontName = new FontName {Val = StringValue.FromString("Arial")};
                var fontSize = new FontSize {Val = DoubleValue.FromDouble(11)};
                font.FontName = fontName;
                font.FontSize = fontSize;
                fonts.Append(font);
                var cellFormats = new CellFormats();
                         cellFormats.Append(fonts);
    

    【讨论】:

    • 这不是我真正想要做的。将格式应用于整个单元格很容易。我想要做的是有一个单元格,单元格内的粗体状态会发生变化。类似“这个 word 是粗体的”。你可以手动完成,我只是不知道如何编码。
    【解决方案2】:

    我不确定这是否是最有效的方法,但它确实有效。基本思想是使用示例代码将文本插入单元格,然后在所需的子范围上创建光标并在子范围上设置 CharWeight 属性。

    var xCellText = (unoidl.com.sun.star.text.XText)cell;
    var xTextCursor = xCellText.createTextCursor();
    xCellText.insertString(xTextCursor, newLine, false);
    xTextCursor.gotoStart(false);
    xTextCursor.goRight((short)boldStartIndex, false);
    xTextCursor.goRight((short)boldLength, true);
    var xPropSet = (XPropertySet)xTextCursor;
    xPropSet.setPropertyValue("CharWeight", new uno.Any(unoidl.com.sun.star.awt.FontWeight.BOLD));
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-12-24
      • 2013-02-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-29
      相关资源
      最近更新 更多