asp.net core|asp.net OpenXML 设置Excel单元格格式

更新时间:2017-05-07    来源:excel    手机版     字体:

【www.bbyears.com--excel】

一、设置单元格字体

Stylesheet中存储字体集的Class是Fonts,设置字体的class是Font。

首先,定义一个有三种字体的字体集:

 代码如下

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

 

然后,定义几种字体,并将该字体添加到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开始。代码如下:

 代码如下

stylesheet.Borders = new Borders()
             {
                 Count = (UInt32Value)2U
             };
 
             //borderID=0
             Border borderDefault = new Border(new LeftBorder(), new RightBorder(), new TopBorder() { }, new BottomBorder(), new DiagonalBorder());
             stylesheet.Borders.Append(borderDefault);
 
             //borderID=1
             Border borderContent = new Border(
                new LeftBorder(new Color() { Auto = true }) { Style = BorderStyleValues.Thin },
                 new RightBorder(new Color() { Auto = true }) { Style = BorderStyleValues.Thin },
                 new TopBorder(new Color() { Auto = true }) { Style = BorderStyleValues.Thin },
                 new BottomBorder(new Color() { Auto = true }) { Style = BorderStyleValues.Thin },
                 new DiagonalBorder()
                 );
             stylesheet.Borders.Append(borderContent);

 

三、设置单元格的填充色

同上述设置字体和边框一样,设置填充色也是需要先设置填充色的集合,然后再将具体的填充色添加到填充色集合中。但是这里需要注意的在Fills的fillid=0和fillId=1的位置均是系统默认的。fillId=0的填充色是None,fillId=1的填充色是Gray125,但需要自定义填充色时,必须从fillId=2开始定义,就是说在需要自定义时候需要先定义这两种填充色。(是通过自己反复测试发现的,被折腾很久)。代码如下:

 代码如下

 

           //fillId,0总是None,1总是gray125,自定义的从fillid =2开始
            stylesheet.Fills = new Fills()
            {
                 Count = (UInt32Value)3U
            };
 
             //fillid=0
             Fill fillDefault = new Fill(new PatternFill() { PatternType = PatternValues.None });
             stylesheet.Fills.Append(fillDefault);
 
            //fillid=1
            Fill fillGray = new Fill();
            PatternFill patternFillGray = new PatternFill()
             {
                 PatternType = PatternValues.Gray125
             };
             fillGray.Append(patternFillGray);
             stylesheet.Fills.Append(fillGray);
 
             //fillid=2
             Fill fillYellow = new Fill();
             PatternFill patternFillYellow = new PatternFill(new ForegroundColor() { Rgb = new HexBinaryValue() { Value = "FFFFFF00" } })
             {
                 PatternType = PatternValues.Solid
             };
            fillYellow.Append(patternFillYellow);
             stylesheet.Fills.Append(fillYellow);
 
             stylesheet.Borders = new Borders()
             {
                 Count = (UInt32Value)2U
             };

 

四、定义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 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设置常用的单元格格式

 

本文来源:http://www.bbyears.com/bangongshuma/32459.html

猜你感兴趣

热门标签

更多>>

本类排行