Beispiel #1
0
        public void Load(string path, IO.FileFormat fileFormat, Encoding encoding)
        {
            if (fileFormat == IO.FileFormat._Auto)
            {
                foreach (var p in FileFormatProviders)
                {
                    if (p.Value.IsValidFormat(path))
                    {
                        fileFormat = p.Key;
                        break;
                    }
                }

                if (fileFormat == FileFormat._Auto)
                {
                    throw new NotSupportedException("Cannot determine the file format to load workbook from specified path, try specify explicitly the file format by argument.");
                }
            }

            using (var fs = new FileStream(path, FileMode.Open, FileAccess.Read))
            {
                this.Load(fs, fileFormat, encoding == null ? Encoding.Default : encoding);
            }

            // for csv only
            if (fileFormat == FileFormat.CSV)
            {
                if (this.worksheets.Count > 0)
                {
                    this.worksheets[0].Name = Path.GetFileNameWithoutExtension(path);
                }
            }
        }
Beispiel #2
0
        public void Save(System.IO.Stream stream, IO.FileFormat fileFormat, Encoding encoding)
        {
            IFileFormatProvider provider = null;

            if (!FileFormatProviders.TryGetValue(fileFormat, out provider))
            {
                throw new FileFormatNotSupportException("Specified file format is not supported");
            }

            if (this.controlAdapter != null)
            {
                this.controlAdapter.ChangeCursor(CursorStyle.Busy);
            }

            try
            {
                provider.Save(this, stream, encoding, null);
            }
            finally
            {
                if (this.controlAdapter != null)
                {
                    this.controlAdapter.ChangeCursor(CursorStyle.PlatformDefault);
                }

                if (this.WorkbookSaved != null)
                {
                    this.WorkbookSaved(this, null);
                }
            }
        }
Beispiel #3
0
        public void Save(string path, IO.FileFormat fileFormat, Encoding encoding, object arg = null)
        {
            if (fileFormat == IO.FileFormat._Auto)
            {
                foreach (var p in FileFormatProviders)
                {
                    if (p.Value.IsValidFormat(path))
                    {
                        fileFormat = p.Key;
                        break;
                    }
                }

                if (fileFormat == FileFormat._Auto)
                {
                    throw new NotSupportedException("Cannot determine a file format to load workbook from specified path, try specify the file format.");
                }
            }

            if (fileFormat == FileFormat.Excel2007)
            {
                var ext = Path.GetExtension(path);
                if (ext.Equals(".xlsx", StringComparison.InvariantCultureIgnoreCase))
                {
                    arg = IO.OpenXML.Schema.OpenXMLContentTypes.Workbook______;
                }
                else if (ext.Equals(".xltx", StringComparison.InvariantCultureIgnoreCase))
                {
                    arg = IO.OpenXML.Schema.OpenXMLContentTypes.Template______;
                }
                if (ext.Equals(".xlsm", StringComparison.InvariantCultureIgnoreCase))
                {
                    arg = IO.OpenXML.Schema.OpenXMLContentTypes.MacroWorkbook_;
                }
                else if (ext.Equals(".xltm", StringComparison.InvariantCultureIgnoreCase))
                {
                    arg = IO.OpenXML.Schema.OpenXMLContentTypes.MacroTemplate_;
                }
            }

            var temp = Path.GetTempFileName();

            try
            {
                using (var fs = new FileStream(temp, FileMode.Create, FileAccess.ReadWrite))
                {
                    Save(fs, fileFormat, encoding, arg);
                }
                File.Copy(temp, path, true);
            }
            finally
            {
                File.Delete(temp);
            }
        }
Beispiel #4
0
        public object Load(System.IO.Stream stream, IO.FileFormat fileFormat, Encoding encoding, object arg)
        {
            object ret = null;

            if (fileFormat == FileFormat._Auto)
            {
                throw new System.ArgumentException("File format 'Auto' is invalid for loading workbook from stream, try specify a file format.");
            }

            IFileFormatProvider provider = null;

            if (!FileFormatProviders.TryGetValue(fileFormat, out provider))
            {
                throw new FileFormatNotSupportException("Specified file format is not supported.");
            }

            if (this.controlAdapter != null)
            {
                this.controlAdapter.ChangeCursor(CursorStyle.Busy);
            }

            if (encoding == null)
            {
                encoding = Encoding.Default;
            }

            try
            {
                ret = provider.Load(this, stream, encoding, arg);
                Recalculate();
                ExpandToContent();
            }
            finally
            {
                if (controlAdapter != null)
                {
                    controlAdapter.ChangeCursor(CursorStyle.PlatformDefault);
                }

                WorkbookLoaded?.Invoke(this, null);
            }
            return(ret);
        }
Beispiel #5
0
        public void Load(System.IO.Stream stream, IO.FileFormat fileFormat, Encoding encoding, string singleSheet = "")
        {
            if (fileFormat == FileFormat._Auto)
            {
                throw new System.ArgumentException("File format 'Auto' is invalid for loading workbook from stream, try specify a file format.");
            }

            IFileFormatProvider provider = null;

            if (!FileFormatProviders.TryGetValue(fileFormat, out provider))
            {
                throw new FileFormatNotSupportException("Specified file format is not supported.");
            }

            if (this.controlAdapter != null)
            {
                this.controlAdapter.ChangeCursor(CursorStyle.Busy);
            }

            if (encoding == null)
            {
                encoding = Encoding.Default;
            }

            try
            {
                provider.Load(this, stream, encoding, null, singleSheet);
            }
            finally
            {
                if (this.controlAdapter != null)
                {
                    this.controlAdapter.ChangeCursor(CursorStyle.PlatformDefault);
                }

                this.WorkbookLoaded?.Invoke(this, null);
            }
        }
Beispiel #6
0
        public void Save(string path, IO.FileFormat fileFormat, Encoding encoding)
        {
            if (fileFormat == IO.FileFormat._Auto)
            {
                foreach (var p in FileFormatProviders)
                {
                    if (p.Value.IsValidFormat(path))
                    {
                        fileFormat = p.Key;
                        break;
                    }
                }

                if (fileFormat == FileFormat._Auto)
                {
                    throw new NotSupportedException("Cannot determine a file format to load workbook from specified path, try specify the file format.");
                }
            }

            using (var fs = new FileStream(path, FileMode.Create, FileAccess.Write))
            {
                Save(fs, fileFormat, encoding);
            }
        }
Beispiel #7
0
 public void Load(System.IO.Stream stream, IO.FileFormat fileFormat)
 {
     this.Load(stream, fileFormat, Encoding.Default);
 }
Beispiel #8
0
 public void Load(string path, IO.FileFormat fileFormat)
 {
     this.Load(path, fileFormat, Encoding.Default);
 }
Beispiel #9
0
 public void Save(string path, IO.FileFormat fileFormat)
 {
     this.Save(path, fileFormat, Encoding.Default);
 }
Beispiel #10
0
 public void Load(System.IO.Stream stream, IO.FileFormat fileFormat, string singleSheet = "")
 {
     this.Load(stream, fileFormat, Encoding.Default, singleSheet);
 }
Beispiel #11
0
 public void Load(string path, IO.FileFormat fileFormat, string singleSheet = "")
 {
     this.Load(path, fileFormat, Encoding.Default, singleSheet);
 }
Beispiel #12
0
 public object Load(System.IO.Stream stream, IO.FileFormat fileFormat, object arg)
 {
     return(Load(stream, fileFormat, Encoding.Default, arg));
 }
Beispiel #13
0
 public object Load(string path, IO.FileFormat fileFormat, object arg = null)
 {
     return(Load(path, fileFormat, Encoding.Default, arg));
 }
Beispiel #14
0
 public void Save(System.IO.Stream stream, IO.FileFormat fileFormat, object arg = null)
 {
     Save(stream, fileFormat, Encoding.Default, arg);
 }
Beispiel #15
0
 public void Save(string path, IO.FileFormat fileFormat, object arg = null)
 {
     Save(path, fileFormat, Encoding.Default, arg);
 }