• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

C#WinfForm控件之dev电子表格SpreadSheet

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

网上找了一些资料可是不得入门 只能再回过头来看demos

看了一点 例子大多继承自SpreadsheetRibbonTutorialControlBase 这个类,它又继承自SpreadSheetTutorialControlBase 再往前就不说了

SpreadSheetTutorialControlBase 这个类呢 只是定义了两个属性  可能就是运行语言格式的 没有注释

SpreadsheetRibbonTutorialControlBase 这个呢又定义了一个属性 selectPage  看样是选中的按钮页的意思,类型是 RibbonPage 又绑定了两个方法 一个是单元格值改变 另一个呢是 是最后编辑,我想是这样的 看名字 (spreadsheetControl1_CellValueChangedOverride、spreadsheetControl1_CellEndEditOverride)这个类就有所有的控件了,这是他的样子。有个doShow()(大约是显示的意思)方法 这个应继承自最基类的dll里先不管什么意思

再看他的子类们

1.AutoFilterModule 打开一个文档 看注释

namespace DevExpress.XtraSpreadsheet.Demos {
    public partial class AutoFilterModule : SpreadsheetRibbonTutorialControlBase {
        public AutoFilterModule() {
            InitializeComponent();

            // 这个是en-US 。。。英语的 (汉化的话 在程序入口加 en-Hans 这去百度用法)
            spreadsheetControl1.Options.Culture = DefaultCulture;

            //这个打开路径下的 auto...xlsx表格 getRelativePath是取程序路径 可以看到spreadsheetControl1.LoadDocument就是把文档加到spreadSheetControl中   
            spreadsheetControl1.LoadDocument(DemoUtils.GetRelativePath("AutoFilter_template.xlsx"));
        }

        #region Properties 重载了 选中dataribbonPage1
        protected internal override RibbonPage SelectedRibbonPage { get { return dataRibbonPage1; } }
        #endregion
    }
}
View Code

2。 BreakevenAnalysisModule 定义一个workBook 接收 ssc.document   ssc是spreadSheetControl下同 并加载excel表格 这个有点错误 ssc.docment 是取ssc上的sheet的意思

namespace DevExpress.XtraSpreadsheet.Demos {
    public partial class BreakevenAnalysisModule : SpreadsheetRibbonTutorialControlBase {
        public BreakevenAnalysisModule() {
            InitializeComponent();
            InitializeWorkbook();
        }

        #region Properties
        protected internal override RibbonPage SelectedRibbonPage { get { return insertRibbonPage1; } }
        #endregion

        //新建一个空文档 并加入excel表格  IWorkbook=spreadsheetControl1.Document;
        void InitializeWorkbook() {
            IWorkbook workbook = spreadsheetControl1.Document;
            workbook.LoadDocument(DemoUtils.GetRelativePath("BreakevenAnalysis.xlsx"));
        }
    }
}
View Code

3.CellPropertiesViewerModule

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Globalization;
using DevExpress.DXperience.Demos;
using DevExpress.XtraVerticalGrid.Rows;
using DevExpress.XtraEditors.Repository;
using DevExpress.XtraVerticalGrid;
using DevExpress.XtraVerticalGrid.Events;
using DevExpress.Spreadsheet;
using System.Diagnostics;

namespace DevExpress.XtraSpreadsheet.Demos {
    public partial class CellPropertiesViewerModule : SpreadsheetRibbonTutorialControlBase {
        IWorkbook workbook;
        Worksheet sheet;
        Range selectedCell;

        public CellPropertiesViewerModule() {
            InitializeComponent();
            InitializePropertyGrid();
            InitializeWorkbook();

            //清空历史文档  spreadsheetControl1.Document.History.Clear()
            this.spreadsheetControl1.Document.History.Clear();
        }

        /// <summary>
        /// 绑定 两个事件 
        /// </summary>
        protected internal override void SubscribeEvents() {
            base.SubscribeEvents();
            spreadsheetControl1.ContentChanged += spreadsheetControl1_ContentChanged;
            spreadsheetControl1.SelectionChanged += spreadsheetControl1_SelectionChanged;
        }

        //单元格改变是显示单元格属性
        void spreadsheetControl1_ContentChanged(object sender, EventArgs e) {
            workbook = spreadsheetControl1.Document;
            sheet = workbook.Worksheets.ActiveWorksheet;//活动页
            selectedCell = sheet.SelectedCell; //选中的单元格
            propertyGridControl1.SelectedObject = new RangeAdapter(selectedCell);//选中表格的属性 类在下边;
        }

        /// <summary>
        /// 不同步的话就同步过来
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void spreadsheetControl1_SelectionChanged(object sender, EventArgs e) {
            workbook = spreadsheetControl1.Document;
            sheet = workbook.Worksheets.ActiveWorksheet;
            if (!sheet.SelectedCell.Equals(selectedCell)) {
                selectedCell = sheet.SelectedCell;
                propertyGridControl1.SelectedObject = new RangeAdapter(selectedCell);
            }
        }

        #region PropertyGrid initialization 初始化属性表格
        void InitializePropertyGrid() {
            CategoryRow contentCategory = AddCategoryRow(null, "CellContent", "Cell content");//一级 cellContent
            AddEditorRow(contentCategory, "Cell.Formula", "Formula"); // cellcontent的下一级
            AddEditorRow(contentCategory, "Cell.ArrayFormula", "ArrayFormula");

            CategoryRow valueCategory = AddCategoryRow(contentCategory, "Value", "Value");
            AddEditorRow(valueCategory, "Value.TextValue", "TextValue");
            AddEditorRow(valueCategory, "Value.BooleanValue", "BooleanValue");
            AddEditorRow(valueCategory, "Value.NumericValue", "NumericValue");
            AddEditorRow(valueCategory, "Value.ErrorValue", "ErrorValue");
            AddEditorRow(valueCategory, "Value.Type", "Type");

            CategoryRow formattingCategory = AddCategoryRow(null, "Formatting", "Formatting");

            CategoryRow numberFormatCategory = AddCategoryRow(formattingCategory, "NumberFormat", "NumberFormat");
            AddEditorRow(numberFormatCategory, "NumberFormat", "NumberFormat");
            AddEditorRow(numberFormatCategory, "Cell.IsDisplayedAsDateTime", "IsDisplayedAsDateTime");

            CategoryRow alignmentCategory = AddCategoryRow(formattingCategory, "Alignment", "Alignment");
            AddEditorRow(alignmentCategory, "Alignment.Horizontal", "Horizontal");
            AddEditorRow(alignmentCategory, "Alignment.Vertical", "Vertical");
            AddEditorRow(alignmentCategory, "Alignment.Indent", "Indent");
            AddEditorRow(alignmentCategory, "Alignment.WrapText", "WrapText");

            CategoryRow fillCategory = AddCategoryRow(formattingCategory, "Fill", "Fill");
            AddEditorRow(fillCategory, "Fill.BackgroundColor", "Color");

            CategoryRow fontCategory = AddCategoryRow(formattingCategory, "Font", "Font");
            AddEditorRow(fontCategory, "Font.Color", "Color");
            AddEditorRow(fontCategory, "Font.Name", "Name");
            AddEditorRow(fontCategory, "Font.FontStyle", "FontStyle");
            AddEditorRow(fontCategory, "Font.Script", "Script");
            AddEditorRow(fontCategory, "Font.Size", "Size");
            AddEditorRow(fontCategory, "Font.Strikethrough", "Strikethrough");
            AddEditorRow(fontCategory, "Font.UnderlineType", "UnderlineType");

            CategoryRow bordersCategory = AddCategoryRow(formattingCategory, "Borders", "Borders");
            CategoryRow diagBorderCategory = AddCategoryRow(bordersCategory, "DiagonalBorder", "Diagonal");
            AddEditorRow(diagBorderCategory, "Borders.DiagonalBorderColor", "Color");
            AddEditorRow(diagBorderCategory, "Borders.DiagonalBorderLineStyle", "LineStyle");
            AddEditorRow(diagBorderCategory, "Borders.DiagonalBorderType", "Type");

            CategoryRow leftBorderCategory = AddCategoryRow(bordersCategory, "LeftBorder", "Left");
            AddEditorRow(leftBorderCategory, "LeftBorder.Color", "Color");
            AddEditorRow(leftBorderCategory, "LeftBorder.LineStyle", "LineStyle");

            CategoryRow topBorderCategory = AddCategoryRow(bordersCategory, "TopBorder", "Top");
            AddEditorRow(topBorderCategory, "TopBorder.Color", "Color");
            AddEditorRow(topBorderCategory, "TopBorder.LineStyle", "LineStyle");

            CategoryRow rightBorderCategory = AddCategoryRow(bordersCategory, "RightBorder", "Right");
            AddEditorRow(rightBorderCategory, "RightBorder.Color", "Color");
            AddEditorRow(rightBorderCategory, "RightBorder.LineStyle", "LineStyle");

            CategoryRow bottomBorderCategory = AddCategoryRow(bordersCategory, "BottomBorder", "Bottom");
            AddEditorRow(bottomBorderCategory, "BottomBorder.Color", "Color");
            AddEditorRow(bottomBorderCategory, "BottomBorder.LineStyle", "LineStyle");

            CategoryRow styleCategory = AddCategoryRow(formattingCategory, "Style", "Style");
            AddEditorRow(styleCategory, "Style.Name", "Name");

            CategoryRow layoutCategory = AddCategoryRow(null, "Layout", "Layout");
            AddEditorRow(layoutCategory, "Cell.ColumnWidth", "Width");
            AddEditorRow(layoutCategory, "Cell.ColumnWidthInCharacters", "WidthInCharacters");
            AddEditorRow(layoutCategory, "Cell.RowHeight", "Height");

            CategoryRow protectionCategory = AddCategoryRow(null, "Protection", "Protection");
            AddEditorRow(protectionCategory, "Protection.Locked", "Locked");

            bordersCategory.Expanded = false;
        }

        CategoryRow AddCategoryRow(CategoryRow parent, string name, string caption) {
            CategoryRow result = new CategoryRow(name);
            result.Properties.Caption = caption;
            if (parent == null)
                propertyGridControl1.Rows.Add(result);
            else
                parent.ChildRows.Add(result);
            return result;
        }
        void AddEditorRow(CategoryRow parent, string name, string caption) {
            EditorRow row = new EditorRow(name);
            row.Properties.Caption = caption;
            parent.ChildRows.Add(row);
        }
        #endregion

        #region Sample workbook initialization
        void InitializeWorkbook() {
            workbook = spreadsheetControl1.Document;
            workbook.LoadDocument(DemoUtils.GetRelativePath("CellPropertiesViewer_template.xlsx"));
        }
        #endregion

        protected override void DoShow() {
            base.DoShow();
            spreadsheetControl1.Focus();//焦点
        }
    }

    //属性对应的类
    public class RangeAdapter {
        Range range;

        public RangeAdapter(Range range) {
            this.range = range;
        }

        
        public Cell Cell { get { return range.Worksheet.Cells[range.TopRowIndex, range.LeftColumnIndex]; } }
        public CellValue Value { get { return range.Value; } }
        public Alignment Alignment { get { return range.Alignment; } }
        public Borders Borders { get { return range.Borders; } }
        public Border LeftBorder { get { return range.Borders.LeftBorder; } }
        public Border TopBorder { get { return range.Borders.TopBorder; } }
        public Border RightBorder { get { return range.Borders.RightBorder; } }
        public Border BottomBorder { get { return range.Borders.BottomBorder; } }
        public Fill Fill { get { return range.Fill; } }
        public DevExpress.Spreadsheet.SpreadsheetFont Font { get { return range.Font; } }
        public Protection Protection { get { return range.Protection; } }
        public Style Style { get { return range.Style; } }
        public string NumberFormat { get { return Cell.NumberFormat; } set { Cell.NumberFormat = value; } }
    }
}
View Code

4.ChartApiMode  这个 显示c#与VB的demo 后能不管他了 看他前台的demo 这里只看一个

 private void button1_Click(object sender, EventArgs e)
        {
            IWorkbook workbook = ssc.Document;
            Worksheet worksheet = workbook.Worksheets["sheet2"];

            worksheet.Cells["B2"].Value = "Phone";
            worksheet.Cells["c2"].Value = "pecent";
            worksheet.Cells["B4"].Value="adriod";
            worksheet.Cells["c4"].Value = 70;
            worksheet.Cells["b3"].Value = "iso";
            worksheet.Cells["c3"].Value = 30;
            workbook.Worksheets.ActiveWorksheet = worksheet;

            // 建一个本地图. 取值区间如果有无效数据则忽略
            DevExpress.Spreadsheet.Charts.Chart chart = worksheet.Charts.Add(ChartType.PieExploded, worksheet["B3:C4"]);

            //图表位置
            chart.TopLeftCell = worksheet.Cells["E2"];
            chart.BottomRightCell = worksheet.Cells["K15"];
            
            // 颜色类型.
            chart.Style = ChartStyle.ColorArea;

            // 隐藏右侧的说明
           chart.Legend.Visible = false;

            // 第一个切片的角度 .
            chart.Views[0].FirstSliceAngle = 0;

            // 各个切片是否显示名称 百分比 及显示方式为换行.
            DataLabelOptions dataLabels = chart.Views[0].DataLabels;
            dataLabels.ShowCategoryName = true;
            dataLabels.ShowPercent = true;
            dataLabels.Separator = "\n";

        }
View Code

5.ConditionalFormatting 这个主要是改变背景色 等显示 代码实现在TopTradingPartners这个静态类里  只看一个例子

  public static void ApplyTopImportsConditionalFormatting(Worksheet sheet) {
            ConditionalFormattingCollection conditionalFormattings = sheet.ConditionalFormattings;
            // 建一个 imports 列 5个格改变颜色.
            RankConditionalFormatting cfRule2 = conditionalFormattings.AddRankConditionalFormatting(sheet["Table[Imports]"], ConditionalFormattingRankCondition.TopByRank, 5);
            // Specify formatting options to be applied to cells if the condition is true.
            // Set the background color to light-red.
            cfRule2.Formatting.Fill.BackgroundColor = Color.FromArgb(0xFC, 0xB3, 0xB3);
        }
View Code

6.删除空列(空行同 只不过是遍历行操作)

 for (int i = spreadsheetControl1.ActiveWorksheet.Columns.LastUsedIndex; i >= 0; i--)
            {
                bool b = false;
                for (int j = spreadsheetControl1.ActiveWorksheet.Rows.LastUsedIndex; j >= 0; j--)
                {
                    if (spreadsheetControl1.ActiveWorksheet.Rows[j][i].Value.ToString() != "")
                    {
                        b = true;
                        break;
                    }

                }

                if (!b)
                {
                    spreadsheetControl1.ActiveWorksheet.Columns.Remove(i);

                }
            }
View Code

 7.取消所有的合并单元格

 foreach (var item in spreadsheetControl1.ActiveWorksheet.Cells.GetMergedRanges())
            {
                item.UnMerge();
            }
View Code

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C#调用迅雷7迅雷下载开放引擎发布时间:2022-07-10
下一篇:
C#中Switchcase返回不止用break发布时间:2022-07-10
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap