GemBox.Spreadsheet如何设置单元格样式和格式

作者:控件中国网   出处:控件中国网   2016-04-26 10:21:41   阅读:9

GemBox.Spreadsheet是一款.NET控件,可以帮助开发人员在.NET程序中读取、写入、转换和打印XLSX, XLS, ODS, CSV, HTML, PDF, XPS等文件格式,并且可以在 WPF, ASP.NET 和 Windows.Forms 程序中浏览和编辑Spreadsheets,可以对单元格进行数字的格式化、单元格文本对齐、缩进、旋转、边框、阴影、保护、换行等,字体的大小、加粗、颜色等进行设置,下面的代码主要讲述了如何对单元格进行格式化:
CellStyle.png
using GemBox.Spreadsheet;
using System;
using System.IO;
 
class Sample
{
    [STAThread]
    static void Main(string[] args)
    {
        // If using Professional version, put your serial key below.
        SpreadsheetInfo.SetLicense("FREE-LIMITED-KEY");
 
        ExcelFile ef = new ExcelFile();
        ExcelWorksheet ws = ef.Worksheets.Add("Styles and Formatting");
 
        ws.Cells[0, 1].Value = "Cell style examples:";
        ws.PrintOptions.PrintGridlines = true;
 
        int row = 0;
 
        // Column width of 4, 30 and 35 characters.
        ws.Columns[0].Width = 4 * 256;
        ws.Columns[1].Width = 30 * 256;
        ws.Columns[2].Width = 35 * 256;
 
        ws.Cells[row += 2, 1].Value = ".Style.Borders.SetBorders(...)";
        ws.Cells[row, 2].Style.Borders.SetBorders(MultipleBorders.All, SpreadsheetColor.FromArgb(252, 1, 1), LineStyle.Thin);
 
        ws.Cells[row += 2, 1].Value = ".Style.FillPattern.SetPattern(...)";
        ws.Cells[row, 2].Style.FillPattern.SetPattern(FillPatternStyle.ThinHorizontalCrosshatch, SpreadsheetColor.FromName(ColorName.Green), SpreadsheetColor.FromName(ColorName.Yellow));
 
        ws.Cells[row += 2, 1].Value = ".Style.Font.Color =";
        ws.Cells[row, 2].Value = "Color.Blue";
        ws.Cells[row, 2].Style.Font.Color = SpreadsheetColor.FromName(ColorName.Blue);
 
        ws.Cells[row += 2, 1].Value = ".Style.Font.Italic =";
        ws.Cells[row, 2].Value = "true";
        ws.Cells[row, 2].Style.Font.Italic = true;
 
        ws.Cells[row += 2, 1].Value = ".Style.Font.Name =";
        ws.Cells[row, 2].Value = "Comic Sans MS";
        ws.Cells[row, 2].Style.Font.Name = "Comic Sans MS";
 
        ws.Cells[row += 2, 1].Value = ".Style.Font.ScriptPosition =";
        ws.Cells[row, 2].Value = "ScriptPosition.Superscript";
        ws.Cells[row, 2].Style.Font.ScriptPosition = ScriptPosition.Superscript;
 
        ws.Cells[row += 2, 1].Value = ".Style.Font.Size =";
        ws.Cells[row, 2].Value = "18 * 20";
        ws.Cells[row, 2].Style.Font.Size = 18 * 20;
 
        ws.Cells[row += 2, 1].Value = ".Style.Font.Strikeout =";
        ws.Cells[row, 2].Value = "true";
        ws.Cells[row, 2].Style.Font.Strikeout = true;
 
        ws.Cells[row += 2, 1].Value = ".Style.Font.UnderlineStyle =";
        ws.Cells[row, 2].Value = "UnderlineStyle.Double";
        ws.Cells[row, 2].Style.Font.UnderlineStyle = UnderlineStyle.Double;
 
        ws.Cells[row += 2, 1].Value = ".Style.Font.Weight =";
        ws.Cells[row, 2].Value = "ExcelFont.BoldWeight";
        ws.Cells[row, 2].Style.Font.Weight = ExcelFont.BoldWeight;
 
        ws.Cells[row += 2, 1].Value = ".Style.HorizontalAlignment =";
        ws.Cells[row, 2].Value = "HorizontalAlignmentStyle.Center";
        ws.Cells[row, 2].Style.HorizontalAlignment = HorizontalAlignmentStyle.Center;
 
        ws.Cells[row += 2, 1].Value = ".Style.Indent";
        ws.Cells[row, 2].Value = "five";
        ws.Cells[row, 2].Style.HorizontalAlignment = HorizontalAlignmentStyle.Left;
        ws.Cells[row, 2].Style.Indent = 5;
 
        ws.Cells[row += 2, 1].Value = ".Style.IsTextVertical = ";
        ws.Cells[row, 2].Value = "true";
        // Set row height to 50 points.
        ws.Rows[row].Height = 50 * 20;
        ws.Cells[row, 2].Style.IsTextVertical = true;
 
        ws.Cells[row += 2, 1].Value = ".Style.NumberFormat";
        ws.Cells[row, 2].Value = 1234;
        ws.Cells[row, 2].Style.NumberFormat = "#.##0,00 [$Krakozhian Money Units]";
 
        ws.Cells[row += 2, 1].Value = ".Style.Rotation";
        ws.Cells[row, 2].Value = "35 degrees up";
        ws.Cells[row, 2].Style.Rotation = 35;
 
        ws.Cells[row += 2, 1].Value = ".Style.ShrinkToFit";
        ws.Cells[row, 2].Value = "This property is set to true so this text appears shrunk.";
        ws.Cells[row, 2].Style.ShrinkToFit = true;
 
        ws.Cells[row += 2, 1].Value = ".Style.VerticalAlignment =";
        ws.Cells[row, 2].Value = "VerticalAlignmentStyle.Top";
        // Set row height to 30 points.
        ws.Rows[row].Height = 30 * 20;
        ws.Cells[row, 2].Style.VerticalAlignment = VerticalAlignmentStyle.Top;
 
        ws.Cells[row += 2, 1].Value = ".Style.WrapText";
        ws.Cells[row, 2].Value = "This property is set to true so this text appears broken into multiple lines.";
        ws.Cells[row, 2].Style.WrapText = true;
 
        ef.Save("Styles and Formatting.xls");
    }
}
Copyright© 2006-2015 ComponentCN.com all rights reserved.重庆磐岩科技有限公司(控件中国网) 版权所有 渝ICP备12000264号 法律顾问:元炳律师事务所
客服软件
live chat