Пример #1
0
        public static object ListObjectsFromFile(
            [ExcelArgument(Name = "path", Description = "The path", AllowReference = true)]
            object xlPath,
            [ExcelArgument(Name = "fileName", Description = "The file name", AllowReference = true)]
            object xlFileName,
            [ExcelArgument(Name = "fileFormat", Description = "[Optional] The file format; the file extension will be used by default", AllowReference = true)]
            object xlFileFormat)
        {
            try
            {
                IExcelDataQuery fileFormatDataQuery = ExcelDataQuery.Create(xlFileFormat, "File format");

                string          fileName;
                IObjectStreamer objectStreamer;

                if (fileFormatDataQuery.IsEmpty == true)
                {
                    fileName = GetFileName(xlPath, xlFileName, ObjectStreamer.GetFileExtensions());

                    if (ObjectStreamer.TryGetObjectStreamerByFileExtension(ExtendedPath.GetExtension(fileName), out objectStreamer) == false)
                    {
                        throw new ArgumentException("Invalid file extension '" + ExtendedPath.GetExtension(fileName) + "', used default file extensions or specify the file format.");
                    }
                }
                else
                {
                    if (fileFormatDataQuery.TryGetPoolValue <IObjectStreamer>(ObjectStreamer.TryGetObjectStreamer, out objectStreamer, dataAdvice: ExcelDataAdvice.Create(ObjectStreamer.GetNames())) == false)
                    {
                        throw new ArgumentException("Invalid file format " + fileFormatDataQuery.ToString(0, 0) + ".");
                    }
                    fileName = GetFileName(xlPath, xlFileName, objectStreamer.FileExtension);
                    fileFormatDataQuery.QueryCompleted();
                }

                StreamReader        streamReader       = new StreamReader(fileName);
                IObjectStreamReader objectStreamReader = objectStreamer.GetStreamReader(streamReader);

                var objectNames = ExcelPool.GetObjectNames(objectStreamReader);
                int n           = objectNames.Count();
                object[,] output = new object[n + 1, 2];
                output[0, 0]     = "Name";
                output[0, 1]     = "Type";
                int j = 1;
                foreach (var obj in objectNames)
                {
                    output[j, 0] = obj.Item2;
                    output[j, 1] = obj.Item1.Name.String;
                    j++;
                }
                objectStreamReader.Close();
                return(ExcelDataConverter.GetExcelRangeOutput(output));
            }
            catch (Exception e)
            {
                return(ExcelDataConverter.GetExcelRangeErrorMessage(e));
            }
        }
        /// <summary>Handles the Click event of the open [file] button.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void openToolStripMenuItem_Click(object sender, EventArgs e)
        {
            StringBuilder filterStrBuilder = new StringBuilder();

            foreach (var objectStreamer in ObjectStreamer.Values)
            {
                if (filterStrBuilder.Length > 1)
                {
                    filterStrBuilder.Append("|");
                }
                filterStrBuilder.Append(objectStreamer.Name.String + " (*." + objectStreamer.FileExtension.String + ")|");
                filterStrBuilder.Append("*." + objectStreamer.FileExtension.String);
            }
            string filter = filterStrBuilder.ToString() + "|All files (*.*)|*.*";

            OpenFileDialog openFileDialog = new OpenFileDialog();

            openFileDialog.CheckFileExists = true;
            openFileDialog.CheckPathExists = true;
            openFileDialog.AddExtension    = true;
            openFileDialog.Multiselect     = false;
            openFileDialog.Title           = "Load Dodoni.net objects";
            openFileDialog.Filter          = filter;

            if (openFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                IObjectStreamer objectStreamer;
                string          fileExtension = ExtendedPath.GetExtension(openFileDialog.FileName);
                if (ObjectStreamer.TryGetObjectStreamerByFileExtension(fileExtension, out objectStreamer) == false)
                {
                    MessageBox.Show("Invalid file extension '" + fileExtension + "'.");
                    return; // exit function
                }
                try
                {
                    StreamReader        streamReader       = new StreamReader(openFileDialog.FileName);
                    IObjectStreamReader objectStreamReader = objectStreamer.GetStreamReader(streamReader);
                    string infoMessage;
                    IEnumerable <ExcelPoolItem> excelPoolItems;
                    if (ExcelPool.TryLoadObjects(objectStreamReader, out infoMessage, out excelPoolItems) == false)
                    {
                        MessageBox.Show("Error: " + infoMessage);
                    }
                    ObjectStreamReaderInfoForm infoForm = new ObjectStreamReaderInfoForm();
                    infoForm.ShowDetails(infoMessage, excelPoolItems);

                    objectStreamReader.Close();
                }
                catch (Exception exception)
                {
                    MessageBox.Show("Error: " + exception.Message);
                }
            }
        }
Пример #3
0
        public static object LoadObjectsFromFile(
            [ExcelArgument(Name = "path", Description = "The path", AllowReference = true)]
            object xlPath,
            [ExcelArgument(Name = "fileName", Description = "The file name", AllowReference = true)]
            object xlFileName,
            [ExcelArgument(Name = "objectNames", Description = "[Optional] A list of object names to load; all objects will be loaded by default", AllowReference = true)]
            object xlObjectNames,
            [ExcelArgument(Name = "fileFormat", Description = "[Optional] The file format; the file extension will be used by default", AllowReference = true)]
            object xlFileFormat)
        {
            try
            {
                IExcelDataQuery fileFormatDataQuery = ExcelDataQuery.Create(xlFileFormat, "File format");

                string          fileName;
                IObjectStreamer objectStreamer;
                if (fileFormatDataQuery.IsEmpty == true)
                {
                    fileName = GetFileName(xlPath, xlFileName, ObjectStreamer.GetFileExtensions());

                    if (ObjectStreamer.TryGetObjectStreamerByFileExtension(ExtendedPath.GetExtension(fileName), out objectStreamer) == false)
                    {
                        throw new ArgumentException("Invalid file extension '" + Path.GetExtension(fileName) + "', used default file extensions or specify the file format.");
                    }
                }
                else
                {
                    if (fileFormatDataQuery.TryGetPoolValue <IObjectStreamer>(ObjectStreamer.TryGetObjectStreamer, out objectStreamer, dataAdvice: ExcelDataAdvice.Create(ObjectStreamer.GetNames())) == false)
                    {
                        throw new ArgumentException("Invalid file format " + fileFormatDataQuery.ToString(0, 0) + ".");
                    }
                    fileName = GetFileName(xlPath, xlFileName, objectStreamer.FileExtension);
                    fileFormatDataQuery.QueryCompleted();
                }

                IExcelDataQuery      objectNamesDataQuery = ExcelDataQuery.Create(xlObjectNames, "Object names");
                IEnumerable <string> objectNames          = objectNamesDataQuery.GetColumnVector <string>();
                objectNamesDataQuery.QueryCompleted();

                StreamReader        streamReader       = new StreamReader(fileName);
                IObjectStreamReader objectStreamReader = objectStreamer.GetStreamReader(streamReader);
                string infoMessage;
                IEnumerable <ExcelPoolItem> excelPoolItems;
                ExcelPool.TryLoadObjectsByName(objectStreamReader, objectNames, out infoMessage, out excelPoolItems);
                objectStreamReader.Close();

                return(infoMessage.ToTimeStampString());
            }
            catch (Exception e)
            {
                return(ExcelDataConverter.GetExcelRangeErrorMessage(e));
            }
        }