Ejemplo n.º 1
0
        public static ExcelDocument CreateWorkbook(Package package)
        {
            SpreadsheetDocumentType type = SpreadsheetDocumentType.Workbook;
            SpreadsheetDocument     doc  = SpreadsheetDocument.Create(package, type);

            return(CreateBlankWorkbook(doc));
        }
Ejemplo n.º 2
0
        public static ExcelDocument CreateWorkbook(Stream stream)
        {
            SpreadsheetDocumentType type = SpreadsheetDocumentType.Workbook;
            SpreadsheetDocument     doc  = SpreadsheetDocument.Create(stream, type);

            return(CreateBlankWorkbook(doc));
        }
Ejemplo n.º 3
0
        public static ExcelDocument CreateWorkbook(string path, bool autoSave)
        {
            SpreadsheetDocumentType type = SpreadsheetDocumentType.Workbook;
            SpreadsheetDocument     doc  = SpreadsheetDocument.Create(path, type, autoSave);

            return(CreateBlankWorkbook(doc));
        }
Ejemplo n.º 4
0
 /// <summary>
 /// 导出
 /// </summary>
 /// <returns></returns>
 public void Export(Stream stream, SpreadsheetDocumentType type = SpreadsheetDocumentType.Workbook)
 {
     using (SpreadsheetDocument document = SpreadsheetDocument.Create(stream, SpreadsheetDocumentType.Workbook))
     {
         CreateParts(document);
     }
 }
Ejemplo n.º 5
0
        /// <summary>
        /// Creates a new instance of the SpreadsheetDocument class from the specified package.
        /// </summary>
        /// <param name="package">The specified OpenXml package.</param>
        /// <param name="type">The type of the SpreadsheetDocument.</param>
        /// <param name="autoSave">Whether to auto save the created document.</param>
        /// <returns>A new instance of SpreadsheetDocument.</returns>
        /// <exception cref="ArgumentNullException">Thrown when "package" is null reference.</exception>
        /// <exception cref="IOException">Thrown when "package" is not opened with Write access.</exception>
        public static SpreadsheetDocument Create(Package package, SpreadsheetDocumentType type, bool autoSave)
        {
            SpreadsheetDocument doc = new SpreadsheetDocument();

            doc.DocumentType          = type;
            doc.OpenSettings          = new OpenSettings();
            doc.OpenSettings.AutoSave = autoSave;
            doc.MainPartContentType   = MainPartContentTypes[type];
            doc.CreateCore(package);
            return(doc);
        }
        /// <summary>
        /// Копирует лист
        /// </summary>
        /// <param name="ws">Исходный лист</param>
        /// <param name="newSheetName">Имя нового листа</param>
        /// <param name="docType">Тип исходного листа SpreadsheetDocumentType</param>
        public static Worksheet Duplicate(this Worksheet ws, string newSheetName, SpreadsheetDocumentType docType = SpreadsheetDocumentType.Workbook)
        {
            var sourceSheetPart                   = ws.WorksheetPart;
            SpreadsheetDocument tempSheet         = SpreadsheetDocument.Create(new MemoryStream(), docType);
            WorkbookPart        tempWorkbookPart  = tempSheet.AddWorkbookPart();
            WorksheetPart       tempWorksheetPart = tempWorkbookPart.AddPart(sourceSheetPart);

            var WbPart = ws.GetWorkbookPart();
            //Add cloned sheet and all associated parts to workbook
            WorksheetPart clonedSheet = WbPart.AddPart <WorksheetPart>(tempWorksheetPart);
            //Table definition parts are somewhat special and need unique ids...so let's make an id based on count
            int numTableDefParts = sourceSheetPart.GetPartsCountOfType <TableDefinitionPart>();

            //Clean up table definition parts (tables need unique ids)
            if (numTableDefParts != 0)
            {
                //Every table needs a unique id and name
                foreach (TableDefinitionPart tableDefPart in clonedSheet.TableDefinitionParts)
                {
                    numTableDefParts++;
                    tableDefPart.Table.Id          = (uint)numTableDefParts;
                    tableDefPart.Table.DisplayName = "CopiedTable" + numTableDefParts;
                    tableDefPart.Table.Name        = "CopiedTable" + numTableDefParts;
                    tableDefPart.Table.Save();
                }
            }

            //There can only be one sheet that has focus
            SheetViews views = clonedSheet.Worksheet.GetFirstChild <SheetViews>();

            if (views != null)
            {
                views.Remove();
                clonedSheet.Worksheet.Save();
            }

            //Add new sheet to main workbook part
            Sheets sheets      = WbPart.Workbook.GetFirstChild <Sheets>();
            Sheet  copiedSheet = new Sheet
            {
                Name    = newSheetName,
                Id      = WbPart.GetIdOfPart(clonedSheet),
                SheetId = (uint)sheets.ChildElements.Count + 1
            };

            sheets.Append(copiedSheet);
            //Save Changes
            WbPart.Workbook.Save();

            return(clonedSheet.Worksheet);
        }
Ejemplo n.º 7
0
        private void UpdateDocumentTypeFromContentType()
        {
            if (MainPartContentType is null)
            {
                throw new InvalidOperationException();
            }

            foreach (KeyValuePair <SpreadsheetDocumentType, string> types in MainPartContentTypes)
            {
                if (types.Value == MainPartContentType)
                {
                    DocumentType = types.Key;
                }
            }
        }
Ejemplo n.º 8
0
        public static SpreadsheetDocument Create(Package package, SpreadsheetDocumentType type, bool autoSave)
        {
            var doc = new SpreadsheetDocument
            {
                DocumentType = type,
                OpenSettings = new OpenSettings {
                    AutoSave = autoSave
                },
                MainPartContentType = MainPartContentTypes[type],
            };

            doc.CreateCore(package);

            return(doc);
        }
Ejemplo n.º 9
0
        /// <summary>
        /// Creates a new instance of the SpreadsheetDocument class from the specified file.
        /// </summary>
        /// <param name="path">The path and file name of the target SpreadsheetDocument.</param>
        /// <param name="type">The type of the SpreadsheetDocument.</param>
        /// <param name="autoSave">Whether to auto save the created document.</param>
        /// <returns>A new instance of SpreadsheetDocument.</returns>
        /// <exception cref="ArgumentNullException">Thrown when "path" is null reference.</exception>
        public static SpreadsheetDocument Create(string path, SpreadsheetDocumentType type, bool autoSave)
        {
            if (string.IsNullOrEmpty(path))
            {
                throw new ArgumentNullException(nameof(path));
            }
            SpreadsheetDocument doc = new SpreadsheetDocument();

            doc.DocumentType          = type;
            doc.OpenSettings          = new OpenSettings();
            doc.OpenSettings.AutoSave = autoSave;
            doc.MainPartContentType   = MainPartContentTypes[type];
            doc.CreateCore(path);
            return(doc);
        }
Ejemplo n.º 10
0
        public void SavesAsProperSpreadsheetDocumentType(string extension, SpreadsheetDocumentType expectedType)
        {
            using (var tf = new TemporaryFile(Path.ChangeExtension(Path.GetTempFileName(), extension)))
            {
                using (var wb = new XLWorkbook())
                {
                    wb.Worksheets.Add("Sheet1");
                    wb.SaveAs(tf.Path);
                }

                using (var package = SpreadsheetDocument.Open(tf.Path, false))
                {
                    Assert.AreEqual(expectedType, package.DocumentType);
                }
            }
        }
        public static SpreadsheetFile NewSpreadsheetFile(string filePath, SpreadsheetDocumentType documentType)
        {
            try
            {
                SpreadsheetFile newFile = new SpreadsheetFile(filePath);
                if (newFile.fileSystemInfo.Exists)
                {
                    throw new Exception("file already exist in the given path");
                }
                if (documentType != SpreadsheetDocumentType.Workbook)
                {
                    throw new Exception("'DevEx.OOXml.SpreadsheetFile' currently supports only 'SpreadsheetDocumentType.Workbook' type.");
                }

                newFile.documentXmlPackage = SpreadsheetDocument.Create(filePath, documentType);
                newFile.InitializeWorkbook();

                return(newFile);
            }
            catch { throw; }
        }
Ejemplo n.º 12
0
        public void ChangeDocumentType(SpreadsheetDocumentType newType)
        {
            ThrowIfObjectDisposed();

            if (newType == DocumentType)
            {
                // same type, just return
                return;
            }

            if (FileOpenAccess == FileAccess.Read)
            {
                throw new IOException(ExceptionMessages.PackageAccessModeIsReadonly);
            }

            SpreadsheetDocumentType oldType = DocumentType;

            DocumentType        = newType;
            MainPartContentType = MainPartContentTypes[newType];

            if (WorkbookPart is null)
            {
                return;
            }

            try
            {
                ChangeDocumentTypeInternal <WorkbookPart>();
            }
            catch (OpenXmlPackageException e)
            {
                if (e.Message == ExceptionMessages.CannotChangeDocumentType)
                {
                    DocumentType        = oldType;
                    MainPartContentType = MainPartContentTypes[oldType];
                }

                throw;
            }
        }
Ejemplo n.º 13
0
 public static SpreadsheetDocument Create(Package package, SpreadsheetDocumentType type)
 {
     return(Create(package, type, true));
 }
Ejemplo n.º 14
0
 public static SpreadsheetDocument Create(Stream stream, SpreadsheetDocumentType type)
 {
     return(Create(stream, type, true));
 }
Ejemplo n.º 15
0
 public static SpreadsheetDocument Create(string path, SpreadsheetDocumentType type)
 {
     return(Create(path, type, true));
 }
Ejemplo n.º 16
0
        public static ExcelDocument CreateWorkbook(Package package, bool autoSave)
        {
            SpreadsheetDocumentType workbook = SpreadsheetDocumentType.Workbook;

            return(CreateBlankWorkbook(SpreadsheetDocument.Create(package, workbook, autoSave)));
        }
Ejemplo n.º 17
0
        private void CreatePackage(Stream stream, bool newStream, SpreadsheetDocumentType spreadsheetDocumentType)
        {
            var package = newStream
                ? SpreadsheetDocument.Create(stream, spreadsheetDocumentType)
                : SpreadsheetDocument.Open(stream, true);

            using (package)
            {
                CreateParts(package);
                //package.Close();
            }
        }
Ejemplo n.º 18
0
        private void CreatePackage(String filePath, SpreadsheetDocumentType spreadsheetDocumentType)
        {
            PathHelper.CreateDirectory(Path.GetDirectoryName(filePath));
            var package = File.Exists(filePath)
                ? SpreadsheetDocument.Open(filePath, true)
                : SpreadsheetDocument.Create(filePath, spreadsheetDocumentType);

            using (package)
            {
                CreateParts(package);
                //package.Close();
            }
        }
Ejemplo n.º 19
0
        public static ExcelDocument CreateWorkbook(string path)
        {
            SpreadsheetDocumentType workbook = SpreadsheetDocumentType.Workbook;

            return(CreateBlankWorkbook(SpreadsheetDocument.Create(path, workbook)));
        }
Ejemplo n.º 20
0
        public static ExcelDocument CreateWorkbook(Stream stream, bool autoSave)
        {
            SpreadsheetDocumentType workbook = SpreadsheetDocumentType.Workbook;

            return(CreateBlankWorkbook(SpreadsheetDocument.Create(stream, workbook, autoSave)));
        }
Ejemplo n.º 21
0
 public XLWorkbook(String file, XLEventTracking eventTracking)
     : this(eventTracking)
 {
     _loadSource = XLLoadSource.File;
     _originalFile = file;
     _spreadsheetDocumentType = GetSpreadsheetDocumentType(_originalFile);
     Load(file);
 }
Ejemplo n.º 22
0
        /// <summary>
        ///     Copies a sheet with a given sheet name and saves it with the clonedSheetName in the workbook
        /// </summary>
        /// <param name="docType">DocType to created for temp file in the method</param>
        /// <param name="workbookPart">Workbook which the worksheet to be copied and the copied worksheet to be saved</param>
        /// <param name="sheetName">Name of the worksheet to copy</param>
        /// <param name="clonedSheetName">New name of the copied sheet</param>
        private void copySheet(SpreadsheetDocumentType docType,
                               WorkbookPart workbookPart, string sheetName,
                               string clonedSheetName)
        {
            //Get the source sheet to be copied
            WorksheetPart sourceSheetPart = getWorksheetPart(workbookPart,
                                                             sheetName);
            //Take advantage of AddPart for deep cloning
            SpreadsheetDocument tempSheet =
                SpreadsheetDocument.Create(new MemoryStream(), docType);
            WorkbookPart tempWorkbookPart = tempSheet.AddWorkbookPart();
            WorksheetPart tempWorksheetPart =
                tempWorkbookPart.AddPart(sourceSheetPart);
            //Add cloned sheet and all associated parts to workbook
            WorksheetPart clonedSheet = workbookPart.AddPart(tempWorksheetPart);

            //Table definition parts are somewhat special and need unique ids...so let's make an id based on count
            int numTableDefParts =
                sourceSheetPart.GetPartsCountOfType<TableDefinitionPart>();
            _tableId = numTableDefParts;
            //Clean up table definition parts (tables need unique ids)
            if (numTableDefParts != 0) {
                fixupTableParts(clonedSheet);
            }
            //There should only be one sheet that has focus
            cleanView(clonedSheet);

            //Add new sheet to main workbook part
            var sheets = workbookPart.Workbook.GetFirstChild<Sheets>();
            var copiedSheet = new Sheet();
            copiedSheet.Name = clonedSheetName;
            copiedSheet.Id = workbookPart.GetIdOfPart(clonedSheet);
            copiedSheet.SheetId = (uint) sheets.ChildElements.Count + 1;
            sheets.Append(copiedSheet);
            //Save Changes
            workbookPart.Workbook.Save();
        }