Spire.doc读取模板文档,并在书签处插入内容

在书签位置插入文字

csharp 复制代码
//加载模板文档 
Document document = new Document(Server.MapPath("~/File/评价结果.doc"));
//创建书签导航器
BookmarksNavigator bn = new BookmarksNavigator(document);
//添加一个section到文档 
Section newSec = document.AddSection();
//目标内容
var name = "张三";
//添加段落到section
newSec.AddParagraph().AppendText(name); 
//获取段落内容 
ParagraphBase firstReplacementParagraph = newSec.Paragraphs[0].Items.FirstItem as ParagraphBase;
ParagraphBase lastReplacementParagraph = newSec.Paragraphs[newSec.Paragraphs.Count - 1].Items.LastItem as ParagraphBase;
TextBodySelection selection = new TextBodySelection(firstReplacementParagraph, lastReplacementParagraph);
TextBodyPart part = new TextBodyPart(selection);
//移动到书签 "name", 删除它的内容并保留格式  
bn.MoveToBookmark("name", true, true);
bn.DeleteBookmarkContent(true);
//用新添加段落的内容替换掉原书签的内容并保留格式 
bn.ReplaceBookmarkContent(part, true, true);

在书签位置插入图片

csharp 复制代码
//加载一个含有书签的Word文档
Document document = new Document(Server.MapPath("~/File/评价结果.doc"));
//创建BookmarksNavigator实例
BookmarksNavigator bn = new BookmarksNavigator(document);
//找到名为Spire的书签
bn.MoveToBookmark("图片", true, true);
//添加一个secton并命名为section0
Section section0 = document.AddSection();
//为section0添加一个段落
Paragraph paragraph = section0.AddParagraph();
//加载一张图片
Image image = Image.FromFile("1.png");
//为段落添加图片
DocPicture picture = paragraph.AppendPicture(image);
//把含有图片的段落插入到书签位置
bn.InsertParagraph(paragraph);
document.Sections.Remove(section0);

在书签位置插入图表

csharp 复制代码
Document document = new Document(Server.MapPath("~/File/Model/地下水动态监测系统简报-周报.doc"));
BookmarksNavigator bn = new BookmarksNavigator(document);
//添加一个节
var section0 = document.AddSection();
//在该节中添加一个段
var newPara = section0.AddParagraph();
//将指定大小的柱状图添加到段落中
var shape = newPara.AppendChart(ChartType.Column, 400, 252);
//创建图表对象
Chart chart = shape.Chart;
//设置图表标题
chart.Title.Text = "柱状图";
ChartSeriesCollection seriesColl = chart.Series;
//清除图表的默认系列数据
seriesColl.Clear();
string[] categories1 = new string[]{"甲","乙","丙","丁"};
double[] dataArr1 = new double[]{1.1,2.2,3.3,4.4};
string[] categories2 = new string[]{"甲","乙","丙","丁"};
double[] dataArr2 = new double[]{5.5,6.6,7.7,8.8};
//添加一个具有指定系列名称、类别名称和系列值的自定义系列到图表中
seriesColl.Add("李四", categories1, dataArr1);
seriesColl.Add("张三", categories2, dataArr2);
bn.MoveToBookmark("SWZZT",true,true);
bn.InsertParagraph(newPara);
document.Sections.Remove(section0);
相关推荐
懒人咖5 小时前
缺料分析时携带用料清单的二开字段
c#·金蝶云星空
bugcome_com6 小时前
深入了解 C# 编程环境及其开发工具
c#
wfserial8 小时前
c#使用微软自带speech选择男声仍然是女声的一种原因
microsoft·c#·speech
阔皮大师9 小时前
INote轻量文本编辑器
java·javascript·python·c#
kylezhao201910 小时前
C# 中的 SOLID 五大设计原则
开发语言·c#
啦啦啦_999911 小时前
Redis-5-doFormatAsync()方法
数据库·redis·c#
Porco.w11 小时前
C#与三菱PLC FX5U通信
网络·c#
E_ICEBLUE13 小时前
PPT 批量转图片:在 Web 预览中实现翻页效果(C#/VB.NET)
c#·powerpoint·svg
JQLvopkk15 小时前
C# 轻量级工业温湿度监控系统(含数据库与源码)
开发语言·数据库·c#
wxin_VXbishe17 小时前
C#(asp.net)学员竞赛信息管理系统-计算机毕业设计源码28790
java·vue.js·spring boot·spring·django·c#·php