当前位置 博文首页 > 渊渟无迹静涛君的博客:C# Aspose.Word 操作word文档【五】

    渊渟无迹静涛君的博客:C# Aspose.Word 操作word文档【五】

    作者:[db:作者] 时间:2021-08-05 19:06

    今天给大家介绍下,Aspose.word 对word进行创建一行文字(段落)的时候注意事项

    ? ? ? ? ? ?string tempFile = Application.StartupPath + "\\resource\\templete\\项目建议书模板.doc"; ? ?//这里模板路径 比如:D:\\
    ? ? ? ? ? ? Document doc = new Document(tempFile);

    ? ? ? ? ? ? Paragraph p = new Paragraph(doc);? ? ? ? ? ?
    ? ? ? ? ? ? p.AppendChild(new Run(doc,"这里是创建的文字"));
    ? ? ? ? ? ? p.ParagraphFormat.Style.Font.Size = 8;
    ? ? ? ? ? ? p.ParagraphFormat.Style.Font.Name = "宋体";

    上面代码,在创建段落时,一点也没有问题,但是如果你word中模板比较多,也就是word中的table比较多的时候,别用这种方法去改字体大小,或者样式,因为,这种方法会导致其他模板字体大小或者样式受影响!!!

    ? ? ? ? ? ? 如下代码:

    ? ? ? ? ? ? Paragraph p = new Paragraph(doc);
    ? ? ? ? ? ? Run r = new Run(doc, "生产技术改造项目主要拟拆除设备评估鉴定表");
    ? ? ? ? ? ? r.Font.Name = "等线";
    ? ? ? ? ? ? r.Font.Size = 11;
    ? ? ? ? ? ? r.Font.Bold = true; ? ?//加粗 ? ? ? ? ? ? ? ? ? ? ?
    ? ? ? ? ? ? p.ParagraphFormat.Alignment = ParagraphAlignment.Center; //水平居中
    ? ? ? ? ? ? p.AppendChild(r);

    这个代码虽然和上面都是设置字体大小和样式,但是第二种不会影响其他模板数据!!!


    二、介绍下如何复制Word中的模板table,并粘贴到下一页(分页)

    如图:这是一个模板


    本来我的想法就是复制指定的table,然后插入一页,再将这一页粘贴这个table,但是很遗憾的是aspos.word没有提供获取指定页的方法。然后我就换了方法如下:

    ? ? ? ? ? ? ? ? ? ? string tempFile = Application.StartupPath + "\\resource\\templete\\项目建议书模板.doc"; ? //你们可以在D:\\
    ? ? ? ? ? ? ? ? ? ? Document doc = new Document(tempFile);
    ? ? ? ? ? ? ? ? ? ? DocumentBuilder builder = new DocumentBuilder(doc);
    ? ? ? ? ? ? ? ? ? ? NodeCollection allTables = doc.GetChildNodes(NodeType.Table, true); //获取所有表格table

    ? ? ? ? ? ? ? ? ? ? Aspose.Words.Tables.Table table4 = allTables[0] as Aspose.Words.Tables.Table;//拿到第1个表格 ? 这里你模板在第几页就是第几个表格

    ? ? ? ? ? ? ? ? ? ? builder.MoveToDocumentEnd();
    ? ? ? ? ? ? ? ? ? ? builder.InsertBreak(BreakType.PageBreak); ? ? ? ? ? ? ? ? ?
    ? ? ? ? ? ? ? ? ? ? Paragraph p = new Paragraph(doc);
    ? ? ? ? ? ? ? ? ? ? Run r = new Run(doc, "生产技术改造项目主要拟拆除设备评估鉴定表");
    ? ? ? ? ? ? ? ? ? ? r.Font.Name = "等线";
    ? ? ? ? ? ? ? ? ? ? r.Font.Size = 11;
    ? ? ? ? ? ? ? ? ? ? r.Font.Bold = true; ? ? ? ? ? ? ? ? ? ? ? ? ?
    ? ? ? ? ? ? ? ? ? ? p.ParagraphFormat.Alignment = ParagraphAlignment.Center; //水平居中
    ? ? ? ? ? ? ? ? ? ? p.AppendChild(r);
    ? ? ? ? ? ? ? ? ? ? table4.ParentNode.AppendChild(p); ?//追加新的一行
    ? ? ? ? ? ? ? ? ? ? table4.ParentNode.AppendChild(new Paragraph(doc)); //追加新的空白行
    ? ? ? ? ? ? ? ? ? ? table4.ParentNode.AppendChild(tableClone); //追加复制的表格

    ? ? ? ? ? ? ? ? ? ? //追加一个空行
    ? ? ? ? ? ? ? ? ? ? table4.ParentNode.AppendChild(new Paragraph(doc));
    ? ? ? ? ? ? ? ? ? ? table4.ParentNode.AppendChild(new Paragraph(doc));
    ? ? ? ? ? ? ? ? ? ? table4.ParentNode.AppendChild(new Paragraph(doc));
    ? ? ? ? ? ? ? ? ? ? table4.ParentNode.AppendChild(new Paragraph(doc));
    ? ? ? ? ? ? ? ? ? ? table4.ParentNode.AppendChild(new Paragraph(doc));
    ? ? ? ? ? ? ? ? ? ? table4.ParentNode.AppendChild(new Paragraph(doc));
    ? ? ? ? ? ? ? ? ? ? table4.ParentNode.AppendChild(new Paragraph(doc));
    ? ? ? ? ? ? ? ? ? ? table4.ParentNode.AppendChild(new Paragraph(doc));
    ? ? ? ? ? ? ? ? ? ? table4.ParentNode.AppendChild(new Paragraph(doc));
    ? ? ? ? ? ? ? ? ? ? table4.ParentNode.AppendChild(new Paragraph(doc));
    ? ? ? ? ? ? ? ? ? ? table4.ParentNode.AppendChild(new Paragraph(doc));

    ? ? ? ? ? ? ? ? ? doc.Save(docPath); ?//docPath 保存路径,随便你写

    cs