word_demo

image

点击查看代码
using Aspose.Words;
using Aspose.Words.Tables;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace word_demo
{
    public class Test_wordCreate
    {
        // Aspose.Words.DocumentBuilder
        private readonly DocumentBuilder builder;

        private readonly Document doc;

        private readonly string downloadFile;
        public static Test_wordCreate Build
        {
            get { return new Test_wordCreate(); }
        }

        public Test_wordCreate()
        {
            string tmplFile =
              @"D:\WorkCode\word_demo\word_demo\word\fileword.docx";//word模板
            var now = DateTime.Now;
            var fileName = "TableCreated.docx";

            downloadFile = @"D:\WorkCode\word_demo\word_demo\word\desk\" + fileName; //提供给用户下载的文件路径
            if (!File.Exists(tmplFile))
            {

            }
            if (!Directory.Exists(Path.GetDirectoryName(downloadFile)))
            {
                Directory.CreateDirectory(Path.GetDirectoryName(downloadFile));
            }
            File.Copy(tmplFile, downloadFile, true);//将模板内容复制给用户下载的word中


            doc = new Document(downloadFile);
            builder = new DocumentBuilder(doc);
        }

        /// <summary>
        /// 竖直方向合并的第一个单元格
        /// </summary>
        public void CellCreate()
        {
            builder.InsertCell(); // 首先插入一个单元格
                                  // 设置单元格边框
            builder.CellFormat.Borders.LineStyle = LineStyle.Single;
            builder.CellFormat.Borders.Color = Color.Black;

            builder.CellFormat.VerticalMerge = Aspose.Words.Tables.CellMerge.Previous; // 竖直方向合并的第一个单元格
            builder.CellFormat.HorizontalMerge = Aspose.Words.Tables.CellMerge.None;

            // 设置单元格内容对齐方式
            builder.CellFormat.VerticalAlignment = Aspose.Words.Tables.CellVerticalAlignment.Center;
            builder.ParagraphFormat.Alignment = ParagraphAlignment.Center;

            builder.Write("RowSpan 2 , Cell 0 Content"); // 写入内容
        }


        public void TCell()
        {
            builder.InsertCell();
            builder.CellFormat.Borders.LineStyle = LineStyle.Single;
            builder.CellFormat.Borders.Color = Color.Black;
            builder.CellFormat.VerticalMerge = Aspose.Words.Tables.CellMerge.None;
            builder.CellFormat.HorizontalMerge = Aspose.Words.Tables.CellMerge.None;
            builder.CellFormat.VerticalAlignment = Aspose.Words.Tables.CellVerticalAlignment.Center;
            builder.ParagraphFormat.Alignment = ParagraphAlignment.Center;
            builder.Write("表头1");

        }

        public void T2Cell()
        {
            /**第二个单元格 start **/
            builder.InsertCell(); // 插入第二个单元格
            builder.CellFormat.Borders.LineStyle = LineStyle.Single;
            builder.CellFormat.Borders.Color = Color.Black;

            builder.CellFormat.VerticalMerge = Aspose.Words.Tables.CellMerge.None;
            builder.CellFormat.HorizontalMerge = Aspose.Words.Tables.CellMerge.First; // 水平方向的合并是第一个单元格
            builder.CellFormat.VerticalAlignment = Aspose.Words.Tables.CellVerticalAlignment.Center;
            builder.ParagraphFormat.Alignment = ParagraphAlignment.Center;
            builder.Write("战地号码 \n 站位");  // 写入内容

        }

        public void noCell()
        {
            builder.InsertCell();
            builder.CellFormat.Borders.LineStyle = LineStyle.Single;
            builder.CellFormat.Borders.Color = Color.Black;

            builder.CellFormat.VerticalMerge = Aspose.Words.Tables.CellMerge.None;
            builder.CellFormat.HorizontalMerge = Aspose.Words.Tables.CellMerge.Previous; // 水平方向的合并是合并向前一个单元格
        }
        public void unall2Cell()
        {
            builder.InsertCell();
            builder.CellFormat.Borders.LineStyle = LineStyle.Single;
            builder.CellFormat.Borders.Color = Color.Black;
            builder.CellFormat.VerticalMerge = Aspose.Words.Tables.CellMerge.None;
            builder.CellFormat.HorizontalMerge = Aspose.Words.Tables.CellMerge.None;
            builder.Write("更次");
        }
        public void CreateTableDemo()
        {



            builder.MoveToBookmark("TABLEMARK"); // 移动到书签处

            builder.StartTable(); // 开始创建表格

            TCell();
            T2Cell();
            /**第二个单元格 start **/

            builder.EndTable();//表格结束

            doc.Save(downloadFile);//保存word
        }


        public void CreateTableDemo2()
        {
            builder.MoveToBookmark("TABLEMARK"); // 移动到书签处

            builder.StartTable(); // 开始创建表格

            TCell();
            T2Cell();
      
            for (int i = 0; i <= 3; i++)
            {
                noCell();
            }
            builder.EndRow(); // 这一行结束了
            TCell();
            unall2Cell();
            unall2Cell();
            unall2Cell();

            /**第四个单元格 end **/
            builder.EndRow(); // 这一行结束了


            builder.EndTable();//表格结束

            doc.Save(downloadFile);//保存word
        }
    }


}

posted @ 2023-09-06 22:12  Bo-Hong  阅读(12)  评论(0编辑  收藏  举报