使用OpenXml操作Excel,实现Cell样式的设置<四>
对于Cell的样式设置,网上已经有很多资料,下面供参考。
//添加SheetStyle:Font\Border\Fill\字体居中
Fonts fonts = new Fonts() { Count = (UInt32Value)2U, KnownFonts = true };
//fontId = 0
DocumentFormat.OpenXml.Spreadsheet.Font font = new DocumentFormat.OpenXml.Spreadsheet.Font();
DocumentFormat.OpenXml.Spreadsheet.FontSize fontSize = new DocumentFormat.OpenXml.Spreadsheet.FontSize() { Val = 15D };
FontFamilyNumbering fontFamilyNumbering = new FontFamilyNumbering() { Val = 1 };
FontScheme fontScheme = new FontScheme() { Val = FontSchemeValues.Minor };
font.Append(fontSize);
font.Append(fontFamilyNumbering);
font.Append(fontScheme);
fonts.Append(font);
//fontId = 1
font = new DocumentFormat.OpenXml.Spreadsheet.Font();
Bold bold = new Bold();
fontSize = new DocumentFormat.OpenXml.Spreadsheet.FontSize() { Val = 26D };
fontFamilyNumbering = new FontFamilyNumbering() { Val = 1 };
fontScheme = new FontScheme() { Val = FontSchemeValues.Minor };
font.Append(bold);
font.Append(fontSize);
font.Append(fontFamilyNumbering);
font.Append(fontScheme);
fonts.Append(font);
stylesPart.Stylesheet.Append(fonts);
Fills fills = new Fills() { Count = (UInt32Value)3U };
//FillId = 0
Fill fill = new Fill();
PatternFill patternFill = new PatternFill() { PatternType = PatternValues.None };
fill.Append(patternFill);
fills.Append(fill);
//FillId = 1
fill = new Fill();
patternFill = new PatternFill() { PatternType = PatternValues.Gray125 };
fill.Append(patternFill);
fills.Append(fill);
//FillId = 2
fill = new Fill();
patternFill = new PatternFill() { PatternType = PatternValues.Solid };
ForegroundColor foregroundColor = new ForegroundColor() { Rgb = "FF87ceeb" };
BackgroundColor backgroundColor = new BackgroundColor() { Indexed = (UInt32Value)64U };
patternFill.Append(foregroundColor);
patternFill.Append(backgroundColor);
fill.Append(patternFill);
fills.Append(fill);
stylesPart.Stylesheet.Append(fills);
Borders borders = new Borders() { Count = (UInt32Value)2U };
//BorderId = 0
Border border = new Border();
LeftBorder leftBorder = new LeftBorder();
RightBorder rightBorder = new RightBorder();
TopBorder topBorder = new TopBorder();
BottomBorder bottomBorder = new BottomBorder();
DiagonalBorder diagonalBorder = new DiagonalBorder();
border.Append(leftBorder);
border.Append(rightBorder);
border.Append(topBorder);
border.Append(bottomBorder);
border.Append(diagonalBorder);
borders.Append(border);
//BorderId = 1
border = new Border();
leftBorder = new LeftBorder() { Style = BorderStyleValues.Thin };
rightBorder = new RightBorder() { Style = BorderStyleValues.Thin };
topBorder = new TopBorder() { Style = BorderStyleValues.Thin };
bottomBorder = new BottomBorder() { Style = BorderStyleValues.Thin };
DocumentFormat.OpenXml.Spreadsheet.Color color = new DocumentFormat.OpenXml.Spreadsheet.Color() { Indexed = (UInt32Value)64U };
leftBorder.Append(color);
color = new DocumentFormat.OpenXml.Spreadsheet.Color() { Indexed = (UInt32Value)64U };
rightBorder.Append(color);
color = new DocumentFormat.OpenXml.Spreadsheet.Color() { Indexed = (UInt32Value)64U };
topBorder.Append(color);
color = new DocumentFormat.OpenXml.Spreadsheet.Color() { Indexed = (UInt32Value)64U };
bottomBorder.Append(color);
diagonalBorder = new DiagonalBorder();
border.Append(leftBorder);
border.Append(rightBorder);
border.Append(topBorder);
border.Append(bottomBorder);
border.Append(diagonalBorder);
borders.Append(border);
stylesPart.Stylesheet.Append(borders);
//StyleIndex = 0;
CellFormats cellFormats = new CellFormats() { Count = (UInt32Value)5U };
CellFormat cellFormat = new CellFormat() { NumberFormatId = (UInt32Value)0U, FontId = (UInt32Value)0U, FillId = (UInt32Value)0U, BorderId = (UInt32Value)0U, FormatId = (UInt32Value)0U };
Alignment alignment = new Alignment() { Vertical = VerticalAlignmentValues.Center };
cellFormat.Append(alignment);
cellFormats.Append(cellFormat);
//StyleIndex = 1;
cellFormat = new CellFormat() { NumberFormatId = (UInt32Value)0U, FontId = (UInt32Value)0U, FillId = (UInt32Value)2U, BorderId = (UInt32Value)0U, FormatId = (UInt32Value)0U, ApplyFont = true, ApplyFill = true, ApplyBorder = true, ApplyAlignment = true };
alignment = new Alignment() { Vertical = VerticalAlignmentValues.Center };
cellFormat.Append(alignment);
cellFormats.Append(cellFormat);
//StyleIndex = 2;
cellFormat = new CellFormat() { NumberFormatId = (UInt32Value)0U, FontId = (UInt32Value)1U, FillId = (UInt32Value)2U, BorderId = (UInt32Value)0U, FormatId = (UInt32Value)0U, ApplyFont = true, ApplyFill = true, ApplyBorder = true, ApplyAlignment = true };
alignment = new Alignment() { Horizontal = HorizontalAlignmentValues.Center, Vertical = VerticalAlignmentValues.Center };
cellFormat.Append(alignment);
cellFormats.Append(cellFormat);
//StyleIndex = 3;
cellFormat = new CellFormat() { NumberFormatId = (UInt32Value)0U, FontId = (UInt32Value)0U, FillId = (UInt32Value)0, BorderId = (UInt32Value)1U, FormatId = (UInt32Value)0U, ApplyFont = true, ApplyFill = true, ApplyBorder = true, ApplyAlignment = true };
alignment = new Alignment() { Horizontal = HorizontalAlignmentValues.Center, Vertical = VerticalAlignmentValues.Center };
cellFormat.Append(alignment);
cellFormats.Append(cellFormat);
//StyleIndex = 4;
cellFormat = new CellFormat() { NumberFormatId = (UInt32Value)0U, FontId = (UInt32Value)0U, FillId = (UInt32Value)0, BorderId = (UInt32Value)0U, FormatId = (UInt32Value)0U, ApplyFont = true, ApplyFill = true, ApplyBorder = true, ApplyAlignment = true };
cellFormats.Append(cellFormat);
stylesPart.Stylesheet.Append(cellFormats);
stylesPart.Stylesheet.Save();