使用我們的 C# 庫,開發人員可以輕鬆地從頭開始製作 DOCX。為此,.NET 開發人員只需執行幾個步驟:
值得注意的是,從技術上講,空白文檔應該包含一個段落,因此當您以編程方式創建 DOCX 文檔時,您將獲得完全一致的基本文檔結構。
請注意,您可以立即將內容添加到新創建的 DOCX 文件中。因此,您將獲得的不僅僅是一個空文檔,而是一個包含必要內容的文檔。有關如何編輯文檔的更多信息,請參閱編輯頁面。
此 .NET 庫允許您以編程方式創建 DOCX 文檔。試試我們強大的功能,看看如何使用以下示例以某些格式創建 DOCX:
dotnet add package Aspose.Words
複製
using Aspose.Words;
var doc = new Document();
var builder = new DocumentBuilder(doc);
var font = builder.Font;
font.Name = "Courier New";
font.Color = Color.Blue;
font.Size = 36;
font.HighlightColor = Color.Yellow;
builder.Write("Morbi enim nunc faucibus a.");
doc.Save("Output.docx");
using Aspose.Words;
var doc = new Document();
var builder = new DocumentBuilder(doc);
var firstRun = new Run(doc, "Proin eros metus, sagittis sed. ");
var secondRun = new Run(doc, "Morbi enim nunc faucibus a.");
doc.FirstSection.Body.FirstParagraph.AppendChild(firstRun);
doc.FirstSection.Body.FirstParagraph.AppendChild(secondRun);
builder.MoveTo(secondRun);
builder.StartBookmark("Aspose bookmark");
// 如果 NextSibling 為 null,很可能這是該段落中的最後一次 Run 。
if (secondRun.NextSibling != null)
builder.MoveTo(secondRun.NextSibling);
else
builder.MoveTo(secondRun.ParentParagraph);
builder.EndBookmark("Aspose bookmark");
doc.Save("Output.docx");
using Aspose.Words;
var doc = new Document();
var builder = new DocumentBuilder(doc);
var firstRun = new Run(doc, "Proin eros metus, sagittis sed. ");
var secondRun = new Run(doc, "Morbi enim nunc faucibus a.");
doc.FirstSection.Body.FirstParagraph.AppendChild(firstRun);
doc.FirstSection.Body.FirstParagraph.AppendChild(secondRun);
builder.MoveTo(secondRun);
builder.StartBookmark("Aspose bookmark");
// 如果 NextSibling 為 null,很可能這是該段落中的最後一次 Run 。
if (secondRun.NextSibling != null)
builder.MoveTo(secondRun.NextSibling);
else
builder.MoveTo(secondRun.ParentParagraph);
builder.EndBookmark("Aspose bookmark");
var saveOptions = new PdfSaveOptions();
saveOptions.OutlineOptions.BookmarksOutlineLevels.Add("Aspose bookmark", 1);
doc.Save("Output.docx", saveOptions);
using Aspose.Words;
var doc = new Document();
var run = new Run(doc, "Proin eros metus, sagittis sed.");
var para = doc.FirstSection.Body.FirstParagraph;
para.AppendChild(run);
var comment = new Comment(doc)
{
Author = "John Doe",
Initial = "JD",
DateTime = DateTime.Now
};
comment.SetText("Quisque fringilla leo.");
var commentRangeStart = new CommentRangeStart(doc, comment.Id);
var commentRangeEnd = new CommentRangeEnd(doc, comment.Id);
run.ParentNode.InsertBefore(commentRangeStart, run);
run.ParentNode.InsertAfter(commentRangeEnd, run);
commentRangeEnd.ParentNode.InsertAfter(comment, commentRangeEnd);
comment.AddReply("Jane Doe", "JD", DateTime.Now, "Pellentesque vel sapien justo.");
doc.Save("Output.docx");
using Aspose.Words;
var doc = new Document();
var builder = new DocumentBuilder(doc);
var firstRun = new Run(doc, "Proin eros metus, sagittis sed. ");
var secondRun = new Run(doc, "Morbi enim nunc faucibus a.");
doc.FirstSection.Body.FirstParagraph.AppendChild(firstRun);
doc.FirstSection.Body.FirstParagraph.AppendChild(secondRun);
builder.MoveTo(secondRun);
builder.StartBookmark("Aspose bookmark");
// 如果 NextSibling 為 null,很可能這是該段落中的最後一次 Run 。
if (secondRun.NextSibling != null)
builder.MoveTo(secondRun.NextSibling);
else
builder.MoveTo(secondRun.ParentParagraph);
builder.EndBookmark("Aspose bookmark");
doc.Save("Output.docx");
using Aspose.Words;
var doc = new Document();
var builder = new DocumentBuilder(doc);
var firstRun = new Run(doc, "Proin eros metus, sagittis sed. ");
var secondRun = new Run(doc, "Morbi enim nunc faucibus a.");
doc.FirstSection.Body.FirstParagraph.AppendChild(firstRun);
doc.FirstSection.Body.FirstParagraph.AppendChild(secondRun);
builder.MoveTo(secondRun);
builder.StartBookmark("Aspose bookmark");
// 如果 NextSibling 為 null,很可能這是該段落中的最後一次 Run 。
if (secondRun.NextSibling != null)
builder.MoveTo(secondRun.NextSibling);
else
builder.MoveTo(secondRun.ParentParagraph);
builder.EndBookmark("Aspose bookmark");
var saveOptions = new PdfSaveOptions();
saveOptions.OutlineOptions.BookmarksOutlineLevels.Add("Aspose bookmark", 1);
doc.Save("Output.docx", saveOptions);
using Aspose.Words;
var doc = new Document();
var builder = new DocumentBuilder(doc);
var firstRun = new Run(doc, "Proin eros metus, sagittis sed. ");
var secondRun = new Run(doc, "Morbi enim nunc faucibus a.");
doc.FirstSection.Body.FirstParagraph.AppendChild(firstRun);
doc.FirstSection.Body.FirstParagraph.AppendChild(secondRun);
builder.MoveTo(secondRun);
builder.StartBookmark("Aspose bookmark");
// 如果 NextSibling 為 null,很可能這是該段落中的最後一次 Run 。
if (secondRun.NextSibling != null)
builder.MoveTo(secondRun.NextSibling);
else
builder.MoveTo(secondRun.ParentParagraph);
builder.EndBookmark("Aspose bookmark");
doc.Save("Output.docx");
using Aspose.Words;
var doc = new Document();
var builder = new DocumentBuilder(doc);
var firstRun = new Run(doc, "Proin eros metus, sagittis sed. ");
var secondRun = new Run(doc, "Morbi enim nunc faucibus a.");
doc.FirstSection.Body.FirstParagraph.AppendChild(firstRun);
doc.FirstSection.Body.FirstParagraph.AppendChild(secondRun);
builder.MoveTo(secondRun);
builder.StartBookmark("Aspose bookmark");
// 如果 NextSibling 為 null,很可能這是該段落中的最後一次 Run 。
if (secondRun.NextSibling != null)
builder.MoveTo(secondRun.NextSibling);
else
builder.MoveTo(secondRun.ParentParagraph);
builder.EndBookmark("Aspose bookmark");
doc.Save("Output.docx");
using Aspose.Words;
var doc = new Document();
var builder = new DocumentBuilder(doc);
var firstRun = new Run(doc, "Proin eros metus, sagittis sed. ");
var secondRun = new Run(doc, "Morbi enim nunc faucibus a.");
doc.FirstSection.Body.FirstParagraph.AppendChild(firstRun);
doc.FirstSection.Body.FirstParagraph.AppendChild(secondRun);
builder.MoveTo(secondRun);
builder.StartBookmark("Aspose bookmark");
// 如果 NextSibling 為 null,很可能這是該段落中的最後一次 Run 。
if (secondRun.NextSibling != null)
builder.MoveTo(secondRun.NextSibling);
else
builder.MoveTo(secondRun.ParentParagraph);
builder.EndBookmark("Aspose bookmark");
var saveOptions = new PdfSaveOptions();
saveOptions.OutlineOptions.BookmarksOutlineLevels.Add("Aspose bookmark", 1);
doc.Save("Output.docx", saveOptions);
using Aspose.Words;
var doc = new Document();
var builder = new DocumentBuilder(doc);
Shape shape = builder.InsertChart(ChartType.Pie, 432, 252);
Chart chart = shape.Chart;
chart.Title.Text = "Demo Chart";
chart.Series.Clear();
chart.Series.Add("Series 1",
new string[] { "Category1", "Category2", "Category3" },
new double[] { 2.7, 3.2, 0.8 });
doc.Save("Output.docx");
using Aspose.Words;
var doc = new Document();
var builder = new DocumentBuilder(doc);
var firstRun = new Run(doc, "Proin eros metus, sagittis sed. ");
var secondRun = new Run(doc, "Morbi enim nunc faucibus a.");
doc.FirstSection.Body.FirstParagraph.AppendChild(firstRun);
doc.FirstSection.Body.FirstParagraph.AppendChild(secondRun);
builder.MoveTo(secondRun);
builder.StartBookmark("Aspose bookmark");
// 如果 NextSibling 為 null,很可能這是該段落中的最後一次 Run 。
if (secondRun.NextSibling != null)
builder.MoveTo(secondRun.NextSibling);
else
builder.MoveTo(secondRun.ParentParagraph);
builder.EndBookmark("Aspose bookmark");
doc.Save("Output.docx");
using Aspose.Words;
var doc = new Document();
var builder = new DocumentBuilder(doc);
var firstRun = new Run(doc, "Proin eros metus, sagittis sed. ");
var secondRun = new Run(doc, "Morbi enim nunc faucibus a.");
doc.FirstSection.Body.FirstParagraph.AppendChild(firstRun);
doc.FirstSection.Body.FirstParagraph.AppendChild(secondRun);
builder.MoveTo(secondRun);
builder.StartBookmark("Aspose bookmark");
// 如果 NextSibling 為 null,很可能這是該段落中的最後一次 Run 。
if (secondRun.NextSibling != null)
builder.MoveTo(secondRun.NextSibling);
else
builder.MoveTo(secondRun.ParentParagraph);
builder.EndBookmark("Aspose bookmark");
var saveOptions = new PdfSaveOptions();
saveOptions.OutlineOptions.BookmarksOutlineLevels.Add("Aspose bookmark", 1);
doc.Save("Output.docx", saveOptions);
在您的系統上 "Aspose.Words for .NET" 有三個備選選項。請選擇符合您需求的一種,並按照分步說明進行操作:
我們的產品完全跨平台,支援所有主要的 .NET 實作:
由於 .NET 程式碼不依賴底層硬體或作業系統,而僅依賴虛擬機,因此您可以自由地為 Windows、macOS、Android、iOS 和 Linux 開發任何類型的軟體。只要確保您已安裝對應版本的.NET Framework, .NET Core, Windows Azure, Mono或Xamarin即可。
我們建議使用 Microsoft Visual Studio、Xamarin 和 MonoDevelop 整合開發環境來建立C#, F#, VB.NET應用程式。
更多詳細資訊請參閱產品文件。