public class SpreadsheetDocument : DocumentFormat.OpenXml.Packaging.OpenXmlPackage
type SpreadsheetDocument = class
inherit OpenXmlPackage
Public Class SpreadsheetDocument
Inherits OpenXmlPackage
SpreadsheetDocument
以下示例创建一个基本的 Excel 文档,一个包含一个工作表的工作簿。 运行代码示例后,查看创建的文件“SpreadsheetDocumentEx.xlsx”,并注意名为“mySheet”的工作表。
using System;
using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Spreadsheet;
namespace SpreadsheetDocumentEx
class Program
static void Main(string[] args)
string fileName = @"c:\Users\Public\Documents\SpreadsheetDocumentEx.xlsx";
// Create a spreadsheet document by supplying the file name.
SpreadsheetDocument spreadsheetDocument = SpreadsheetDocument.
Create(fileName, SpreadsheetDocumentType.Workbook);
// Add a WorkbookPart to the document.
WorkbookPart workbookpart = spreadsheetDocument.AddWorkbookPart();
workbookpart.Workbook = new Workbook();
// Add a WorksheetPart to the WorkbookPart.
WorksheetPart worksheetPart = workbookpart.AddNewPart<WorksheetPart>();
worksheetPart.Worksheet = new Worksheet(new SheetData());
// Add Sheets to the Workbook.
Sheets sheets = spreadsheetDocument.WorkbookPart.Workbook.
AppendChild<Sheets>(new Sheets());
// Append a new worksheet and associate it with the workbook.
Sheet sheet = new Sheet() { Id = spreadsheetDocument.WorkbookPart.
GetIdOfPart(worksheetPart), SheetId = 1, Name = "mySheet" };
sheets.Append(sheet);
// Close the document.
spreadsheetDocument.Close();
Console.WriteLine("The spreadsheet document has been created.\nPress a key.");
Console.ReadKey();
Imports DocumentFormat.OpenXml
Imports DocumentFormat.OpenXml.Packaging
Imports DocumentFormat.OpenXml.Spreadsheet
Module Module1
Sub Main()
' Create a spreadsheet document by supplying the file name.
Dim fileName As String = "c:\Users\Public\Documents\SpreadsheetDocumentEx.xlsx"
Dim spreadsheetDocument As SpreadsheetDocument = _
spreadsheetDocument.Create(fileName, SpreadsheetDocumentType.Workbook)
' Add a WorkbookPart to the document.
Dim workbookpart As WorkbookPart = spreadsheetDocument.AddWorkbookPart
workbookpart.Workbook = New Workbook
' Add a WorksheetPart to the WorkbookPart.
Dim worksheetPart As WorksheetPart = workbookpart.AddNewPart(Of WorksheetPart)()
worksheetPart.Worksheet = New Worksheet(New SheetData())
' Add Sheets to the Workbook.
Dim sheets As Sheets = spreadsheetDocument.WorkbookPart.Workbook.AppendChild(Of Sheets)(New Sheets())
' Append a new worksheet and associate it with the workbook.
Dim sheet As Sheet = New Sheet
sheet.Id = spreadsheetDocument.WorkbookPart.GetIdOfPart(worksheetPart)
sheet.SheetId = 1
sheet.Name = "mySheet"
sheets.Append(sheet)
workbookpart.Workbook.Save()
' Close the document.
spreadsheetDocument.Close()
Console.WriteLine("The spreadsheet document has been created." +
ControlChars.Lf + "Press a key.")
Console.ReadKey()
End Sub
End Module
创建此 OpenXml 包的克隆,该包在给定流中打开。
使用与此 OpenXml 包相同的设置(即 FileOpenAccess 和 OpenSettings)打开克隆的 OpenXml 包。
(继承自 OpenXmlPackage)
创建从给定文件打开的此 OpenXml 包的克隆 (将通过克隆此 OpenXml 包) 创建。
使用与此 OpenXml 包相同的设置(即 FileOpenAccess 和 OpenSettings)打开克隆的 OpenXml 包。
(继承自 OpenXmlPackage)
创建从给定文件打开的此 OpenXml 包的克隆 (将通过克隆此 OpenXml 包) 创建。
使用与此 OpenXml 包相同的 OpenSettings 打开克隆的 OpenXml 包。
(继承自 OpenXmlPackage)