[VB.NET] Hướng dẫn fill dữ liệu từ winform vào Microsoft word

[VB.NET] Hướng dẫn fill dữ liệu từ winform vào Microsoft word
Đăng bởi: Thảo Meo - Lượt xem: 118 10:43:19, 15/02/2025VB.NET   In bài viết

Xin chào các bạn, bài viết hôm nay mình tiếp tục hướng dẫn các bạn cách fill dữ liệu từ winform vào file tài liệu microsoft word.

[VB.NET] How to fill data from winform to Microsoft Word Template File.

Dưới đây là video demo ứng dụng:



Đầu tiên, các bạn cần cài đặt thư viện DocX.

Thư viện này có cung cấp 2 phiên bản: cá nhân và thương mại.

Nếu dùng cá nhân thì vẫn dùng bình thường nhé bạn.

Source code vb.net:

Imports Xceed.Words.NET

Public Class Form1
    Private templatePath As String = "Template.docx"
    Private outputPath As String = "Output.docx"
    Private newImagePath As String = ""

    <Obsolete>
    Private Sub btnFillToWord_Click(sender As Object, e As EventArgs) Handles btnFillToWord.Click
        Try
            Dim document As DocX = DocX.Load(templatePath)

            document.ReplaceText("<sohopdong>", txtSoHopDong.Text.Trim())

            document.ReplaceText("<tenkhachhang>", txtTenKhachHang.Text.Trim())
            document.ReplaceText("<dienthoai>", txtDienThoai.Text.Trim())

            document.ReplaceText("<diachi>", txtDiaChi.Text.Trim())

            If newImagePath <> "" Then

                Dim placeholderParagraph = document.Paragraphs.FirstOrDefault(Function(p) p.Text.Contains("<hinhkhachhang>"))

                If placeholderParagraph IsNot Nothing Then

                    Dim img = document.AddImage(newImagePath)
                    Dim picture = img.CreatePicture()
                    picture.Width = 80
                    picture.Height = 100

                    placeholderParagraph.InsertPicture(picture)
                    document.ReplaceText("<hinhkhachhang>", "")

                Else
                    MessageBox.Show("The picture placeholder was not found in the document.", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning)
                End If
            End If


            document.SaveAs(outputPath)

            MessageBox.Show("File Word đã được tạo thành công tại:" & vbCrLf & outputPath, "Thành công", MessageBoxButtons.OK, MessageBoxIcon.Information)
            Process.Start(outputPath)
        Catch ex As Exception
            MessageBox.Show("Có lỗi xảy ra: " & ex.Message, "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub

    Private Sub PictureBox1_Click(sender As Object, e As EventArgs) Handles PictureBox1.Click
        Dim dlg = New OpenFileDialog()
        If dlg.ShowDialog() = DialogResult.OK Then
            newImagePath = dlg.FileName
            PictureBox1.LoadAsync(newImagePath)
        End If

    End Sub

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load


    End Sub
End Class

Thanks for watching!

DOWNLOAD SOURCE

THÔNG TIN TÁC GIẢ

BÀI VIẾT LIÊN QUAN

[VB.NET] Hướng dẫn fill dữ liệu từ winform vào Microsoft word
Đăng bởi: Thảo Meo - Lượt xem: 118 10:43:19, 15/02/2025VB.NET   In bài viết

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

Đọc tiếp