Pankaj Agarwal
Pankaj Agarwal

Reputation: 11309

How to create Multiple worksheet in Excel?

I am creating Excel using DocumentFormat.OpenXml in ASP.Net.

Can anybody have idea how can create Multiple worksheet in Excel.

For ex. Sheet1, Sheet2, Sheet3...... sheetn

Upvotes: 0

Views: 2994

Answers (1)

amurra
amurra

Reputation: 15411

Try the following method:

        /// <summary>
        /// Add a blank worksheet to the workbook
        /// </summary>
        /// <param name="workbookPart">Wookbook part</param>
        public static void InsertBlankWorksheet(WorkbookPart workbookPart)
        {
            // Add a blank WorksheetPart.
            WorksheetPart newWorksheetPart = workbookPart.AddNewPart<WorksheetPart>();

            // Create the new worksheet
            Worksheet worksheet = new Worksheet();
            worksheet.AddNamespaceDeclaration("r", "http://schemas.openxmlformats.org/officeDocument/2006/relationships");
            SheetDimension sheetDimension1 = new SheetDimension() { Reference = "A1" };

            SheetViews sheetViews1 = new SheetViews();
            SheetView sheetView1 = new SheetView() { TabSelected = true, WorkbookViewId = (UInt32Value)0U };

            sheetViews1.Append(sheetView1);
            SheetFormatProperties sheetFormatProperties1 = new SheetFormatProperties() { DefaultRowHeight = 15D };
            SheetData sheetData1 = new SheetData();
            PageMargins pageMargins1 = new PageMargins() { Left = 0.7D, Right = 0.7D, Top = 0.75D, Bottom = 0.75D, Header = 0.3D, Footer = 0.3D };
            PageSetup pageSetup1 = new PageSetup() { Orientation = OrientationValues.Portrait, Id = "rId1" };

            worksheet.Append(sheetDimension1);
            worksheet.Append(sheetViews1);
            worksheet.Append(sheetFormatProperties1);
            worksheet.Append(sheetData1);
            worksheet.Append(pageMargins1);
            worksheet.Append(pageSetup1);

            newWorksheetPart.Worksheet = worksheet;
            newWorksheetPart.Worksheet.Save();

            Sheets sheets = workbookPart.Workbook.GetFirstChild<Sheets>();
            string relationshipId = 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);
            workbookPart.Workbook.Save();
        }

EDIT

Here is the class that contains the method:

public static class ExcelHelpers
{
    public static void InsertBlankWorksheet(WorkbookPart workbookPart)
    {...}
}

Open up your excel document like this and call the method:

public static void Export(string document)
{
    using (SpreadsheetDocument doc = SpreadsheetDocument.Open(document, true))
    {
        ExcelHelpers.InsertBlankWorksheet(doc.WorkbookPart);
    }
}

Upvotes: 1

Related Questions