excel - 如何使用 openxml 在 Excel 文件单元格中的文本上应用字体和颜色

标签 excel fonts openxml openxml-sdk

我是 Openxml 新手。我正在尝试使用 openxml 创建 xlsx 文件。我想对Excel文件中不同单元格的文本应用不同的字体和颜色。我使用它来创建 xlsx 文件,但无法执行字体和颜色部分。

SpreadsheetDocument spreadsheetDocument = SpreadsheetDocument.Create("E:\\Word9.xlsx", SpreadsheetDocumentType.Workbook);

        WorkbookPart workbookpart = spreadsheetDocument.AddWorkbookPart();
        workbookpart.Workbook = new Workbook();


        WorksheetPart worksheetPart = workbookpart.AddNewPart<WorksheetPart>();
        worksheetPart.Worksheet = new Worksheet(new SheetData());


        Sheets sheets = spreadsheetDocument.WorkbookPart.Workbook.AppendChild<Sheets>(new Sheets());

        int i = 1;
        while (i <= 5)
        {
            Sheet sheet = new Sheet() { Id = spreadsheetDocument.WorkbookPart.GetIdOfPart(worksheetPart), SheetId = (UInt32)i, Name = "mySheet" + i.ToString() };

            for (int ii = 1; ii <= 5; ii++)
            {

                Row row1 = new Row();
                sheets.AppendChild(row1);

                Cell cll = new Cell(new InlineString(new DocumentFormat.OpenXml.Spreadsheet.Text("qqqqqq"))) { DataType = CellValues.InlineString };
                row1.AppendChild(cll);

            }
            sheets.Append(sheet);
            i++;
        }


        workbookpart.Workbook.Save();

有谁知道如何将字体和颜色应用到不同的单元格吗?

最佳答案

就像爱德华已经说过的,您需要创建一个样式表对象。

var stylesheet = new Stylesheet() { MCAttributes = new MarkupCompatibilityAttributes() {Ignorable = "x14ac"}};
stylesheet.AddNamespaceDeclaration("mc", "http: //schemas.openxmlformats.org/markup-compatibility/2006");
stylesheet.AddNamespaceDeclaration("x14ac", "http: //schemas.microsoft.com/office/spreadsheetml/2009/9/ac");

样式表对象必须包含您要使用的所有字体、填充、单元格格式等。

// create collections for fonts, fills, cellFormats, ...
var fonts = new Fonts() { Count = 1U, KnownFonts = true };
var fills = new Fills() {Count = 5U};
var cellFormats = new CellFormats() { Count = 4U };

// create a font: bold, red, calibr
Font font = new Font();
font.Append(new FontSize() {Val = 11D});
font.Append(new Color() { Rgb = "ff0000"});
font.Append(new FontName() {Val = "Calibri"});
font.Append(new FontFamilyNumbering() {Val = 2});
font.Append(new FontScheme() {Val = FontSchemeValues.Minor});
font.Append(new Bold());
// add the created font to the fonts collection
// since this is the first added font it will gain the id 1U
fonts.Append(font);

// create a background: green
Fill fill = new Fill();
var patternFill = new PatternFill() {PatternType = PatternValues.Solid};
patternFill.Append(new ForegroundColor() {Rgb = "00ff00"});
patternFill.Append(new BackgroundColor() {Indexed = 64U});
fill.Append(patternFill);
fills.Append(fill);

// create a cell format (combining font and background)
// the first added font/fill/... has the id 0. The second 1,...
cellFormats.AppendChild(new CellFormat(){ FontId = 0U, FillId = 0U });

// add the new collections to the stylesheet
stylesheet.Append(fonts);
stylesheet.Append(fills);
stylesheet.Append(cellFormats);

将样式表分配给您的工作簿部分对象

var stylePart = workbookpart.AddNewPart<WorkbookStylesPart>();
stylePart.Stylesheet = stylesheet;
stylePart.Stylesheet.Save();

之后,您可以将 CellStyleId 分配给单元格。

var cell = new Cell() {
  CellValue = new CellValue("your cooler string"),
  DataType = new EnumValue<CellValues>(CellValues.String),
  StyleIndex = 0U // index in the cellFormats array
};
<小时/>

为 CellFormats 等对象分配字体、填充、边框等

每个FontFillBorder...都可以通过其在相应集合中的索引分配给其他对象。例如,第一个添加到字体对象的字体将具有索引1U

这些索引表示为无符号整数(没有负值大于正值)。 “U”后缀表示 uint 或 ulong。

<小时/>

链接

OpenXml docs , unsigned integers

关于excel - 如何使用 openxml 在 Excel 文件单元格中的文本上应用字体和颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39872791/

相关文章:

excel - 1004 将范围从一张工作表复制到另一张工作表时出错

C# openxml去除段落

c# - AltChunk 破坏富文本内容控件

ubuntu - Ubuntu 服务器中的 DocumentFormat.OpenXml

excel - 根据一个字母删除行

excel - 范围仅在从引用的工作表中调用时才有效

excel - Apache POI : comments in empty cells in an empty row

android - 如何在 TextView 中使用 Roboto 字体?

html - 使文本适合 flexbox 容器

java - 如何在 Eclipse 中为 Java 文本编辑器更改字体大小?