如何在Excel中创建多个工作表?

时间:2011-06-11 08:04:05

标签: asp.net excel openxml-sdk

我在ASP.Net中使用Excel创建DocumentFormat.OpenXml

任何人都可以知道如何在Excel中创建多个工作表。

对于前。 Sheet1,Sheet2,Sheet3 ...... sheetn

1 个答案:

答案 0 :(得分:1)

尝试以下方法:

        /// <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();
        }

修改

这是包含方法的类:

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

打开这样的Excel文档并调用方法:

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