SpreadsheetControl组件修改,拖拽、新建、打开文件在独立浮窗打开

一、修改文件拖拽功能,使其能够在另外一个独立窗体打开,需要配合documentManager控件实现。

实现后效果:将11.xlsx文件拖拽到工作区

 1.创建XExceluserControl用户窗体,代码如下:

 public partial class XExcelUserControl : DevExpress.XtraEditors.XtraUserControl
 {

     public string filePath;
     public XExcelUserControl(string filePath = "")

     {
         InitializeComponent();
         this.filePath = filePath;
         loadDocument();
     }
     
     /// <summary>
     /// 加载excel
     /// </summary>
     /// <param name="filePath"></param>
     private void loadDocument() 
     {
         if (File.Exists(filePath))
         {
             IWorkbook workbook = spreadsheetControl1.Document;
             workbook.LoadDocument(filePath);
         }
         
     }

     private void XExcelUserControl_Load(object sender, EventArgs e)
     {
         spreadsheetControl1.DragEnter += SpreadsheetControl1_DragEnter;// 添加拖拽文件的事件处理程序
         spreadsheetControl1.DragOver += SpreadsheetControl1_DragOver;

         // 隐藏快速访问工具栏和应用程序按钮
         ribbonControl1.ToolbarLocation = DevExpress.XtraBars.Ribbon.RibbonQuickAccessToolbarLocation.Hidden;
         ribbonControl1.ShowApplicationButton = DevExpress.Utils.DefaultBoolean.False;
     }


     private void SpreadsheetControl1_DragEnter(object sender, DragEventArgs e)
     {
         Console.WriteLine("SpreadsheetControl1_DragEnter");
         if (e.Data.GetDataPresent(DataFormats.FileDrop))
         {
             // 获取拖拽的文件路径数组
             string[] filePaths = (string[])e.Data.GetData(DataFormats.FileDrop);

             // 处理每个文件路径
             foreach (string filePath in filePaths)
             {
                 XExcelManageForm manageForm = Application.OpenForms.OfType<XExcelManageForm>().FirstOrDefault();
                 manageForm.CreateForm(filePath);
                 // 处理拖拽的文件路径,例如打开文件等
                 Console.WriteLine("拖拽文件路径:" + filePath);
                 return;
             }
         }

     }

     private void SpreadsheetControl1_DragOver(object sender, DragEventArgs e)
     {
         e.Effect = DragDropEffects.None;
     }
 }

2.创建XExcelManageForm窗体,加入下面代码

        public int autoTmpId = 0;
        public  void CreateForm(string text="") 
        {
            string _text;
            if (text!="")
            {
                //获取文件最后两级,预防不同目录文件重名
                _text = Path.GetFileName(Path.GetDirectoryName(text)) + '/' + Path.GetFileName(text);
            }
            else
            {
                //创建临时excel的时候会走这里
                _text = "临时";
                if (autoTmpId!=0)
                {
                    _text += "_" + autoTmpId;
                }
                autoTmpId++;
            }
            // 检查是否已经打开了相同路径的文档
            foreach (Document doc in tabbedView1.Documents)
            {
                XExcelUserControl form = doc.Control as XExcelUserControl;
                if (form.filePath == text)
                {
                    // 如果已经存在相同路径的文档,则激活该文档并返回
                    tabbedView1.ActivateDocument(doc.Control);
                    return;
                }
            }
            XExcelUserControl xExcelForm = new XExcelUserControl(text);
            xExcelForm.Text = _text;
            tabbedView1.AddDocument(xExcelForm);
            tabbedView1.ActivateDocument(xExcelForm);
        }

二、自带的File Bars创建的fileRibbonPage1中的新建文件FileNew功能不是在新的独立浮窗打开

实现后效果:点击新建按钮

 1.删除原先新建按钮,再重新创建一个barButtonItem1按钮,添加图片展示和原按钮一样。(原先按钮组件自动生成的,不知道怎么修改)

2.添加点击事件

private void barButtonItem1_ItemClick(object sender, ItemClickEventArgs e)
{
    FileNew();
}
/// <summary>
/// 新建文档
/// </summary>
private void FileNew(string _filePath = "")
{
    XExcelManageForm manageForm = (XExcelManageForm)CommonForm.GetActivedForm<XExcelManageForm>();
    manageForm.CreateForm(_filePath);
}

3.修改spreadsheetControl1自带的快捷键"ctrl+N"

private void SpreadsheetControl1_KeyDown(object sender, KeyEventArgs e)
{
    if (e.Control && e.KeyCode == Keys.N)
    {
        FileNew();
        e.Handled = true;//阻止事件继续传播
    }

}

三、自带的File Bars创建的fileRibbonPage1中的新建文件OpenFile功能不是在新的独立浮窗打开

实现后效果:点击新建按钮

 

 

 

1.该步骤与标题二一样

2.添加点击事件

private void barButtonItem2_ItemClick(object sender, ItemClickEventArgs e)
{
    OpenFile();
}

#region 非事件

/// <summary>
/// 新建文档
/// </summary>
private void FileNew(string _filePath = "")
{
    XExcelManageForm manageForm = (XExcelManageForm)CommonForm.GetActivedForm<XExcelManageForm>();
    manageForm.CreateForm(_filePath);
}

private void OpenFile()
{
    string _filePath = FileTool.OpenFile(FileTool.ExcelFileFilter);
    if (_filePath != null)
    {
        FileNew(_filePath);
    }
}
#endregion
OpenFile可以自行找位置加上
public const string TextFileFilter = "文本文件 (*.txt)|*.txt|所有文件 (*.*)|*.*";
public const string ExcelFileFilter = "Excel 文件 (*.xls;*.xlsx)|*.xls;*.xlsx|所有文件 (*.*)|*.*";
public const string JsonFileFilter = "JSON 文件 (*.json)|*.json|所有文件 (*.*)|*.*";
public const string DbFileFilter = "db 文件 (*.db)|*.db|所有文件 (*.*)|*.*";
public static string OpenFile(string filter)
{
    using (OpenFileDialog openFileDialog = new OpenFileDialog())
    {
        openFileDialog.Filter = filter;
        openFileDialog.FilterIndex = 1;
        openFileDialog.RestoreDirectory = true;

        DialogResult result = openFileDialog.ShowDialog();

        if (result == DialogResult.OK)
        {
            // 用户选择了文件,将文件路径显示在文本框中
            return openFileDialog.FileName;
        }
        return null; // 或者可以返回空字符串 ""
    }
}

3.修改spreadsheetControl1自带的快捷键"Ctrl+O"

  private void SpreadsheetControl1_KeyDown(object sender, KeyEventArgs e)
  {
      if (e.Control && e.KeyCode == Keys.N)
      {
          FileNew();
          e.Handled = true;//阻止事件继续传播
      }
      if (e.Control && e.KeyCode == Keys.O)
      {
          OpenFile();
          e.Handled = true;//阻止事件继续传播
      }
  }

 

posted @ 2024-04-17 17:39  A-GREATE-USERNAME  阅读(192)  评论(0编辑  收藏  举报