
buat Dokumen MS Word di C #
- Buat Dokumen MS Word di C# - instalasi API
- Bagaimana cara membuat dokumen Word secara terprogram?
- Tambahkan paragraf dalam file Word menggunakan C#
- Cara menyisipkan tabel dalam file MS Word di C#
- Menambahkan daftar dalam dokumen Word secara terprogram
- Bagaimana cara mengedit Font dokumen Word di aplikasi .NET?
Buat Dokumen MS Word di C# - instalasi API
Anda dapat menginstal API .Net ini dengan dua cara. Unduh file DLL untuk mengaktifkan perpustakaan ini di aplikasi .NET Anda. Atau, Anda dapat menginstalnya dengan menjalankan perintah berikut di pengelola paket NuGet.
Install-Package Aspose.Words
Bagaimana cara membuat dokumen Word secara terprogram?
Setelah berhasil menginstal .NET Word automation API, kita siap untuk menulis kode dalam C# untuk membuat dokumen MS Word pertama kita secara terprogram. Anda dapat mengikuti langkah-langkah berikut dan cuplikan kode untuk membuat Dokumen Word di C#.
- Buat objek dari kelas Document untuk membuat file MS Word kosong.
- Menginisialisasi instance dari kelas DocumentBuilder dan melampirkannya ke objek Dokumen baru.
- Aktifkan metode Write untuk memasukkan string ke dalam dokumen.
- Panggil metode save untuk menyimpan file MS Word.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
// Create an object of the Document class to create a blank MS Word file. Document doc = new Document(); // Initialize an instance of DocumentBuilder class and attach it to a new Document object DocumentBuilder builder = new DocumentBuilder(doc); // Invoke Write method to insert a string into the document builder.Write("this is start of the page "); // call the save method to save the MS Word file doc.Save("./output.docx");
Tambahkan paragraf dalam file Word menggunakan C#
Di bagian ini, kita akan mempelajari cara menambahkan paragraf dalam dokumen MS Word secara terprogram. Berikut langkah dan potongan kodenya bisa diikuti:
- Buat instance objek dari kelas Document untuk membuat file MS Word kosong.
- Buat instance kelas DocumentBuilder dan lampirkan ke objek Dokumen baru.
- Panggil metode Writeln untuk memasukkan pemisah paragraf ke dalam dokumen.
- Metode Writeln(string) menambahkan sebuah paragraf.
- Metode save akan menyimpan file MS Word.
// Instantiate an object of the Document class to create a blank MS Word file. | |
Document doc = new Document(); | |
// create an instance of DocumentBuilder class and attach it to a new Document object | |
DocumentBuilder builder = new DocumentBuilder(doc); | |
// call Writeln to insert a paragraph break into the document. | |
builder.Writeln(); | |
// Writeln(string) method adds a paragraph. | |
builder.Writeln("this is the first paragraph"); | |
builder.Writeln(); | |
builder.Writeln("this is the second paragraph"); | |
builder.Writeln(); | |
// the save method will save the MS Word file | |
doc.Save("./output.docx"); |
Cara menyisipkan tabel dalam file MS Word di C#
Ikuti langkah-langkah dan cuplikan kode untuk menambahkan tabel di dokumen Word menggunakan C#:
- Inisialisasi instance dari kelas Document untuk membuat file MS Word kosong.
- Buat instance kelas DocumentBuilder dan lampirkan ke objek Dokumen baru.
- Panggil metode StartTable untuk memasukkan tabel ke dalam file MS Word.
- Metode InsertCell akan menyisipkan sel tabel.
- Metode EndRow mengakhiri deretan tabel.
- Panggil metode EndTable untuk mengakhiri tabel.
- Gunakan metode save untuk menyimpan file MS Word.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
// Call the StartTable method to insert a table into the MS Word file. builder.StartTable(); // InsertCell will insert a table cell builder.InsertCell(); builder.Write("Row 1, Cell 1."); builder.InsertCell(); builder.Write("Row 1, Cell 2."); // EndRow method ends a row of a table builder.EndRow(); builder.InsertCell(); builder.Write("Row 2, Cell 1."); builder.InsertCell(); builder.Write("Row 2, Cell 2."); builder.EndRow(); // Invoke the EndTable method to end the table. builder.EndTable(); // call the save method to save the MS Word file doc.Save("./output.docx");
Menambahkan daftar dalam dokumen Word secara terprogram
Daftar ini merupakan bagian integral dari dokumen apa pun. Namun, Anda dapat menambahkan komponen daftar dalam dokumen Word di C# dengan mengikuti langkah-langkah dan potongan kode yang disebutkan di bawah ini:
- Buat objek dari kelas Document untuk membuat file MS Word kosong.
- Buat instance dari kelas DocumentBuilder dan lampirkan ke objek Dokumen baru.
- Panggil properti ListFormat yang mengembalikan objek yang mewakili properti pemformatan daftar saat ini dan metode ApplyBulletDefault akan menerapkan indentasi dan simbol poin ("•") sebelum setiap paragraf.
- Aktifkan metode save untuk menyimpan file MS Word.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
// Call the StartTable method to insert a table into the MS Word file. builder.StartTable(); // InsertCell will insert a table cell builder.InsertCell(); builder.Write("Row 1, Cell 1."); builder.InsertCell(); builder.Write("Row 1, Cell 2."); // EndRow method ends a row of a table builder.EndRow(); builder.InsertCell(); builder.Write("Row 2, Cell 1."); builder.InsertCell(); builder.Write("Row 2, Cell 2."); builder.EndRow(); // Invoke the EndTable method to end the table. builder.EndTable(); // call the save method to save the MS Word file doc.Save("./output.docx");
Bagaimana cara mengedit Font dokumen Word di aplikasi .NET?
Sekarang, kami akan menambahkan, dan mengedit font di dokumen MS Word menggunakan kode C# secara terprogram. Ikuti langkah-langkah berikut dan potongan kode:
- Buat objek dari kelas Document untuk membuat file MS Word kosong.
- Menginisialisasi instance dari kelas DocumentBuilder dan melampirkannya ke objek Dokumen baru.
- Kelas Font menawarkan berbagai properti untuk menata font dokumen Word.
- Properti LineWidth menetapkan lebar batas dalam titik.
- LineStyle menentukan gaya garis perbatasan.
- Panggil properti Size untuk mengatur ukuran font.
- Properti Name digunakan untuk mengatur nama font.
- Buat teks Anda tebal dengan memanggil properti Bold.
- Panggil metode save untuk menyimpan file MS Word.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
// LineWidth property ets the border width in points. builder.Font.Border.LineWidth = 0.5d; // LineStyle specifies line style of a Border. builder.Font.Border.LineStyle = LineStyle.DashDotStroker; // Call Size property to set the size of the font. builder.Font.Size = 16; // Name property is used to set the name of the font. builder.Font.Name = "Arial"; // Make your text bold by calling Bold property builder.Font.Bold = true; builder.Write("Text surrounded by border."); builder.Writeln(); // call the save method to save the MS Word file doc.Save("./output.docx");
Dapatkan Lisensi Gratis
Anda bisa mendapatkan lisensi sementara gratis untuk mencoba API tanpa batasan evaluasi.
Menyimpulkan
Output.docx berikut akan dihasilkan setelah menjalankan cuplikan kode di atas sekaligus dalam satu file C#.

Berikan pertanyaan
Anda dapat memberi tahu kami tentang pertanyaan atau pertanyaan Anda di forum kami.