当前位置: 移动技术网 > IT编程>开发语言>c# > 基于DevExpress的SpreadsheetControl实现对Excel的打开、预览、保存、另存为、打印(附源码下载)

基于DevExpress的SpreadsheetControl实现对Excel的打开、预览、保存、另存为、打印(附源码下载)

2019年08月27日  | 移动技术网IT编程  | 我要评论

场景

winform控件-devexpress18下载安装注册以及在vs中使用:

https://blog.csdn.net/badao_liumang_qizhi/article/details/100061243

参照以上将devexpress安装并引进到工具箱。

这里使用的是vs2013所以安装的devexpress是14版本。

devexpress14以及注册机下载


效果

 

实现

环境搭建

新建winform程序,拖拽一个spreadsheetcontrol,以及一个button按钮。

 

 

然后双击进入打开以及预览按钮的点击事件中

 private void simplebutton1_click(object sender, eventargs e)
        {
            string filepath = filedialoghelper.openexcel();
            if (!string.isnullorempty(filepath))
            {
                iworkbook workbook = spreadsheetcontrol1.document;
                workbook.loaddocument(filepath);
            }
        }

 

其中打开文件的路径是有工具类filedialoghelper中的openeecel方法返回的。

新建filedialoghelper类,类中新建方法实现打开一个选择文件对话框并将文件路径返回。

 public static string openexcel()
        {
            openfiledialog filedialog = new openfiledialog();
            filedialog.multiselect = true;
            filedialog.title = "请选择文件";
            filedialog.filter = "所有文件(*xls*)|*.xls*"; //设置要选择的文件的类型
            if (filedialog.showdialog() == dialogresult.ok)
            {
                return filedialog.filename;//返回文件的完整路径               
            }
            else
            {
                return null;
            }

        }

 

保存excel实现

拖拽一个按钮,双击进入其点击事件中。

在上面预览窗口中双击单元格对excel进行编辑后点击保存会将源文件进行保存。

private void simplebutton2_click(object sender, eventargs e)
        {
            spreadsheetcontrol1.savedocument();
        }

 

excel另存为实现

拖拽一个按钮,然后双击进入其点击事件中

 

private void simplebutton3_click(object sender, eventargs e)
        {
            //获取要保存的文件路径
            string filepath = filedialoghelper.saveexcel();
            //如果不为空
            if (!string.isnullorempty(filepath))
            {
                try
                {
                    //获取预览的excel对象 document提供对控件中加载的工作簿的访问
                    iworkbook workbook = spreadsheetcontrol1.document;
                    //根据选择的路径保存excel
                    workbook.savedocument(filepath);
                    //弹窗提示
                    messagebox.show("保存成功");
                }
                catch (exception ex)
                {
                    messagebox.show(ex.message);
                }
            }
        }

 

同理使用工具类弹窗选择保存路径,然后调用saveocument(path)进行保存另存为。

saveexcel方法代码

 public static string saveexcel()
        {
            string filename = "霸道";
            savefiledialog savedialog = new savefiledialog();
            //设置默认文件扩展名。
            savedialog.defaultext = "xls";
            //设置当前文件名筛选器字符串,该字符串决定对话框的“另存为文件类型”或“文件类型”框中出现的选择内容。
            savedialog.filter = "excel文件|*.xls";
          
            //  用默认的所有者运行通用对话框。
            savedialog.showdialog();
            //如果修改了文件名,用对话框中的文件名名重新赋值
            filename = savedialog.filename;
            //被点了取消
            if (filename.indexof(":") < 0) return null;
            else
            {
                //获取文件对话框中选定的文件名的字符串
                return savedialog.filename.tostring();
            }

        }

 

效果

excel打印实现

拖拽一个按钮,然后双击进入其点击事件中。

 private void simplebutton4_click(object sender, eventargs e)
        {
            this.spreadsheetcontrol1.showprintpreview();
        }

 

效果

 

 

源码下载

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网