Wiki

Clone wiki

Aspose for OpenXML / Insert a new worksheet

OpenXML Excel:

using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Spreadsheet;

class Program
    {
        static void Main(string[] args)
        {
            string docName = @"E:\Dropbox\Personal\Aspose Vs OpenXML\Files\Insert a new worksheet.xlsx";
            InsertWorksheet(docName);
        }

        private static void InsertWorksheet(string docName)
        {
            // Open the document for editing.
            using (SpreadsheetDocument spreadSheet = SpreadsheetDocument.Open(docName, true))
            {
                // Add a blank WorksheetPart.
                WorksheetPart newWorksheetPart = spreadSheet.WorkbookPart.AddNewPart<WorksheetPart>();
                newWorksheetPart.Worksheet = new Worksheet(new SheetData());

                Sheets sheets = spreadSheet.WorkbookPart.Workbook.GetFirstChild<Sheets>();
                string relationshipId = spreadSheet.WorkbookPart.GetIdOfPart(newWorksheetPart);

                // Get a unique ID for the new worksheet.
                uint sheetId = 1;
                if (sheets.Elements<Sheet>().Count() > 0)
                {
                    sheetId = sheets.Elements<Sheet>().Select(s => s.SheetId.Value).Max() + 1;
                }

                // Give the new worksheet a name.
                string sheetName = "Sheet" + sheetId;

                // Append the new worksheet and associate it with the workbook.
                Sheet sheet = new Sheet() { Id = relationshipId, SheetId = sheetId, Name = sheetName };
                sheets.Append(sheet);
            }
        }
    }

Aspose.Cells:

class Program
    {
        static void Main(string[] args)
        {
            string docName = "Insert a new worksheet.xlsx";
            InsertWorksheet(docName);
        }

        private static void InsertWorksheet(string docName)
        {
            //Instantiating a Workbook object
            Workbook workbook = new Workbook(docName);

            //Adding a new worksheet to the Excel object
            int SheetIndex = workbook.Worksheets.Add();

            //Saving the Excel file
            workbook.Save(docName);
        }
    }

Download

Updated