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

时间:2016-10-05 11:30:09

标签: 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();

有没有人知道如何将字体和颜色应用于不同的单元格?

2 个答案:

答案 0 :(得分:2)

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

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");

Stylesheet对象必须包含要使用的所有字体,填充,单元格格式等。

// 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);

将样式表分配给您的workbookpart对象

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,...都可以通过它们在相应集合中的索引分配给其他对象。例如,第一个添加到fonts对象的字体将具有索引1U

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


链接

OpenXml docsunsigned integers

答案 1 :(得分:0)

您需要创建并使用样式表。这是一个很好的参考,但如果您搜索创建样式表,还有更多参考。 Stylizing your Excel worksheets