c# openxml
C#概念 (C# CONCEPTS)
本文演示了如何以编程方式在C#控制台应用程序中使用Excel Document。 (The articles demonstrate how to play with Excel Document in C# Console Application programmatically.)
先决条件 (Prerequisites)
Install below packages from NuGet
从NuGet安装以下软件包
DocumentFormat.OpenXml
Following “Assembly Directives” are used to compile the code
以下“汇编指令”用于编译代码
using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Spreadsheet;
创建电子表格文档 (Create a Spreadsheet Document)
To build an Excel document, construct an instance of the “SpreadsheetDocument” class.
要构建Excel文档,请构建“ SpreadsheetDocument”类的实例。
SpreadsheetDocument spreadsheetDocument = SpreadsheetDocument.Create
(
filepath,
SpreadsheetDocumentType.Workbook
);
There are many constructor overloads for SpreadsheetDocument, but the above instance takes two arguments.
SpreadsheetDocument有许多构造函数重载,但以上实例带有两个参数。
- File Path: The destination path to save the final excel file. 文件路径:保存最终Excel文件的目标路径。
- SpreadSheetDocumentType: It specifies the type of excel document. SpreadSheetDocumentType:它指定excel文档的类型。
SDK中可用的文档类型枚举: (Available document types enum in SDK:)
Select the suitable SpreadsheetDocumentType and ensure that the file path where the output excel file is saved is correct, matching filename extension used.
选择合适的SpreadsheetDocumentType并确保保存输出excel文件的文件路径正确,且匹配使用的文件扩展名。
If the SpreadsheetDocumentType does not match the output file name extension, an error occurs when you open the file.
如果SpreadsheetDocumentType与输出文件扩展名不匹配,则在打开文件时发生错误。