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);
相关推荐
wearegogog12310 小时前
C# .NET 文件比较工具 WinForms
开发语言·c#·.net
糖不吃11 小时前
WPF值转换器
c#
学以智用11 小时前
.NET Core Swagger 超详细讲解(从入门到企业级)
后端·.net
DS随心转插件12 小时前
AI导出鸭:DeepSeek 转 Word 效果实测与案例展示
人工智能·ai·word·豆包·deepseek·ai导出鸭
Popeye-lxw13 小时前
由罗技 K380 键盘 FN 键模式切换引发的血案
c#
FL162386312913 小时前
C# OpenCvSharp 基于霍夫变换直线检测的文本图像倾斜校正文本图像倾斜校
开发语言·c#
aini_lovee14 小时前
C# 快递单打印系统(万能套打系统)
开发语言·c#
白菜上路14 小时前
C# Serilog.AspNetCore基本使用
c#·serilog
小白不白11115 小时前
C# WinForm 与 VP 二次开发
开发语言·c#
SunnyDays101116 小时前
如何使用 C# 自动调整 Excel 行高和列宽
开发语言·c#·excel