示例#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));
            }
        }
示例#2
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));
            }
        }