天天看點

使用OpenXML簡單設定Excel單元格格式

使用OpenXML簡單設定Excel單元格格式

前言:本文簡單介紹使用OpenXML如何設定Excel單元格格式。

在操作Excel時,不可避免的需要設定單元格的格式。使用OpenXML設定簡單的單元格格式還是很友善的。Excel需要使用到的單元格式在CellFormats對象中,CellFormats是對象workbookPart.WorkbookStylesPart.Stylesheet的一個屬性。是以使用時需要先建立Stylesheet對象的執行個體,然後根據需要建立具體的CellFormat對象。這裡簡單介紹下如何設定單元格值得字型(Fonts),單元格邊框(Borders),以及單元格填充色(Fills)。

一、設定單元格字型

Stylesheet中存儲字型集的Class是Fonts,設定字型的class是Font。

首先,定義一個有三種字型的字型集:

View Code

1  stylesheet.Fonts = new Fonts() 
2  {       
3         Count = (UInt32Value)3U           
4 };      

然後,定義幾種字型,并将該字型添加到Fonts中:

//fontId 從0開始,這裡的fontId=0,      
Font fontCalibri = new Font(new FontSize() { Val = 11D },
new FontName() { Val = "Calibri" },
new FontFamily() { Val = 2 },
new FontScheme() { Val = FontSchemeValues.Minor });

stylesheet.Fonts.Append(fontCalibri );      
//另2種字型的這裡略去,可以仿照上述字型定義。。。      

複制代碼

二、設定單元格邊框

單元格的邊框是定義在對象Borders中,同設定字型一樣,先建立指定大小的Borders對象,然後将具體的Border添加到邊框集中,borderId從0開始。代碼如下:

View Code

1   stylesheet.Borders = new Borders()
 2             {
 3                 Count = (UInt32Value)2U
 4             };
 5 
 6             //borderID=0
 7             Border borderDefault = new Border(new LeftBorder(), new RightBorder(), new TopBorder() { }, new BottomBorder(), new DiagonalBorder());
 8             stylesheet.Borders.Append(borderDefault);
 9 
10             //borderID=1
11             Border borderContent = new Border(
12                 new LeftBorder(new Color() { Auto = true }) { Style = BorderStyleValues.Thin },
13                 new RightBorder(new Color() { Auto = true }) { Style = BorderStyleValues.Thin },
14                 new TopBorder(new Color() { Auto = true }) { Style = BorderStyleValues.Thin },
15                 new BottomBorder(new Color() { Auto = true }) { Style = BorderStyleValues.Thin },
16                 new DiagonalBorder()
17                 );
18             stylesheet.Borders.Append(borderContent);      

三、設定單元格的填充色

同上述設定字型和邊框一樣,設定填充色也是需要先設定填充色的集合,然後再将具體的填充色添加到填充色集合中。但是這裡需要注意的在Fills的fillid=0和fillId=1的位置均是系統預設的。fillId=0的填充色是None,fillId=1的填充色是Gray125,但需要自定義填充色時,必須從fillId=2開始定義,就是說在需要自定義時候需要先定義這兩種填充色。(是通過自己反複測試發現的,被折騰很久)。代碼如下:

View Code

1             //fillId,0總是None,1總是gray125,自定義的從fillid =2開始
 2             stylesheet.Fills = new Fills()
 3             {
 4                 Count = (UInt32Value)3U
 5             };
 6 
 7             //fillid=0
 8             Fill fillDefault = new Fill(new PatternFill() { PatternType = PatternValues.None });
 9             stylesheet.Fills.Append(fillDefault);
10 
11             //fillid=1
12             Fill fillGray = new Fill();
13             PatternFill patternFillGray = new PatternFill()
14             {
15                 PatternType = PatternValues.Gray125
16             };
17             fillGray.Append(patternFillGray);
18             stylesheet.Fills.Append(fillGray);
19 
20             //fillid=2
21             Fill fillYellow = new Fill();
22             PatternFill patternFillYellow = new PatternFill(new ForegroundColor() { Rgb = new HexBinaryValue() { Value = "FFFFFF00" } })
23             {
24                 PatternType = PatternValues.Solid
25             };
26             fillYellow.Append(patternFillYellow);
27             stylesheet.Fills.Append(fillYellow);
28 
29             stylesheet.Borders = new Borders()
30             {
31                 Count = (UInt32Value)2U
32             };      

四、定義CellFormats

同之前描述的一樣,定義完CellFormat後,将其添加到單元格式集CellFormats中。

需要提及的是,不論Fonts,Borders,Fills還是CellFormats對象,他們都是Stylesheet的屬性。如果要讓設定的字型,邊框等有效,還需将CellFormat同Font,Border,Fill關聯起來,這就需要上述說的FontId,BorderId和FillId了(id的順序由加入到集合的先後決定)。

建立單元格(Cell)時,隻要将Cell的StyleIndex屬性設定為CellFormat的CellFormatId就可以應用單元格式了。代碼如下:

//定義格式      
stylesheet.CellFormats = new CellFormats();
stylesheet.CellFormats.Count = 2;
//styleIndex =0U
CellFormat cfDefault = new CellFormat();
cfDefault.Alignment = new Alignment();
cfDefault.NumberFormatId = 0;
cfDefault.FontId = 0;
cfDefault.BorderId = 0;
cfDefault.FillId = 0;
cfDefault.ApplyAlignment = true;
cfDefault.ApplyBorder = true;
stylesheet.CellFormats.Append(cfDefault);      
//styleIndex =1U
CellFormat cfContent = new CellFormat();
cfContent.Alignment = new Alignment();
cfContent.NumberFormatId = 0;
cfContent.FontId = 0;
cfContent.BorderId = 1;
cfContent.FillId = 2;
cfContent.ApplyAlignment = true;
cfContent.ApplyBorder = true;
stylesheet.CellFormats.Append(cfContent);      

複制代碼

建立單元格的代碼如下:

private Cell CreateTextCell(object cellValue, Nullable<uint> styleIndex)
        {
            Cell cell = new Cell();

            cell.DataType = CellValues.InlineString;

            cell.CellReference = “A1”;

if (styleIndex.HasValue)
                cell.StyleIndex = styleIndex.Value;

            InlineString inlineString = new InlineString();
            Text t = new Text();

            t.Text = cellValue.ToString();
            inlineString.AppendChild(t);
            cell.AppendChild(inlineString);

return cell;
        }      

複制代碼

注:本文主要簡單的介紹使用OpenXML設定常用的單元格格式。

使用OpenXML簡單設定Excel單元格格式