DevExpress SpreadsheetControl 经验小记

这一段用spreadsheetcontrol,好多问题,记性越来越差,记录一下(14.1.4.0版本)

1、A1 B1两个单元格合并,触发MouseClick 和CellBeginEdit事件,会因点击的是A1位置还是B1位置,e.columnindex 会有所不同

2、最后修改A1单元格,然后点击界面按钮保存时,因为单元格焦点没有转移,A1单元格的Editor没有自动关闭,所以无法获取到刚输入的值,可以在保存按钮里添加

spreadsheetControl1.CloseCellEditor(DevExpress.XtraSpreadsheet.CellEditorEnterValueMode.ActiveCell);


 

强制关闭Editor。

 3、保护特定单元格

            using DevExpress.Spreadsheet;
            spreadsheetControl1.BeginUpdate();
            Worksheet worksheet = spreadsheetControl1.ActiveWorksheet;
            // Give specific user permission to edit a range in a protected worksheet 
            ProtectedRange protectedRange = worksheet.ProtectedRanges.Add("My Range", worksheet["C3:E8"]);
            EditRangePermission permission = new EditRangePermission();
            permission.UserName = Environment.UserName;
            permission.DomainName = Environment.UserDomainName;
            permission.Deny = false;
            protectedRange.SecurityDescriptor = protectedRange.CreateSecurityDescriptor(new EditRangePermission[] { permission });
            protectedRange.SetPassword("123");

            // Protect a worksheet
            if (!worksheet.IsProtected)
                worksheet.Protect("password", WorksheetProtectionPermissions.Default);

            spreadsheetControl1.EndUpdate();


这样有个问题,每次双击受保护的单元格时会有个弹出提示,很影响使用感受

还是使用CellBeginEdit事件来实现不可编辑,依据条件设置e.Cancel,但无法阻止粘贴

---------------------------------

终于想出来阻止粘贴的办法

单元格赋值时,通过调用SetValue函数或直接Value= ,这两种操作都不会触发spreadsheetcontrol的CellValueChanged事件,所以可以在此事件中设置条件,判断此单元格的值是否能够被修改(修改途径只包含界面操作,比如直接编辑--CellBeginEdit事件控制,粘贴)

4、滚动条事件 worksheet

void ScrollTo(Range scrolledAreaTopLeftCell);
void ScrollTo(int rowIndex, int columnIndex);
void ScrollToRow(int rowIndex);
void ScrollToRow(string rowHeading);
void ScrollToColumn(int columnIndex);
void ScrollToColumn(string columnHeading);

 

5、有的时候设置了某cell.Formula="=A1";

但不起作用,单元格直接显示 =A1

不清楚为什么,但跟excel格式有关,可能与导入的excel文件是否有宏代码冲突

6.格式粘贴Range

            Worksheet worksheet = spreadsheetControl1.ActiveWorksheet;
            worksheet.Columns["A"].WidthInCharacters = 32;
            worksheet.Columns["B"].WidthInCharacters = 20;
            //Style style = workbook.Styles[BuiltInStyleId.Input];

            // Specify the content and formatting for a source cell.
            worksheet.Cells["A1"].Value = "Source Cell";

            Cell sourceCell = worksheet.Cells["B1"];
            sourceCell.Formula = "= PI()";
            sourceCell.NumberFormat = "0.0000";
            //sourceCell.Style = style;
            sourceCell.Font.Color = Color.Blue;
            sourceCell.Font.Bold = true;
            sourceCell.Borders.SetOutsideBorders(Color.Black, BorderLineStyle.Thin);

            // Copy all information from the source cell to the "B3" cell. 
            worksheet.Cells["A3"].Value = "Copy All";
            worksheet.Cells["B3"].CopyFrom(sourceCell);

            // Copy only the source cell content (e.g., text, numbers, formula calculated values) to the "B4" cell.
            worksheet.Cells["A4"].Value = "Copy Values";
            worksheet.Cells["B4"].CopyFrom(sourceCell, PasteSpecial.Values);

            // Copy the source cell content (e.g., text, numbers, formula calculated values) 
            // and number formats to the "B5" cell.
            worksheet.Cells["A5"].Value = "Copy Values and Number Formats";
            worksheet.Cells["B5"].CopyFrom(sourceCell, PasteSpecial.Values | PasteSpecial.NumberFormats);

            // Copy only the formatting information from the source cell to the "B6" cell.
            worksheet.Cells["A6"].Value = "Copy Formats";
            worksheet.Cells["B6"].CopyFrom(sourceCell, PasteSpecial.Formats);

            // Copy all information from the source cell to the "B7" cell except for border settings.
            worksheet.Cells["A7"].Value = "Copy All Except Borders";
            worksheet.Cells["B7"].CopyFrom(sourceCell, PasteSpecial.All & ~PasteSpecial.Borders);

            // Copy information only about borders from the source cell to the "B8" cell.
            worksheet.Cells["A8"].Value = "Copy Borders";
            worksheet.Cells["B8"].CopyFrom(sourceCell, PasteSpecial.Borders);

你可能感兴趣的:(DevExpress SpreadsheetControl 经验小记)