Insert a new worksheet

OpenXML Excel

 string FilePath = @"..\..\..\..\Sample Files\";

string FileName = FilePath + "Insert a new worksheet.xlsx";

InsertWorksheet(FileName);

}

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

string FilePath = @"..\..\..\..\Sample Files\";

string FileName = FilePath + "Insert a new worksheet.xlsx";

InsertWorksheet(FileName);

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 Sample Code

  • GitHub
  • Sourceforge
  • Bitbucket