文档半岛外围网上直营>>Aspose中文文档>>创建文档
创建文档
Aspose.Words是一种高级Word文档处理API,用于执行各种文档管理和操作任务。API支持生成,修改,转换,呈现和打印文档,而无需在跨平台应用程序中直接使用Microsoft Word。
Aspose API支持流行文件格式处理,并允许将各类文档导出或转换为固定布局文件格式和最常用的图像/多媒体格式。
如何使用 Aspose.Words创建文档
在Aspose.Words中,我们通常使用Document类来创建文档,并使用DocumentBuilder类来修改它。
以下代码示例展示了如何创建文档:
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.Writeln("Hello World!");
doc.Save("CreateDocument.docx");
如何使用 Open XML SDK 创建文档您还可以使用 Open XML SDK 执行相同的操作。 同时请注意,它看起来有些更复杂、更麻烦。
以下是我们需要添加的命名空间:
using DocumentFormat.OpenXml; using DocumentFormat.OpenXml.Packaging; using DocumentFormat.OpenXml.Wordprocessing; using NUnit.Framework;
以下代码示例展示了如何创建文档:
public void CreateADocumentFeature()
{
using (WordprocessingDocument wordDocument =
WordprocessingDocument.Create(ArtifactsDir + "Create a document - OpenXML.docx",
WordprocessingDocumentType.Document))
{
MainDocumentPart mainPart = wordDocument.AddMainDocumentPart();
// Create the document structure and add some text.
mainPart.Document = new Document();
Body body = mainPart.Document.AppendChild(new Body());
Paragraph para = body.AppendChild(new Paragraph());
Run run = para.AppendChild(new Run());
run.AppendChild(new Text("Create text in body - Create wordprocessing document"));
}
}
下载此示例的示例文件。

QQ交谈
在线咨询

渝公网安备
50010702500608号

客服热线