Tạo DOCX bằng cách sử dụng .NET thư viện

Thư viện C# mạnh mẽ của chúng tôi cho phép các nhà phát triển tạo lập trình DOCX chỉ trong vài bước

.NET các nhà phát triển có thể dễ dàng thực hiện DOCX chỉ bằng cách sử dụng API sản phẩm mạnh mẽ của chúng tôi. Có nghĩa là giải pháp của chúng tôi sẽ cung cấp cho các lập trình viên mọi thứ họ cần để tạo DOCX trong C#.

Xem đoạn mã

Tạo DOCX trong C# theo chương trình

Với thư viện C# của chúng tôi, các nhà phát triển có thể dễ dàng tạo DOCX từ đầu. Để làm điều này, các nhà phát triển .NET chỉ cần thực hiện một vài bước:

  1. Thêm tên tệp
  2. Bắt đầu tạo DOCX tài liệu bằng C#
  3. Lưu DOCX tệp đầu ra

Điều đáng chú ý là về mặt kỹ thuật, một tài liệu trống được cho là chứa một đoạn văn, vì vậy khi bạn tạo DOCX tài liệu theo chương trình, bạn sẽ nhận được chính xác cấu trúc tài liệu cơ bản đó.

Lưu ý rằng bạn có thể thêm ngay nội dung vào DOCX tệp mới được tạo. Vì vậy, bạn sẽ nhận được không chỉ là một tài liệu trống, mà là một tài liệu có chứa nội dung cần thiết. Để biết thêm thông tin về cách chỉnh sửa tài liệu, hãy xem trang Chỉnh sửa.

Tạo DOCX trong C#

Thư viện .NET này cho phép bạn tạo DOCX tài liệu theo chương trình. Hãy thử chức năng mạnh mẽ của chúng tôi và xem cách tạo DOCX ở một số định dạng bằng cách sử dụng ví dụ sau:

Tạo một DOCX mới bằng cách sử dụng C#
Chọn định dạng mục tiêu từ danh sách
Mã vận hành
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"); // Nếu NextSibling là null thì rất có thể đây là Run cuối cùng trong Đoạn. 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"); // Nếu NextSibling là null thì rất có thể đây là Run cuối cùng trong Đoạn. 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"); // Nếu NextSibling là null thì rất có thể đây là Run cuối cùng trong Đoạn. 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"); // Nếu NextSibling là null thì rất có thể đây là Run cuối cùng trong Đoạn. 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");
// Nếu NextSibling là null thì rất có thể đây là Run cuối cùng trong Đoạn.
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"); // Nếu NextSibling là null thì rất có thể đây là Run cuối cùng trong Đoạn. 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"); // Nếu NextSibling là null thì rất có thể đây là Run cuối cùng trong Đoạn. 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"); // Nếu NextSibling là null thì rất có thể đây là Run cuối cùng trong Đoạn. 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"); // Nếu NextSibling là null thì rất có thể đây là Run cuối cùng trong Đoạn. 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);
Mã vận hành

Cách tạo DOCX trong C#

  1. Cài đặt Aspose.Words for .NET
  2. Thêm tham chiếu thư viện (nhập thư viện) vào dự án C# của bạn
  3. Tạo DOCX tài liệu mới
  4. Gọi phương thức Save(), chuyển tên tệp
  5. Nhận kết quả dưới dạng một tệp riêng biệt

C# thư viện để tạo DOCX

Có ba tùy chọn thay thế để cài đặt "Aspose.Words for .NET" vào hệ thống của bạn. Vui lòng chọn một cái giống với nhu cầu của bạn và làm theo hướng dẫn từng bước:

yêu cầu hệ thống

Sản phẩm của chúng tôi hoàn toàn đa nền tảng và hỗ trợ tất cả các triển khai .NET chính:

  • .NET ≥ 5.0
  • .NET Core ≥ 2.0
  • .NET Standard ≥ 2.0
  • .NET Framework ≥ 3.5
  • MonoMac
  • MonoAndroid
  • Xamarin

Vì mã .NET không phụ thuộc vào phần cứng hoặc hệ điều hành cơ bản mà chỉ phụ thuộc vào Máy ảo, bạn có thể tự do phát triển bất kỳ loại phần mềm nào cho Windows, macOS, Android, iOS và Linux. Chỉ cần đảm bảo rằng bạn đã cài đặt phiên bản tương ứng của .NET Framework, .NET Core, Windows Azure, Mono hoặc Xamarin.

Chúng tôi khuyên bạn nên sử dụng các môi trường phát triển tích hợp Microsoft Visual Studio, Xamarin và MonoDevelop để tạo các ứng dụng C#, F#, VB.NET.

Để biết thêm chi tiết, vui lòng tham khảo Tài liệu sản phẩm.

Các định dạng tệp được hỗ trợ khác

Bạn có thể tạo tài liệu ở các định dạng tệp khác:

5%

Đăng ký cập nhật sản phẩm của Aspose

Nhận bản tin hàng tháng và ưu đãi gửi trực tiếp đến hộp thư của bạn.

© Aspose Pty Ltd 2001-2024. Đã đăng ký Bản quyền.