文档加载器

来源:互联网 发布:英语哪个软件好 编辑:程序博客网 时间:2024/06/07 03:14


using System.Drawing;
using System.Data;
using System.Linq;
using System.Windows.Forms;
using System.IO;
using DevExpress.Spreadsheet;
using DevExpress.XtraEditors;
using AxWMPLib;

namespace NewPwrWorkStationClient.Common.UserControl
{
    public partial class DocumentLoader : System.Windows.Forms.UserControl
    {
        private DevExpress.XtraSpreadsheet.SpreadsheetControl SubSpreadsheetControl;
        private DevExpress.XtraPdfViewer.PdfViewer SubPdfViewer;
        private DevExpress.XtraRichEdit.RichEditControl SubRichEditControl;
        private DevExpress.XtraEditors.PictureEdit SubPictureEdit;
        private AxAUTOVUEXLib.AxAutoVueX SubAxAutoVueX;
        private AxWindowsMediaPlayer SubAxWindowsMediaPlayer;
        private LabelControl SubLabelControl;
        public DocumentLoader()
        {
            InitializeComponent();
        }
        public string FileName = "";
        public string FileExtension = "";

        #region 文件加载方法调用
        public void LoadDocument(string filePath)
        {
            this.FileName = System.IO.Path.GetFileName(filePath);//文件名  “Default.aspx”
            this.FileExtension = System.IO.Path.GetExtension(filePath);//扩展名 “.aspx”
            FileLoadType loadType = this.GetFileLoadType(FileName);
            //if (loadType.Equals(FileLoadType.未知类型))
            //{
            //    XtraMessageBox.Show(string.Format("不支持{0}格式文件", FileExtension));
            //    return;
            //}
            this.SetControlVisible(loadType);
            this.ControlLoadFile(loadType, filePath);
        }
        public void LoadDocument(Stream memoryStream)
        {
            FileLoadType loadType = this.GetFileLoadType(FileName);
            this.LoadDocument(memoryStream, loadType);
        }

        public void LoadDocument(Stream memoryStream, string fileName)
        {
            this.FileName = fileName;
            this.LoadDocument(memoryStream);
        }

        public void LoadDocument(Stream memoryStream, FileLoadType loadType)
        {
            this.SetControlVisible(loadType);
            this.ControlLoadFile(loadType, memoryStream);
        }
        #endregion

        public void HideAllView()
        {
            this.HideControls(new Control[] { this.SubPdfViewer, this.SubPictureEdit, this.SubRichEditControl, this.SubSpreadsheetControl, this.SubAxAutoVueX, this.SubAxWindowsMediaPlayer, this.SubLabelControl });
        }

        private FileLoadType GetFileLoadType(string fileName)
        {
            FileLoadType loadType = FileLoadType.未知类型;
            if (FileName.Length == 0) return loadType;
            string[] picexts = { ".bmp", ".dib", ".jpg", ".jpeg", ".jpe", ".jfif", ".png", ".tif", ".tiff" };
            string[] excelexts = { ".xls", ".xlsx" };
            string[] docexts = { ".doc", ".docx" };
            string[] pdfexts = { ".pdf" };
            string[] cadexts = { ".dwg" };
            string[] gifexts = { ".gif" };
            string[] videoexts = { ".avi", ".mp4", ".wmv" };
            if (picexts.Where(s => FileName.ToLower().EndsWith(s)).Count() != 0)
            {
                this.FileExtension = picexts.FirstOrDefault(s => FileName.ToLower().EndsWith(s));
                loadType = FileLoadType.图片;
            }
            else if (excelexts.Where(s => FileName.ToLower().EndsWith(s)).Count() != 0)
            {
                this.FileExtension = picexts.FirstOrDefault(s => FileName.ToLower().EndsWith(s));
                loadType = FileLoadType.Excel;
            }
            else if (docexts.Where(s => FileName.ToLower().EndsWith(s)).Count() != 0)
            {
                this.FileExtension = docexts.FirstOrDefault(s => FileName.ToLower().EndsWith(s));
                loadType = FileLoadType.Word;
            }
            else if (pdfexts.Where(s => FileName.ToLower().EndsWith(s)).Count() != 0)
            {
                this.FileExtension = pdfexts.FirstOrDefault(s => FileName.ToLower().EndsWith(s));
                loadType = FileLoadType.PDF;
            }
            else if (cadexts.Where(s => FileName.ToLower().EndsWith(s)).Count() != 0)
            {
                this.FileExtension = cadexts.FirstOrDefault(s => FileName.ToLower().EndsWith(s));
                loadType = FileLoadType.CAD;
            }
            else if (gifexts.Where(s => FileName.ToLower().EndsWith(s)).Count() != 0)
            {
                this.FileExtension = cadexts.FirstOrDefault(s => FileName.ToLower().EndsWith(s));
                loadType = FileLoadType.Gif;
            }
            else if (videoexts.Where(s => FileName.ToLower().EndsWith(s)).Count() != 0)
            {
                this.FileExtension = cadexts.FirstOrDefault(s => FileName.ToLower().EndsWith(s));
                loadType = FileLoadType.Video;
            }
            return loadType;
        }

        private void SetControlVisible(FileLoadType loadType)
        {
            this.HideAllView();

            switch (loadType)
            {
                case FileLoadType.PDF:
                    if (this.SubPdfViewer == null)
                    {
                        this.SubPdfViewer = new DevExpress.XtraPdfViewer.PdfViewer();
                        this.SubPdfViewer.Dock = DockStyle.Fill;
                        this.Controls.Add(SubPdfViewer);
                    }
                    this.SubPdfViewer.Visible = true;
                    break;
                case FileLoadType.Excel:
                    if (this.SubSpreadsheetControl == null)
                    {
                        this.SubSpreadsheetControl = new DevExpress.XtraSpreadsheet.SpreadsheetControl();
                        this.SubSpreadsheetControl.Dock = DockStyle.Fill;
                        this.Controls.Add(SubSpreadsheetControl);
                    }
                    this.SubSpreadsheetControl.Visible = true;
                    break;
                case FileLoadType.图片:
                    if (this.SubPictureEdit == null)
                    {
                        this.SubPictureEdit = new DevExpress.XtraEditors.PictureEdit();
                        this.SubPictureEdit.Dock = DockStyle.Fill;
                        this.Controls.Add(SubPictureEdit);
                    }
                    this.SubPictureEdit.Visible = true;
                    break;
                case FileLoadType.Word:
                    if (this.SubRichEditControl == null)
                    {
                        this.SubRichEditControl = new DevExpress.XtraRichEdit.RichEditControl();
                        this.SubRichEditControl.Dock = DockStyle.Fill;
                        this.SubRichEditControl.ReadOnly = true;
                        this.Controls.Add(SubRichEditControl);
                    }
                    this.SubRichEditControl.Visible = true;
                    break;
                case FileLoadType.CAD:
                    if (this.SubAxAutoVueX == null)
                    {
                        this.SubAxAutoVueX = new AxAUTOVUEXLib.AxAutoVueX();
                        this.SubAxAutoVueX.Dock = DockStyle.Fill;
                        this.Controls.Add(SubAxAutoVueX);
                    }
                    this.SubAxAutoVueX.Visible = true;
                    break;
                case FileLoadType.Gif:
                    if (this.SubLabelControl == null)
                    {
                        this.SubLabelControl = new LabelControl();
                        this.SubLabelControl.AutoSizeMode = LabelAutoSizeMode.None;
                        this.SubLabelControl.Dock = DockStyle.Fill;

                        this.Controls.Add(this.SubLabelControl);
                    }
                    this.SubLabelControl.Visible = true;
                    //this.SubLabelControl.Text = "1222222";
                    break;
                case FileLoadType.Video:
                    if (this.SubAxWindowsMediaPlayer == null)
                    {

                        this.SubAxWindowsMediaPlayer = new AxWindowsMediaPlayer();
                        this.SubAxWindowsMediaPlayer.Enabled = true;
                        this.SubAxWindowsMediaPlayer.Dock = DockStyle.Fill;
                        this.SubAxWindowsMediaPlayer.BeginInit();
                        this.Controls.Add(this.SubAxWindowsMediaPlayer);
                        this.SubAxWindowsMediaPlayer.EndInit();
                    }
                    this.SubAxWindowsMediaPlayer.Visible = true;
                    break;
                default:
                    break;
            }
        }

        private void HideControls(Control[] ctrls)
        {
            foreach (Control item in ctrls)
            {
                this.HideControl(item);
            }
        }

        private void HideControl(Control ctrl)
        {
            if (ctrl != null)
            {

                ctrl.Visible = false;
                if (ctrl is AxWindowsMediaPlayer)
                {
                    ((AxWindowsMediaPlayer)ctrl).Ctlcontrols.stop();
                }
            }
        }

        private void ControlLoadFile(FileLoadType loadType, Stream memoryStream)
        {
            switch (loadType)
            {
                case FileLoadType.PDF:
                    this.SubPdfViewer.LoadDocument(memoryStream);
                    break;
                case FileLoadType.Excel:
                    SubSpreadsheetControl.CloseCellEditor(DevExpress.XtraSpreadsheet.CellEditorEnterValueMode.ActiveCell);
                    if (this.FileExtension.Equals(".xls"))
                    {
                        SubSpreadsheetControl.LoadDocument(memoryStream, DocumentFormat.Xls);
                    }
                    else if (this.FileExtension.Equals(".xlsx"))
                    {
                        SubSpreadsheetControl.LoadDocument(memoryStream, DocumentFormat.Xlsx);
                    }
                    break;
                case FileLoadType.图片:
                    Image img = Image.FromStream(memoryStream);
                    this.SubPictureEdit.Image = img;
                    break;
                case FileLoadType.Word:
                    this.SubRichEditControl.LoadDocument(memoryStream, DevExpress.XtraRichEdit.DocumentFormat.Doc);
                    break;
                default:
                    break;
            }
        }

        private void ControlLoadFile(FileLoadType loadType, string Filepath)
        {

            switch (loadType)
            {
                case FileLoadType.PDF:
                    this.SubPdfViewer.LoadDocument(Filepath);
                    break;
                case FileLoadType.Excel:
                    SubSpreadsheetControl.CloseCellEditor(DevExpress.XtraSpreadsheet.CellEditorEnterValueMode.ActiveCell);
                    SubSpreadsheetControl.LoadDocument(Filepath);
                    break;
                case FileLoadType.图片:
                    Image img = Image.FromFile(Filepath);
                    this.SubPictureEdit.Image = img;
                    break;
                case FileLoadType.Word:
                    this.SubRichEditControl.LoadDocument(Filepath);
                    break;
                case FileLoadType.CAD:
                    this.SubAxAutoVueX.SRC = Filepath;
                    break;
                case FileLoadType.Gif:
                    Image img2 = Image.FromFile(Filepath);
                    this.SubLabelControl.Appearance.Image = img2;
                    break;
                case FileLoadType.Video:
                    this.SubAxWindowsMediaPlayer.URL = Filepath;
                    //SubAxWindowsMediaPlayer.
                    //this.SubAxWindowsMediaPlayer.Ctlcontrols.play();
                    break;
                default:
                    break;
            }
        }
    }
    public enum FileLoadType
    {
        PDF = 1, Excel = 2, 图片 = 3, Word = 4, CAD = 5, Gif = 6, Video = 7, 未知类型 = -1
    }
}

0 0
原创粉丝点击