NEWS

[C#] Hướng dẫn ghi string text file to Microsoft Office Word

[C#] Hướng dẫn ghi string text file to Microsoft Office Word
Đăng bởi: Thảo Meo - Lượt xem: 5175 20:37:56, 06/08/2021DEVEXPRESS   In bài viết

Xin chào các bạn, bài viết hôm nay mình sẽ tiếp tục hướng dẫn các bạn cách Ghi string text ra file Microsoft Office Word trên lập trình C#, Winform.

[C#] Write string text to Microsoft Office Word

export_text_to_ms_word

Các bạn thiết kế hai control: Richtextbox và button, có giao diện đơn giản như hình dưới đây.

word_csharp

Hoặc các bạn có thể xem video để thực hiện từng bước như mình ở bên dưới:

Tiếp đến các bạn import thư viện làm việc với word vào.

import_thuvien_msword

Full source code C# write text to Win Word:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Microsoft.Office.Interop.Word;

namespace WriteTexttoMSword
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            var word = new Microsoft.Office.Interop.Word.Application();
            word.Visible = true;
            word.WindowState = WdWindowState.wdWindowStateNormal;
            Microsoft.Office.Interop.Word.Document doc = word.Documents.Add();
            Microsoft.Office.Interop.Word.Paragraph paragraph = doc.Paragraphs.Add();
            paragraph.Range.Text = richTextBox1.Text;
            doc.SaveAs2("test.doc");
            Process.Start("test.doc");

        }
    }
}

Thanks for watching!

DOWNLOAD SOURCE

THÔNG TIN TÁC GIẢ

BÀI VIẾT LIÊN QUAN

[C#] Hướng dẫn ghi string text file to Microsoft Office Word
Đăng bởi: Thảo Meo - Lượt xem: 5175 20:37:56, 06/08/2021DEVEXPRESS   In bài viết

CÁC BÀI CÙNG CHỦ ĐỀ

Đọc tiếp
.