예제 #1
0
        public static void 已有的文档添加表格_添加文字_添加表格()
        {
            XWPFDocument doc = null;

            // 加载文件
            using (FileStream fs = new FileStream(Demo.docPath, FileMode.Open, FileAccess.Read))
            {
                doc = new XWPFDocument(fs);
            }
            // 添加表格
            List <Student> stus = new List <Student>()
            {
                new Student()
                {
                    StuNo = "111111", Age = 10, PhoneNum = "4321", Nationality = "Japan", Sex = 5
                },
                new Student()
                {
                    StuNo = "222222", Age = 20, PhoneNum = "1243", Nationality = "Japan", Sex = 4
                },
                new Student()
                {
                    StuNo = "333333", Age = 30, PhoneNum = "4321", Nationality = "Japan", Sex = 3
                },
                new Student()
                {
                    StuNo = "444444", Age = 10, PhoneNum = "1234", Nationality = "Japan", Sex = 2
                },
            };
            DataTable dt = ListDatatableMapper <Student> .ListToDataTable(stus);

            WordHelper.AddTable(doc, dt);

            // 添加文字
            WordHelper.AddParagrath(doc, "中间文字");

            // 添加表格
            List <Room> rooms = new List <Room>()
            {
                new Room()
                {
                    ID = 1, Name = "Room1", Floor = "1"
                },
                new Room()
                {
                    ID = 2, Name = "Room2", Floor = "3"
                },
                new Room()
                {
                    ID = 3, Name = "Room3", Floor = "2"
                },
            };
            DataTable dt2 = ListDatatableMapper <Room> .ListToDataTable(rooms);

            WordHelper.AddTable(doc, dt2);
            // 保存

            using (FileStream fs = new FileStream(docToPath, FileMode.OpenOrCreate, FileAccess.Write))
            {
                doc.Write(fs);
            }
        }