asp.net OpenXML 設定Excel儲存格格式

來源:互聯網
上載者:User

一、設定儲存格字型

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<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設定常用的儲存格格式

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.