代码改变世界

使用OpenXML简单设置Excel单元格格式

2011-11-16 21:46  ps_zw  阅读(2437)  评论(0编辑  收藏  举报

前言:本文简单介绍使用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设置常用的单元格格式。