c#调用Aspose.Word组件操作word 插入文字/图片/表格 书签替换套打

由于NPOI暂时没找到书签内容替换功能,所以换用Apose.Word组件.

 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Aspose.Words;
using Aspose.Words.Drawing;

namespace WordNPOI
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        private void Form2_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                Utils.ExcelDataTableConverter edc = new Utils.ExcelDataTableConverter("60岁以上人员.xlsx");
                DataTable dt = edc.ExcelToDataTable("sheet1", true);

                int rowCount = dt.Rows.Count;
                int columnCount = dt.Columns.Count;

                Aspose.Words.Document doc = new Aspose.Words.Document("TEMPLATE.DOCX");
                Aspose.Words.DocumentBuilder builder = new Aspose.Words.DocumentBuilder(doc);

                builder.MoveToBookmark("BK001");
                builder.StartTable();//开始画Table            
                builder.ParagraphFormat.Alignment = ParagraphAlignment.Center; // RowAlignment.Center;                

                string str = string.Empty;

                builder.RowFormat.Height = 20;

                //添加列头
                for (int i = 0; i < columnCount; i++)
                {
                    builder.InsertCell();
                    //Table单元格边框线样式
                    builder.CellFormat.Borders.LineStyle = LineStyle.Single;
                    //Table此单元格宽度
                    builder.CellFormat.Width = 600;
                    //此单元格中内容垂直对齐方式
                    builder.CellFormat.VerticalAlignment = Aspose.Words.Tables.CellVerticalAlignment.Center;
                    builder.CellFormat.HorizontalMerge = Aspose.Words.Tables.CellMerge.None;
                    builder.CellFormat.VerticalMerge = Aspose.Words.Tables.CellMerge.None;
                    //字体大小
                    builder.Font.Size = 10;
                    //是否加粗
                    builder.Bold = true;
                    //向此单元格中添加内容
                    builder.Write(dt.Columns[i].ColumnName);
                }
                builder.EndRow();

                //添加每行数据
                for (int i = 0; i < rowCount; i++)
                {


                    for (int j = 0; j < columnCount; j++)
                    {
                        str = dt.Rows[i][j].ToString();

                        //http://www.cnblogs.com/geovindu/p/4106418.html
                        //http://www.cnblogs.com/wuhuacong/archive/2012/08/30/2662961.html

                        //插入Table单元格
                        builder.InsertCell();

                        //Table单元格边框线样式
                        builder.CellFormat.Borders.LineStyle = LineStyle.Single;

                        //Table此单元格宽度 跟随列头宽度
                        //builder.CellFormat.Width = 500;

                        //此单元格中内容垂直对齐方式
                        builder.CellFormat.VerticalAlignment = Aspose.Words.Tables.CellVerticalAlignment.Center;

                        builder.CellFormat.HorizontalMerge = Aspose.Words.Tables.CellMerge.None;
                        builder.CellFormat.VerticalMerge = Aspose.Words.Tables.CellMerge.None;

                        //字体大小
                        builder.Font.Size = 10;

                        //是否加粗
                        builder.Bold = false;

                        //向此单元格中添加内容
                        builder.Write(str);

                    }

                    //Table行结束
                    builder.EndRow();

                }
                builder.EndTable();


                //doc.Range.Bookmarks["BK001"].Text = "";    // 清掉标示  

                doc.Range.Bookmarks["BK002"].Text = "标题"; <span style="color:#ff6666;">//替换书签内容</span>



                //Shape shape = new Shape(doc, ShapeType.Image);
                //shape.ImageData.SetImage("1.png");
                //shape.Width = 600;
                //shape.Height = 400;
                //shape.HorizontalAlignment = Aspose.Words.Drawing.HorizontalAlignment.Center;
                if (doc.Range.Bookmarks["BK003"] != null)
                {
                    //builder.InsertNode(shape); //这种图片会把后面的内容盖掉

                    builder.MoveToBookmark("BK003");
                    var img = builder.<span style="color:#ff6666;">InsertImage</span>("1.png");
                    img.Width = 300;
                    img.Height = 300;
                    img.HorizontalAlignment = Aspose.Words.Drawing.HorizontalAlignment.Center;

                }


                string saveDocFile = "1.DOCX";
                doc.Save(saveDocFile);
                if (MessageBox.Show("保存成功,是否打开文件?", "", MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.Yes)
                {
                    System.Diagnostics.Process.Start(saveDocFile);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return;
            }
        }
    }
}


 

版权声明:本文为博主原创文章,未经博主允许不得转载。

 

转载于:https://www.cnblogs.com/xuejianxiyang/p/4862071.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值