需要帮助来处理表格或列表

Jiale Xue - MSFT 46,296 信誉分 Microsoft 供应商
2024-02-28T06:46:52.5766667+00:00

你好,

处理Word段落时,在创建的文档中创建列表或表格的代码是什么?

Note:此问题总结整理于:Need help to handle a table or a list

.NET
.NET
基于 .NET 软件框架的 Microsoft 技术。
62 个问题
C#
C#
一种面向对象的类型安全的编程语言,它起源于 C 语言系列,包括对面向组件的编程的支持。
187 个问题
0 个注释 无注释
{count} 票

接受的答案
  1. Hui Liu-MSFT 48,571 信誉分 Microsoft 供应商
    2024-02-28T07:40:37.1166667+00:00

    为了在创建的文档中创建一个表,您可以参考以下示例。
    Student.cs

    namespace myWindowsFormsApp  
    {  
        class Student  
        {  
            public string Name;  
            public int Score;  
            public string StuClass;  
            public string Leader;  
        }  
    }
    

    Form1.cs

    namespace myWindowsFormsApp  
    {  
        public partial class Form1 : Form  
        {  
            public Form1()  
            {  
                InitializeComponent();  
            }  
      
            private void button1_Click(object sender, EventArgs e)  
            {  
                CreateTableToDoc();  
            }  
            protected void CreateTableToDoc()  
            {  
                Microsoft.Office.Interop.Word.Application app = new Microsoft.Office.Interop.Word.Application();  
                object filename = @"C:\Users\xingyuzh\Desktop\test.docx";  
                object isread = false;  
                object isvisible = true;  
                object miss = System.Reflection.Missing.Value;  
                app.Documents.Open(ref filename, ref miss, ref isread, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref isvisible, ref miss, ref miss, ref miss, ref miss);  
                try  
                {  
                    List<Student> datas = new List<Student>();  
                    datas.Add(new Student { Leader = "aa", Name = "aaa", Score = 111, StuClass = "aaaa" });  
                    datas.Add(new Student { Leader = "bb", Name = "bbb", Score = 222, StuClass = "bbbb" });  
                    datas.Add(new Student { Leader = "cc", Name = "ccc1", Score = 333, StuClass = "cccc" });  
                    datas.Add(new Student { Leader = "cc", Name = "ccc2", Score = 555, StuClass = "cccc" });  
                    datas.Add(new Student { Leader = "dd", Name = "ddd", Score = 666, StuClass = "dddd" });  
                    var cate = datas.GroupBy(s => s.StuClass);  
                    int rows = cate.Count() + 2;  //row for title  
                    int cols = 5;  
                    object oMissing = System.Reflection.Missing.Value;  
      
                    //The title format  
                    app.Selection.Font.Bold = 700;  
                    app.Selection.Font.Size = 16;  
                    app.Selection.Range.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter;  
                    app.Selection.Text = "Title";  
      
                    //Wrap to add a table  
                    object line = Microsoft.Office.Interop.Word.WdUnits.wdLine;  
                    app.Selection.MoveDown(ref line, oMissing, oMissing);  
                    app.Selection.TypeParagraph();//Wrap  
                    Microsoft.Office.Interop.Word.Range range = app.Selection.Range;  
                    Microsoft.Office.Interop.Word.Table table = app.Selection.Tables.Add(range, rows, cols, ref oMissing, ref oMissing);  
      
                    //Set the font size of the table to be thick  
                    table.Range.Font.Size = 10;  
                    table.Range.Font.Bold = 0;  
      
                    //Set the outer and inner borders of the table  
                    table.Borders.OutsideLineStyle = Microsoft.Office.Interop.Word.WdLineStyle.wdLineStyleSingle;  
                    table.Borders.InsideLineStyle = Microsoft.Office.Interop.Word.WdLineStyle.wdLineStyleOutset;  
      
                    //Set the Column titles  
                    int rowIndex = 1;  
                    table.Cell(rowIndex, 1).Range.Text = "class";  
                    table.Cell(rowIndex, 2).Range.Text = "name";  
                    table.Cell(rowIndex, 3).Range.Text = "score";  
                    table.Cell(rowIndex, 4).Range.Text = "stuNumber";  
                    table.Cell(rowIndex, 5).Range.Text = "leader";  
      
                    //Loop data to create rows of data  
                    rowIndex++;  
                    foreach (var i in cate)  
                    {  
                        int moveCount = i.Count() - 1;//The number of rows merged vertically  
                        if (moveCount.ToString() != "0")  
                        {  
                            table.Cell(rowIndex, 1).Merge(table.Cell(rowIndex + moveCount, 1));//Merge classes  
                            table.Cell(rowIndex, 4).Merge(table.Cell(rowIndex + moveCount, 4));//Merge number  
                            table.Cell(rowIndex, 5).Merge(table.Cell(rowIndex + moveCount, 5));//Merge leader  
                        }  
      
                        //Write the merged data and center vertically  
                        table.Cell(rowIndex, 1).Range.Text = i.Key;  
                        table.Cell(rowIndex, 4).Range.Text = i.Count().ToString();  
                        table.Cell(rowIndex, 5).Range.Text = i.First().Leader;  
                        table.Cell(rowIndex, 1).VerticalAlignment = Microsoft.Office.Interop.Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter;  
                        table.Cell(rowIndex, 4).VerticalAlignment = Microsoft.Office.Interop.Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter;  
                        table.Cell(rowIndex, 5).VerticalAlignment = Microsoft.Office.Interop.Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter;  
      
                        //Build name, score data  
                        foreach (var x in i)  
                        {  
                            table.Cell(rowIndex, 2).Range.Text = x.Name;  
                            table.Cell(rowIndex, 3).Range.Text = x.Score.ToString();  
                            rowIndex++;  
                        }  
                    }  
                    app.Documents.Save();  
                }  
                catch (Exception e)  
                {  
                    Console.WriteLine(e.Message);  
                }  
                finally  
                {  
                    if (app.Documents != null)  
                    {  
                        app.Documents.Close();  
                    }  
                    if (app != null)  
                    {  
                        app.Quit();  
                    }  
                }  
            }  
        }  
    }
    
    

    我的测试结果User's image

    如果答案有帮助,请点击“接受答案”并点赞。
    注意:如果您想接收此线程的相关电子邮件通知,请按照我们文档中的步骤启用电子邮件通知。

    0 个注释 无注释

0 个其他答案

排序依据: 非常有帮助

你的答案

问题作者可以将答案标记为“接受的答案”,这有助于用户了解已解决作者问题的答案。