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);
相关推荐
wqqqianqian5 分钟前
国产linux系统(银河麒麟,统信uos)使用 PageOffice自定义Word模版中的数据区域
linux·word·自定义·pageoffice·数据区域
Tummer83631 小时前
C#+WPF+prism+materialdesign创建工具主界面框架
开发语言·c#·wpf
ghost1432 小时前
C#学习第23天:面向对象设计模式
开发语言·学习·设计模式·c#
yngsqq3 小时前
(for 循环) VS (LINQ) 性能比拼 ——c#
c#·solr·linq
想做后端的小C3 小时前
C# 面向对象 构造函数带参无参细节解析
开发语言·c#·面向对象
炯哈哈4 小时前
【上位机——WPF】App.xml和Application类简介
xml·开发语言·c#·wpf·上位机
bestcxx4 小时前
c# UTC 时间赋值注意事项
c#·utc
酷炫码神4 小时前
C#运算符
开发语言·c#
zybsjn5 小时前
后端系统做国际化改造,生成多语言包
java·python·c#
敲代码的 蜡笔小新5 小时前
【行为型之迭代器模式】游戏开发实战——Unity高效集合遍历与场景管理的架构精髓
unity·设计模式·c#·迭代器模式