NEWS

[C#] Hướng dẫn tạo file Microsoft Word paragraph, table, image, hyperlink sử dụng DocX

[C#] Hướng dẫn tạo file Microsoft Word paragraph, table, image, hyperlink sử dụng DocX
Đăng bởi: Thảo Meo - Lượt xem: 6938 19:21:35, 25/08/2021C#   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 tạo file Microsoft Word sử dụng thư viện DocX trên C#, Winform.

[C#] Create word file with paragraph, table, image, hyperlink using DocX

Giao diện demo ứng dụng:

create word file C#

Ở bài viết này mình sẽ sử dụng thư viện DocX để tạo file word.

Với thư viện DocX này, máy tính bạn chưa có cài đặt Microsoft Office Word, vẫn chạy ứng dụng được bình thường nhé.

Trong demo này, mình sẽ xuất file word gồm các loại sau:

1. Paragraph 

2. Table 

3. Image

4. Hyperlink 

5. Text

Video demo ứng dụng tạo file word:

Đầu tiên, các bạn cần cài đặt thư viện DocX từ Nuget vào project của mình.

import_docx_csharp

PM> Install-Package DocX -Version 1.8.0

Full source code C#:

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xceed.Document.NET;
using Xceed.Words.NET;
using Font = Xceed.Document.NET.Font;

namespace WorkingWithWord
{
    class Program
    {
        static void Main(string[] args)
        {
            #region one
            string fileName = "export_word_laptrinhvb_net.docx";
            var doc = DocX.Create(fileName);
            //doc.InsertParagraph("Hi Everyone");
            #endregion

            #region two
            //Formatting Title  
            string title = "LAPTRINHVB.NET";

            //Formatting Title  
            // like using this we can set font family, font size, position, font color etc

            Formatting titleFormat = new Formatting();
            //Specify font family  
            titleFormat.FontFamily = new Font("Tahoma");
            //Specify font size  
            titleFormat.Size = 20D;
            titleFormat.Position = 40;
            titleFormat.FontColor = Color.BlueViolet;
            titleFormat.UnderlineColor = Color.Gray;
            titleFormat.Italic = true;

            var img2 = doc.AddImage(@"psg.png");
            Picture p2 = img2.CreatePicture();
            Paragraph par2 = doc.InsertParagraph();
            par2.Alignment = Alignment.center;
            par2.AppendPicture(p2);

            //Text  
            string textParagraph = "Hello Laptrinhvb.net, " + Environment.NewLine +
                "Xin chào các bạn đến với website lập trình vb.net";

            //Formatting Text Paragraph  
            Formatting textParagraphFormat = new Formatting();
            //font family  
            textParagraphFormat.FontFamily = new Font("Tahoma");
            //font size  
            textParagraphFormat.Size = 12D;
            //Spaces between characters  
            textParagraphFormat.Spacing = 1;
            //Insert title  
            Paragraph paragraphTitle = doc.InsertParagraph(title, false, titleFormat);
            paragraphTitle.Alignment = Alignment.center;
            //Insert text  
            doc.InsertParagraph(textParagraph, false, textParagraphFormat);
            #endregion

            #region three
            //Create a picture  
            var img = doc.AddImage(@"ronaldo-psg.jpg");
            Picture p = img.CreatePicture();
            //Create a new paragraph  
            titleFormat.FontColor = Color.Orange;
            Paragraph par = doc.InsertParagraph("CR7", false, titleFormat);
            par.AppendPicture(p);
            #endregion

            #region four
            doc.InsertParagraph();
            //Create Table
            var listPlayer = CreateInitData();
            Table t = doc.AddTable(listPlayer.Count + 1, 3);
            t.Alignment = Alignment.center;
            t.Design = TableDesign.ColorfulList;
            // Fill cells by adding text.  
            // First row
            t.Rows[0].Cells[0].Paragraphs.First().Append("Number Shirt");
            t.Rows[0].Cells[1].Paragraphs.First().Append("Player");
            t.Rows[0].Cells[2].Paragraphs.First().Append("Position");
            
            int i = 1;
            foreach (var player in listPlayer)
            {
                t.Rows[i].Cells[0].Paragraphs.First().Append(player.shirt_number.ToString());
                t.Rows[i].Cells[1].Paragraphs.First().Append(player.football_player);
                t.Rows[i].Cells[2].Paragraphs.First().Append(player.position);
              
                i++;
            }                    
            doc.InsertTable(t);
            #endregion

            #region five
            // Hyperlink
            Hyperlink url = doc.AddHyperlink("Laptrinhvb.net", new Uri("https://laptrinhvb.net"));
            Paragraph p1 = doc.InsertParagraph();
            p1.AppendLine("Please visit: ").Bold().AppendHyperlink(url).Color(Color.Orange); // Hyperlink in blue color 
            #endregion

            var img4 = doc.AddImage(@"logo.png");
            Picture p4 = img4.CreatePicture();
            //Create a new paragraph  
            titleFormat.FontColor = Color.Orange;
            Paragraph par4 = doc.InsertParagraph();
            par4.AppendPicture(p4);

            #region part of one
            doc.Save();
            Process.Start("WINWORD.EXE", fileName);
            #endregion
            Console.Read();
        }
        public class Player
        {
            public int id { get; set; }
            public string football_player { get; set; }
            public string position { get; set; }
            public int shirt_number { get; set; }
        }
        public static List<Player> CreateInitData()
        {
            var list = new List<Player>();
            list.Add(new Player() { id = 1, football_player = "Keylor Navas", position = "Goalkeeper", shirt_number = 1 });
            list.Add(new Player() { id = 2, football_player = "Gianluigi Donnarumma", position = "Goalkeeper", shirt_number = 50 });
            list.Add(new Player() { id = 3, football_player = "Sergio Rico", position = "Goalkeeper", shirt_number = 16 });
            list.Add(new Player() { id = 4, football_player = "Achraf Hakimi", position = "Defender", shirt_number = 2 });
            list.Add(new Player() { id = 5, football_player = "Presnel Kimpembe", position = "Defender", shirt_number = 3 });
            list.Add(new Player() { id = 6, football_player = "Sergio Ramos", position = "Defender", shirt_number = 4 });
            list.Add(new Player() { id = 7, football_player = "Marquinhos", position = "Defender", shirt_number = 5 });
            list.Add(new Player() { id = 8, football_player = "Juan Bernat", position = "Defender", shirt_number = 14 });
            list.Add(new Player() { id = 9, football_player = "Colin Dagba", position = "Defender", shirt_number = 17 });
            list.Add(new Player() { id = 10, football_player = "Layvin Kurzawa", position = "Defender", shirt_number = 20 });
            list.Add(new Player() { id = 11, football_player = "Abdou Diallo", position = "Defender", shirt_number = 22 });
            list.Add(new Player() { id = 12, football_player = "Thilo Kehrer", position = "Defender", shirt_number = 24 });
            list.Add(new Player() { id = 13, football_player = "El Chadaille Bitshiabu", position = "Defender", shirt_number = 31 });
            list.Add(new Player() { id = 14, football_player = "Marco Verrati", position = "Midfielder", shirt_number = 6 });
            list.Add(new Player() { id = 15, football_player = "Leandro Paredes", position = "Midfielder", shirt_number = 8 });
            list.Add(new Player() { id = 16, football_player = "Angel Di María", position = "Midfielder", shirt_number = 11 });
            list.Add(new Player() { id = 17, football_player = "Rafinha", position = "Midfielder", shirt_number = 12 });
            list.Add(new Player() { id = 18, football_player = "Danilo Pereira", position = "Midfielder", shirt_number = 15 });
            list.Add(new Player() { id = 19, football_player = "Georginio Wijnaldum", position = "Midfielder", shirt_number = 18 });
            list.Add(new Player() { id = 20, football_player = "Pablo Sarabia", position = "Midfielder", shirt_number = 19 });
            list.Add(new Player() { id = 21, football_player = "Ander Herrera", position = "Midfielder", shirt_number = 21 });
            list.Add(new Player() { id = 22, football_player = "Julian Draxler", position = "Midfielder", shirt_number = 23 });
            list.Add(new Player() { id = 23, football_player = "Idrissa Gueye", position = "Midfielder", shirt_number = 27 });
            list.Add(new Player() { id = 24, football_player = "Xavi Simons", position = "Midfielder", shirt_number = 34 });
            list.Add(new Player() { id = 25, football_player = "Ismaël Gharbi", position = "Midfielder", shirt_number = 35 });
            list.Add(new Player() { id = 26, football_player = "Kylian Mbappé", position = "Forward", shirt_number = 7 });
            list.Add(new Player() { id = 27, football_player = "Mauro Icardi", position = "Forward", shirt_number = 9 });
            list.Add(new Player() { id = 28, football_player = "Neymar Jr", position = "Forward", shirt_number = 10 });
            list.Add(new Player() { id = 29, football_player = "Arnaud Kalimuendo", position = "Forward", shirt_number = 29 });
            list.Add(new Player() { id = 30, football_player = "Lionel Messi", position = "Forward", shirt_number = 30 });
           
            return list;
        }

    }
}

Thanks for watching!

DOWNLOAD SOURCE

THÔNG TIN TÁC GIẢ

BÀI VIẾT LIÊN QUAN

[C#] Hướng dẫn tạo file Microsoft Word paragraph, table, image, hyperlink sử dụng DocX
Đăng bởi: Thảo Meo - Lượt xem: 6938 19:21:35, 25/08/2021C#   In bài viết

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

Đọc tiếp
.