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

在书签位置插入文字

//加载模板文档 
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);

在书签位置插入图片

//加载一个含有书签的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);

在书签位置插入图表

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);

Spire.doc读取模板文档,并在书签处插入内容_第1张图片

你可能感兴趣的:(Spire.doc,.net,.net,core,c#,Spire.doc,.net,word)