• 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;
                }
            }
        }
    }
    


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

    作者:xuejianxiyang
    出处:http://xuejianxiyang.cnblogs.com
    关于作者:Heaven helps those who help themselves.
    本文版权归原作者和博客园共有,欢迎转载,但未经原作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

  • 相关阅读:
    [算法题] 汉诺塔问题
    ubuntu导入torch模块报错
    深度问答之提取语料2
    深度问答之提取语料,导入了yml模块
    python读取文件存到excel中
    zipfile.BadZipFile: File is not a zip file
    查看linux系统时间
    tensorflow:typeerror:‘noneType’ object is not callable
    正则匹配中文标点符号
    re.sub用法
  • 原文地址:https://www.cnblogs.com/xuejianxiyang/p/4862071.html
Copyright © 2020-2023  润新知