สร้าง DOCX โดยใช้ .NET ห้องสมุด

ไลบรารี C# อันทรงพลังของเราช่วยให้นักพัฒนาสามารถสร้าง DOCX แบบเป็นโปรแกรมได้ภายในไม่กี่ขั้นตอน

.NET นักพัฒนาสามารถสร้าง DOCX ได้อย่างง่ายดายโดยใช้เฉพาะ API ผลิตภัณฑ์ที่มีประสิทธิภาพของเรา หมายความว่าโซลูชันของเราจะมอบทุกสิ่งที่จำเป็นสำหรับโปรแกรมเมอร์ในการสร้าง DOCX ใน C#

ดูข้อมูลโค้ด

สร้าง DOCX ใน C# โดยทางโปรแกรม

ด้วยนักพัฒนาไลบรารี C# ของเรา นักพัฒนาสามารถสร้าง DOCX ตั้งแต่เริ่มต้นได้อย่างง่ายดาย ในการดำเนินการนี้ นักพัฒนา .NET ต้องดำเนินการเพียงไม่กี่ขั้นตอน:

  1. เพิ่มชื่อไฟล์
  2. เริ่มสร้างเอกสาร DOCX โดยใช้ C#
  3. บันทึกไฟล์ DOCX ที่ส่งออก

เป็นที่น่าสังเกตว่าเอกสารเปล่าควรจะมีหนึ่งย่อหน้าในทางเทคนิค ดังนั้นเมื่อคุณสร้างเอกสาร DOCX โดยทางโปรแกรม คุณจะได้โครงสร้างเอกสารพื้นฐานที่แน่นอน

โปรดทราบว่าคุณสามารถเพิ่มเนื้อหาลงในไฟล์ DOCX ที่สร้างขึ้นใหม่ได้ทันที ดังนั้น คุณจะได้ไม่เพียงแค่เอกสารเปล่า แต่ได้เอกสารที่มีเนื้อหาที่จำเป็น สำหรับข้อมูลเพิ่มเติมเกี่ยวกับวิธีการแก้ไขเอกสาร โปรดดูที่หน้าการแก้ไข

สร้าง DOCX ใน C#

ไลบรารี .NET นี้ช่วยให้คุณสร้างเอกสาร DOCX โดยทางโปรแกรม ลองใช้ฟังก์ชันอันทรงพลังของเรา และดูวิธีสร้าง DOCX ในบางรูปแบบโดยใช้ตัวอย่างต่อไปนี้:

สร้าง DOCX ใหม่โดยใช้ C#
เลือกรูปแบบเป้าหมายจากรายการ
รันโค้ด
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 เป็นโมฆะ เป็นไปได้มากว่านี่คือ 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 เป็นโมฆะ เป็นไปได้มากว่านี่คือ 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 เป็นโมฆะ เป็นไปได้มากว่านี่คือ 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 เป็นโมฆะ เป็นไปได้มากว่านี่คือ 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 เป็นโมฆะ เป็นไปได้มากว่านี่คือ 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 เป็นโมฆะ เป็นไปได้มากว่านี่คือ 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 เป็นโมฆะ เป็นไปได้มากว่านี่คือ 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 เป็นโมฆะ เป็นไปได้มากว่านี่คือ 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 เป็นโมฆะ เป็นไปได้มากว่านี่คือ 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);
รันโค้ด

วิธีสร้าง DOCX ใน C#

  1. ติดตั้ง Aspose.Words for .NET
  2. เพิ่มการอ้างอิงไลบรารี (นำเข้าไลบรารี) ไปยัง C# โครงการของคุณ
  3. สร้างเอกสาร DOCX ใหม่
  4. เรียกเมธอด Save() โดยส่งชื่อไฟล์
  5. รับผลลัพธ์เป็นไฟล์แยกต่างหาก

C# ไลบรารีเพื่อสร้าง DOCX

มีทางเลือกสามทางในการติดตั้ง "Aspose.Words for .NET" ลงในระบบของคุณ โปรดเลือกหนึ่งรายการที่ตรงกับความต้องการของคุณและทำตามคำแนะนำทีละขั้นตอน:

ความต้องการของระบบ

ผลิตภัณฑ์ของเราสามารถข้ามแพลตฟอร์มได้อย่างสมบูรณ์และรองรับการใช้งาน .NET หลักๆ ทั้งหมด:

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

ตราบใดที่โค้ด .NET ไม่ได้ขึ้นอยู่กับฮาร์ดแวร์หรือระบบปฏิบัติการพื้นฐาน แต่เฉพาะบน Virtual Machine เท่านั้น คุณมีอิสระที่จะพัฒนาซอฟต์แวร์ทุกประเภทสำหรับ Windows, macOS, Android, iOS และ Linux เพียงตรวจสอบให้แน่ใจว่าคุณได้ติดตั้ง .NET Framework, .NET Core, Windows Azure, Mono หรือ Xamarin เวอร์ชันที่เกี่ยวข้อง

เราขอแนะนำให้ใช้ Microsoft Visual Studio, Xamarin และ MonoDevelop สภาพแวดล้อมการพัฒนาแบบรวมเพื่อสร้างแอปพลิเคชัน C#, F#, VB.NET

รายละเอียดเพิ่มเติมโปรดดูที่ เอกสารประกอบผลิตภัณฑ์

รูปแบบไฟล์อื่นๆ ที่รองรับ

คุณสามารถสร้างเอกสารในรูปแบบไฟล์อื่น:

5%

สมัครสมาชิก Aspose Product Updates

รับจดหมายข่าวและข้อเสนอรายเดือนที่ส่งตรงถึงกล่องจดหมายของคุณ