Example #1
0
        private void Open(string fileName)
        {
            string extension = Path.GetExtension(fileName);
            IWorkbookFormatProvider provider = this.providers
                                               .FirstOrDefault(p => p.SupportedExtensions
                                                               .Any(e => string.Compare(extension, e, StringComparison.InvariantCultureIgnoreCase) == 0));

            if (provider != null)
            {
                using (Stream stream = File.OpenRead(fileName))
                {
                    try
                    {
                        this.Workbook = provider.Import(stream);
                    }
                    catch (Exception)
                    {
                        Console.WriteLine("Could not open file.");
                        this.Workbook = null;
                    }
                }
            }
            else
            {
                Console.WriteLine("Could not open file.");
            }
        }
        private static Workbook ImportWorkbook()
        {
            Assembly assembly   = typeof(ConvertViewModel).Assembly;
            string   fileName   = assembly.GetManifestResourceNames().First(n => n.Contains("SpreadProcessingDocument1.xlsx"));
            string   fileFormat = XlsxFormat;

            using (Stream stream = assembly.GetManifestResourceStream(fileName))
            {
                IWorkbookFormatProvider provider = GetProvider(fileFormat);
                Workbook workbook = provider.Import(stream);
                return(workbook);
            }
        }
        private void Open(object obj)
        {
            OpenFileDialog dialog = new OpenFileDialog();

            dialog.Filter      = "Xlsx files|*.xlsx|Csv files|*.Csv|Text files|*.txt|All files (*.*)|*.*";
            dialog.FilterIndex = 1;
            if (dialog.ShowDialog() == true)
            {
#if SILVERLIGHT
                string fileName = dialog.File.Name;
#else
                string fileName = dialog.FileName;
#endif
                string extension = Path.GetExtension(fileName);
                IWorkbookFormatProvider provider = this.providers
                                                   .FirstOrDefault(p => p.SupportedExtensions
                                                                   .Any(e => string.Compare(extension, e, StringComparison.InvariantCultureIgnoreCase) == 0));

                if (provider != null)
                {
#if SILVERLIGHT
                    using (Stream stream = dialog.File.OpenRead())
#else
                    using (Stream stream = dialog.OpenFile())
#endif
                    {
                        try
                        {
                            this.Workbook = provider.Import(stream);
                            this.FileName = Path.GetFileName(fileName);
                        }
                        catch (Exception)
                        {
                            MessageBox.Show("Could not open file.");
                            this.Workbook = null;
                            this.FileName = null;
                        }
                    }
                }
                else
                {
                    MessageBox.Show("Could not open file.");
                }
            }
        }