javacv中matlab中的imwritee报cuo

@项目学习(27)
& & & &引言
& &最近项目中遇到了这么一个需求就是需要我获得上传上来的word文件中的内容,但是在开始的时候自己一点思
路都没有,在之前的项目中遇到过对word操作的需求,所以初步的想法就是给word文件打上书签,然后利用书签获得
里面的内容,这样就可以获得我们想要的内容。下面就给大家分享如何获得上传上来的word文件中的书签中的内容。
并且将内容复制到另外一个word文档中。
& &首先看看目录结构
& & & & & & & & & &
& &下面看一下代码
& &我们首先需要引入最新版的Apose.word.dll这个文件,下面的代码才可以正常运行。
& &namespace CopyBookmarkedText
/// &summary&
/// Shows how to copy bookmarked text from one document to another while preserving all content and formatting.
/// Does not cover all cases possible (bookmark can start/end in various places of a document
/// making copying scenario more complex).
/// Supported scenarios at the moment are:
/// 1. Bookmark start and end are in the same section of the document, but in different paragraphs.
/// Complete paragraphs are copied.
/// &/summary&
class Program
/// &summary&
/// The main entry point for the application.
/// &/summary&
public static void Main(string[] args)
string exeDir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + Path.DirectorySeparatorC
string dataDir = new Uri(new Uri(exeDir), @&../../Data/&).LocalP
// Load the source document.
Document srcDoc = new Document(dataDir + &Template.doc&);
// This is the bookmark whose content we want to copy.
Bookmark srcBookmark = srcDoc.Range.Bookmarks[&ntf&];
// We will be adding to this document.
Document dstDoc = new Document();
// Let's say we will be appending to the end of the body of the last section.
CompositeNode dstNode = dstDoc.LastSection.B
// It is a good idea to use this import context object because multiple nodes are being imported.
// If you import multiple times without a single context, it will result in many styles created.
NodeImporter importer = new NodeImporter(srcDoc, dstDoc, ImportFormatMode.KeepSourceFormatting);
// Do it once.
AppendBookmarkedText(importer, srcBookmark, dstNode);
// Do it one more time for fun.
AppendBookmarkedText(importer, srcBookmark, dstNode);
// Save the finished document.
dstDoc.Save(dataDir + &Template Out.doc&);
/// &summary&
/// Copies content of the bookmark and adds it to the end of the specified node.
/// The destination node can be in a different document.
/// &/summary&
/// &param name=&importer&&Maintains the import context &/param&
/// &param name=&srcBookmark&&The input bookmark&/param&
/// &param name=&dstNode&&Must be a node that can contain paragraphs (such as a Story).&/param&
private static void AppendBookmarkedText(NodeImporter importer, Bookmark srcBookmark, CompositeNode dstNode)
// This is the paragraph that contains the beginning of the bookmark.
Paragraph startPara = (Paragraph)srcBookmark.BookmarkStart.ParentN
// This is the paragraph that contains the end of the bookmark.
Paragraph endPara = (Paragraph)srcBookmark.BookmarkEnd.ParentN
if ((startPara == null) || (endPara == null))
throw new InvalidOperationException(&Parent of the bookmark start or end is not a paragraph, cannot handle this scenario yet.&);
// Limit ourselves to a reasonably simple scenario.
if (startPara.ParentNode != endPara.ParentNode)
throw new InvalidOperationException(&Start and end paragraphs have different parents, cannot handle this scenario yet.&);
// We want to copy all paragraphs from the start paragraph up to (and including) the end paragraph,
// therefore the node at which we stop is one after the end paragraph.
Node endNode = endPara.NextS
// This is the loop to go through all paragraph-level nodes in the bookmark.
for (Node curNode = startP curNode != endN curNode = curNode.NextSibling)
// This creates a copy of the current node and imports it (makes it valid) in the context
// of the destination document. Importing means adjusting styles and list identifiers correctly.
Node newNode = importer.ImportNode(curNode, true);
// Now we simply append the new node to the destination.
dstNode.AppendChild(newNode);
& &上面的程序就完成了这个功能,这个东西实属不易啊,为什么这么说呢?关于Apose.word的资料都事英文的,
对于小编这种英语盲,您说怎么办呢?没有办法啊,其实在API上面就是一个简单的功能,但是没有办法啊,在最为
关键的时候还是大神点播了一下,在根据自己查询的一些资料完成了这个功能。所在代码中的注释也是一些英文的,
算是提高一下小编的英文吧。希望能对读者提供一些帮助!!!
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:172098次
积分:11414
积分:11414
排名:第989名
原创:194篇
转载:11篇
评论:4334条
阅读:18454
阅读:4056
阅读:8502
(3)(5)(6)(3)(8)(8)(6)(7)(5)(8)(7)(14)(7)(7)(7)(8)(7)(5)(6)(7)(5)(6)(5)(6)(9)(10)(9)(4)(7)(4)(1)学而不思则罔,思而不学则殆。
1.创建word模版,使用MergeFeild绑定数据
& & 新建一个Word文档,命名为Template.doc
& & 注意:这里并不是输入"《&和&》&就可以了,而是必须在菜单的"插入&文档部件&域&找到MergeField并输入相应的域名
2.使用数组提供数据源
&string tempPath = Server.MapPath("~/Docs/Temp/Template.doc");
&string outputPath = Server.MapPath("~/Docs/Output/Template.doc");
&//载入模板
&var doc = new Document(tempPath);
&//提供数据源
&String[] fieldNames = new String[] {"UserName", "Gender", "BirthDay", "Address"};
&Object[] fieldValues = new Object[] {"张三", "男", "", "陕西咸阳"};
&//合并模版,相当于页面的渲染
&doc.MailMerge.Execute(fieldNames, fieldValues);
&//保存合并后的文档
&doc.Save(outputPath);
& //在WebForm中,保存文档到流中,使用Response.&BinaryWrite输出该文件
&&var docStream = new MemoryStream();
& doc.Save(docStream, SaveOptions.CreateSaveOptions(SaveFormat.Doc));
&&Response.ContentType = "application/msword";
& Response.AddHeader("content-disposition", " &filename=Template.doc");
& Response.BinaryWrite(docStream.ToArray());
& Response.End();
&//在MVC中采用,保存文档到流中,使用base.File输出该文件
&&var docStream = new MemoryStream();
& doc.Save(docStream, SaveOptions.CreateSaveOptions(SaveFormat.Doc));
& return base.File(docStream.ToArray(), "application/msword","Template.doc");
3.创建循环数据的模版,这里的循环数据类似页面的for结构,不拘泥于形式table
& &&TableStart:UserList&
& &姓名:&UserName&
& &&TableEnd:UserList&
4.使用DataTable提供数据源
//创建名称为UserList的DataTable
DataTable table=new DataTable("UserList");
table.Columns.Add("UserName");
table.Columns.Add("Gender");
table.Columns.Add("BirthDay");
table.Columns.Add("Address");
//----------------------------------------------------------------------------------------------------
//载入模板
&var doc = new Document(tempPath);
&//提供数据源
&var datatable= GetDataTable();
&//合并模版,相当于页面的渲染
&doc.MailMerge.ExecuteWithRegions(datatable);
&var docStream = new MemoryStream();
&doc.Save(docStream, SaveOptions.CreateSaveOptions(SaveFormat.Doc));
&return base.File(docStream.ToArray(), "application/msword","Template.doc");
&5.绑定带有子循环数据模版
6.使用DataSet提供数据源
//用户表结构
&DataTable table = new DataTable("UserList");
&table.Columns.Add(new DataColumn("Id", typeof(int)));
&table.Columns.Add("UserName");
&table.Columns.Add("Gender");
&table.Columns.Add("BirthDay");
&table.Columns.Add("Address");
//分数表结构
&DataTable table = new DataTable("ScoreList");
&table.Columns.Add(new DataColumn("UserId", typeof(int)));
&table.Columns.Add("Name");
&table.Columns.Add("Score");
//----------------------------------------------------------------------------------------------------
//载入模板
&var doc = new Document(tempPath);
&//提供数据源
&DataSet dataSet = new DataSet();
&var userTable= GetUserDataTable();
&var userScoreTable= GetUserScoreDataTable();
&dataSet.Tables.Add(userTable);
&dataSet.Tables.Add(userScoreTable);
&dataSet.Relations.Add(new DataRelation("ScoreListForUser",userTable.Columns["Id"],&userScoreTable.Columns["UserId"]));
&//合并模版,相当于页面的渲染
&doc.MailMerge.ExecuteWithRegions(dataSet);
&var docStream = new MemoryStream();
&doc.Save(docStream, SaveOptions.CreateSaveOptions(SaveFormat.Doc));
&return base.File(docStream.ToArray(), "application/msword","Template.doc");
7.模版上使用书签,插入标记位置
选中文档中的文字,在菜单的"插入&书签&指定书签的名称,排序依据选定为位置,添加一个新书签。选中的文字为书签的Text属性,这里是为了方便查看。也可以直接插入一个书签并指定位置,只是不明显。
8.在书签位置插入另一个文档的内容
//载入模板
&var doc = new Document(tempPath);
&var doc1 = new Document(tempPath1);//新文档
//找到名称为PositionFlag的书签
&var bookmark= doc.Range.Bookmarks["PositionFlag"];
//清空书签的文本
&bookmark.Text = "";
//使用DocumentBuilder对象插入一些文档对象,如插入书签,插入文本框,插入复选框,插入一个段落,插入空白页,追加或另一个word文件的内容等。
&var builder = new DocumentBuilder(doc);
//定位到指定位置进行插入操作
&builder.MoveToBookmark("PositionFlag");
//在PositionFlag书签对应的位置,插入另一个文档的内容。
//InsertDocument方法可以在找到
&InsertDocument(bookmark.BookmarkStart.ParentNode, doc1);
阅读(...) 评论()温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!&&|&&
LOFTER精选
网易考拉推荐
用微信&&“扫一扫”
将文章分享到朋友圈。
用易信&&“扫一扫”
将文章分享到朋友圈。
阅读(1863)|
用微信&&“扫一扫”
将文章分享到朋友圈。
用易信&&“扫一扫”
将文章分享到朋友圈。
历史上的今天
loftPermalink:'',
id:'fks_',
blogTitle:'aspose导出word',
blogAbstract:'protected void LinkButton1_Click(object sender, EventArgs e)&&&&&&& {&&&&&&&&&&& #region 单个标签替换值&&&&&&&&&&& //string tmppath = Server.MapPath(\"~/template.doc\");&&&&&&&&&&& //Document doc = new Document(tmppath); //载入模板&&&&&&&&&&& //if (doc.Range.Bookmarks[\"name\"] != null)&&&&&&&&&&& //{',
blogTag:'',
blogUrl:'blog/static/',
isPublished:1,
istop:false,
modifyTime:0,
publishTime:4,
permalink:'blog/static/',
commentCount:0,
mainCommentCount:0,
recommendCount:0,
bsrk:-100,
publisherId:0,
recomBlogHome:false,
currentRecomBlog:false,
attachmentsFileIds:[],
groupInfo:{},
friendstatus:'none',
followstatus:'unFollow',
pubSucc:'',
visitorProvince:'',
visitorCity:'',
visitorNewUser:false,
postAddInfo:{},
mset:'000',
remindgoodnightblog:false,
isBlackVisitor:false,
isShowYodaoAd:true,
hostIntro:'',
hmcon:'1',
selfRecomBlogCount:'0',
lofter_single:''
{list a as x}
{if x.moveFrom=='wap'}
{elseif x.moveFrom=='iphone'}
{elseif x.moveFrom=='android'}
{elseif x.moveFrom=='mobile'}
${a.selfIntro|escape}{if great260}${suplement}{/if}
{list a as x}
推荐过这篇日志的人:
{list a as x}
{if !!b&&b.length>0}
他们还推荐了:
{list b as y}
转载记录:
{list d as x}
{list a as x}
{list a as x}
{list a as x}
{list a as x}
{if x_index>4}{break}{/if}
${fn2(x.publishTime,'yyyy-MM-dd HH:mm:ss')}
{list a as x}
{if !!(blogDetail.preBlogPermalink)}
{if !!(blogDetail.nextBlogPermalink)}
{list a as x}
{if defined('newslist')&&newslist.length>0}
{list newslist as x}
{if x_index>7}{break}{/if}
{list a as x}
{var first_option =}
{list x.voteDetailList as voteToOption}
{if voteToOption==1}
{if first_option==false},{/if}&&“${b[voteToOption_index]}”&&
{if (x.role!="-1") },“我是${c[x.role]}”&&{/if}
&&&&&&&&${fn1(x.voteTime)}
{if x.userName==''}{/if}
网易公司版权所有&&
{list x.l as y}
{if defined('wl')}
{list wl as x}{/list}温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!&&|&&
本人性格开朗,善于交各种人才的朋友,本人主要从事网站开发,维护,优化等工作,如果有什么需要帮忙的可以加我为博友说哦
LOFTER精选
网易考拉推荐
用微信&&“扫一扫”
将文章分享到朋友圈。
用易信&&“扫一扫”
将文章分享到朋友圈。
& &&新建一个Word文档,命名为Template.doc& &&注意:这里并不是输入"《”和“》”就可以了,而是必须在菜单的"插入→文档部件→域”找到MergeField并输入相应的域名2.使用数组提供数据源&string tempPath = Server.MapPath("~/Docs/Temp/Template.doc");&string outputPath = Server.MapPath("~/Docs/Output/Template.doc");&//载入模板&var doc = new Document(tempPath);&//提供数据源&String[] fieldNames = new String[] {"UserName", "Gender", "BirthDay", "Address"};&Object[] fieldValues = new Object[] {"张三", "男", "", "陕西咸阳"};&//合并模版,相当于页面的渲染&doc.MailMerge.Execute(fieldNames, fieldValues);&//保存合并后的文档&doc.Save(outputPath);& //在WebForm中,保存文档到流中,使用Response.&BinaryWrite输出该文件&&var docStream = new MemoryStream();& doc.Save(docStream, SaveOptions.CreateSaveOptions(SaveFormat.Doc));&&Response.ContentType = "application/msword";& Response.AddHeader("content-disposition", " &filename=Template.doc");& Response.BinaryWrite(docStream.ToArray());& Response.End();&//在MVC中采用,保存文档到流中,使用base.File输出该文件&&var docStream = new MemoryStream();& doc.Save(docStream, SaveOptions.CreateSaveOptions(SaveFormat.Doc));& return base.File(docStream.ToArray(), "application/msword","Template.doc");3.创建循环数据的模版,这里的循环数据类似页面的for结构,不拘泥于形式table& &?TableStart:UserList?& &姓名:?UserName?& &?TableEnd:UserList?& &4.使用DataTable提供数据源//创建名称为UserList的DataTableDataTable table=new DataTable("UserList");table.Columns.Add("UserName");table.Columns.Add("Gender");table.Columns.Add("BirthDay");table.Columns.Add("Address");//----------------------------------------------------------------------------------------------------//载入模板&var doc = new Document(tempPath);&//提供数据源&var datatable= GetDataTable();&//合并模版,相当于页面的渲染&doc.MailMerge.ExecuteWithRegions(datatable);&var docStream = new MemoryStream();&doc.Save(docStream, SaveOptions.CreateSaveOptions(SaveFormat.Doc));&return base.File(docStream.ToArray(), "application/msword","Template.doc");&&5.绑定带有子循环数据模版6.使用DataSet提供数据源//用户表结构&DataTable table = new DataTable("UserList");&table.Columns.Add(new DataColumn("Id", typeof(int)));&table.Columns.Add("UserName");&table.Columns.Add("Gender");&table.Columns.Add("BirthDay");&table.Columns.Add("Address");//分数表结构&DataTable table = new DataTable("ScoreList");&table.Columns.Add(new DataColumn("UserId", typeof(int)));&table.Columns.Add("Name");&table.Columns.Add("Score");//----------------------------------------------------------------------------------------------------//载入模板&var doc = new Document(tempPath);&//提供数据源&DataSet dataSet = new DataSet();&var userTable= GetUserDataTable();&var userScoreTable= GetUserScoreDataTable();&dataSet.Tables.Add(userTable);&dataSet.Tables.Add(userScoreTable);&dataSet.Relations.Add(new DataRelation("ScoreListForUser",userTable.Columns["Id"],&userScoreTable.Columns["UserId"]));&//合并模版,相当于页面的渲染&doc.MailMerge.ExecuteWithRegions(dataSet);&var docStream = new MemoryStream();&doc.Save(docStream, SaveOptions.CreateSaveOptions(SaveFormat.Doc));&return base.File(docStream.ToArray(), "application/msword","Template.doc");7.模版上使用书签,插入标记位置选中文档中的文字,在菜单的"插入→书签”指定书签的名称,排序依据选定为位置,添加一个新书签。选中的文字为书签的Text属性,这里是为了方便查看。也可以直接插入一个书签并指定位置,只是不明显。8.在书签位置插入另一个文档的内容//载入模板&var doc = new Document(tempPath);&var doc1 = new Document(tempPath1);//新文档//找到名称为PositionFlag的书签&var bookmark= doc.Range.Bookmarks["PositionFlag"];//清空书签的文本&bookmark.Text = "";//使用DocumentBuilder对象插入一些文档对象,如插入书签,插入文本框,插入复选框,插入一个段落,插入空白页,追加或另一个word文件的内容等。&var builder = new DocumentBuilder(doc);//定位到指定位置进行插入操作&builder.MoveToBookmark("PositionFlag");//在PositionFlag书签对应的位置,插入另一个文档的内容。//InsertDocument方法可以在找到&InsertDocument(bookmark.BookmarkStart.ParentNode, doc1);
阅读(367)|
用微信&&“扫一扫”
将文章分享到朋友圈。
用易信&&“扫一扫”
将文章分享到朋友圈。
历史上的今天
在LOFTER的更多文章
loftPermalink:'',
id:'fks_',
blogTitle:'Aspose Word模板使用总结',
blogAbstract:'1.创建word模版,使用MergeFeild绑定数据& &&新建一个Word文档,命名为Template.doc& &&注意:',
blogTag:'aspose,word',
blogUrl:'blog/static/',
isPublished:1,
istop:false,
modifyTime:0,
publishTime:7,
permalink:'blog/static/',
commentCount:0,
mainCommentCount:0,
recommendCount:0,
bsrk:-100,
publisherId:0,
recomBlogHome:false,
currentRecomBlog:false,
attachmentsFileIds:[],
groupInfo:{},
friendstatus:'none',
followstatus:'unFollow',
pubSucc:'',
visitorProvince:'',
visitorCity:'',
visitorNewUser:false,
postAddInfo:{},
mset:'000',
remindgoodnightblog:false,
isBlackVisitor:false,
isShowYodaoAd:false,
hostIntro:'本人性格开朗,善于交各种人才的朋友,本人主要从事网站开发,维护,优化等工作,如果有什么需要帮忙的可以加我为博友说哦',
hmcon:'1',
selfRecomBlogCount:'0',
lofter_single:''
{list a as x}
{if x.moveFrom=='wap'}
{elseif x.moveFrom=='iphone'}
{elseif x.moveFrom=='android'}
{elseif x.moveFrom=='mobile'}
${a.selfIntro|escape}{if great260}${suplement}{/if}
{list a as x}
推荐过这篇日志的人:
{list a as x}
{if !!b&&b.length>0}
他们还推荐了:
{list b as y}
转载记录:
{list d as x}
{list a as x}
{list a as x}
{list a as x}
{list a as x}
{if x_index>4}{break}{/if}
${fn2(x.publishTime,'yyyy-MM-dd HH:mm:ss')}
{list a as x}
{if !!(blogDetail.preBlogPermalink)}
{if !!(blogDetail.nextBlogPermalink)}
{list a as x}
{if defined('newslist')&&newslist.length>0}
{list newslist as x}
{if x_index>7}{break}{/if}
{list a as x}
{var first_option =}
{list x.voteDetailList as voteToOption}
{if voteToOption==1}
{if first_option==false},{/if}&&“${b[voteToOption_index]}”&&
{if (x.role!="-1") },“我是${c[x.role]}”&&{/if}
&&&&&&&&${fn1(x.voteTime)}
{if x.userName==''}{/if}
网易公司版权所有&&
{list x.l as y}
{if defined('wl')}
{list wl as x}{/list}

我要回帖

更多关于 matlab中imwrite用法 的文章

 

随机推荐