快捷搜索:  as  test  1111  test aNd 8=8  test++aNd+8=8  as++aNd+8=8  as aNd 8=8

亚洲必赢App官方下:OpenXML 替换word模板中的标签



本文示例源代码或素材下载

近来的项目(MOSS项目)必要,用户必要根据word模板天生相关的word文档,详细需求是根据infopath表单中的内容和相关的模板天生一份word文档

动手做之前想想如果用word api操作的话,后台进程,多用户并发操作等等问题

看来照样用openxml来操作对照好,于是找了找资料,实现了这小小的功能

现在把代码贴出来跟大年夜家分享下

应用openxml技巧时,首先必要引用windowsbase.dll

部分常量

conststringdocumentRelationshipType="http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument";

conststringstrUri="http://schemas.openxmlformats.org/wordprocessingml/2006/main";

staticListbookmarkText;

stringbookmarkName;

该措施是将模板中所有标签都掏出,并放到一个datatable中

voidbindTable(stringfileName)

{

dt=newDataTable();

dt.Columns.Add("标签",typeof(string));

dt.Columns.Add("内容",typeof(string));

DataRowdr;

Streamstrm=null;

PackagePartdocumentPart;

Packagepackage;

using(SPSitesite=newSPSite("http://ascentn-moss:8686/"))

{

SPWebweb=site.OpenWeb();

strm=web.GetFile(fileName).OpenBinaryStream();

docum亚洲必赢App官方下entPart=null;

package=Package.Open(strm,FileMode.Open,FileAccess.ReadWrite);

//FileStreamfs=newFileStream(fileName,FileMode.OpenOrCreate);

//PackagePartdocumentPart=null;

//Packagepackage=Package.Open(fileName,FileMode.Open,Fi亚洲必赢App官方下leAccess.ReadWrite);

//MessageBox.Show(strm.Length.ToString());

//Packagepackage=Package.Open(strm,FileMode.Open,FileAccess.ReadWrite);

foreach(System.IO.Packaging.PackageRelationshipdocumentRelationshipinpackage.GetRelationshipsByType(documentRelationshipType))

{

NameTablent=newNameTable();

XmlNamespaceManagernsManager=newXmlNamespaceManager(nt);

nsManager.AddNamespace("w",strUri);

UridocumentUri=PackUriHelper.ResolvePartUri(newUri("/",UriKind.Relative),documentRelationship.TargetUri);

documentPart=package.GetPart(documentUri);

XmlDocumentxdoc=newXmlDocument();

xdoc.Load(documentPart.GetStream());

XmlNodeListnodeList=xdoc.SelectNodes("//w:bookmarkStart",nsManager);

foreach(XmlNodenodeinnodeList)

{

dr=dt.NewRow();

if(node.NextSibling.Name.ToString()=="w:bookmarkEnd")

{

bookmarkText=newList();

bookmarkName=node.Attributes["w:name"].Value;

bookmarkText.Add("");

dr[0]=bookmarkName;

//dataGridView

}

else

{

bookmarkName=node.Attributes["w:name"].Value;

dr[0]=bookmarkName;

stringbookmarkId=node.Attributes["w:id"].Value;

bookmarkText=newList();

XmlNodenextParentNode=node.ParentNode;

XmlNodenodeIterate=node.NextSibling;

while(nodeIterate.Name.ToString()!="w:bookmarkEnd")

{

if(nodeIterate.Name.ToString()=="w:r")

bookmarkText.Add(nodeIterate.InnerText);

nodeIterate=nodeIterate.NextSibling;

if(nodeIterate==null)

{

if(nextParentNode.NextSibling.Attributes.Count!=0)

{

if((nextParentNode.NextSibling.Attributes["w:id"].Value==bookmarkId))

{

nodeIterate=nextParentNode.NextSibling;

}

}

else

{

nextParentNode=nextParentNode.NextSibling;

nodeIterate=nextParentNode.FirstChild;

boo亚洲必赢App官方下kmarkText.Add('n'+nodeIterate.InnerText);

}

}

}

//dic.Add(bookmarkName,bookmarkText);

}

dt.Rows.Add(dr);

}

}

package.Close();

}

}

接下来必要做的是用infopath中的相枢纽关头点去逐个调换标签内容

#region 处置惩罚标签

stringspUrl=(string)ds["SPServer"];

APWS.Serviceapws=newAPWS.Service();

apws.Url=spUrl+"/spsws/Service.asmx";

apws.Credentials=WFSystem.GetSystemUserCredential();

System.IO.MemoryStreamfileStream=newSystem.IO.MemoryStream();

stringnewfilename=string.Empty;

if(fileLocation.EndsWith("/"))

newfilename=fileLocation+fileName+template.Substring(template.LastIndexOf('.'));

else

newfilename=fileLocation+"/"+fileName+template.Substring(template.LastIndexOf('.'));

apws.SPCopyFile(template,newfilename);

fileStream.Write(apws.SPOpenBinary(newfilename),0,apws.SPGetFileLenght(newfilename));

originalDocPackage=Package.Open(fileStream,FileMode.Open,FileAccess.ReadWrite);

foreach(PackageRelationshiprelationshipinoriginalDocPackage.GetRelationshipsByType(documentRelationshipType))

{

UridocumentUri=PackUriHelper.ResolvePartUri(newUri("/",UriKind.Relative),relationship.TargetUri);

originalDocumentPart=originalDocPackage.GetPart(documentUri);

break;

}

originalXmlDocument=newXmlDocument();

originalXmlDocument.Load(originalDocumentPart.GetStream());

NameTablent=newNameTable();

XmlNamespaceManagerxmlNamespaceManager=newXmlNamespaceManager(nt);

xmlNamespaceManager.AddNamespace("w",strUri);

#region多个书签轮回检索

XmlNodeListbookmarknodeList=originalXmlDocument.SelectNodes("//w:bookmarkStart",xmlNamespaceManager);

XmlNodeListbookmarknodeEndList=originalXmlDocument.SelectNodes("//w:bookmarkEnd",xmlNamespaceManager);

XmlNodeparentNodeOfBookMark;

foreach(XmlNodexmlnodeinbookmarknodeList)

{

foreach(stringcolumnNameinhvalues.Key亚洲必赢App官方下s)

{

if(xmlnode.Attributes["w:name"].Value==columnName)

{

if(xmlnode.NextSibling.Name=="w:r")

{

xmlnode.NextSibling.RemoveChild(xmlnode.NextSibling.LastChild);

XmlElementtextElement=originalXmlDocument.CreateElement("w:t",strUri);

xmlnode.NextSibling.AppendChild(textElement);

XmlNodetextNode=originalXmlDocument.CreateNode(XmlNodeType.Text,"w:t",strUri);

if(hvalues[columnName].ToString().EndsWith("T00:00:00"))

textNode.Value=hvalues[columnName].ToString().Substring(0,hvalues[columnName].亚洲必赢App官方下ToString().IndexOf('T'));

else

textNode.Value=hvalues[columnName].ToString();

textElement.AppendChild(textNode);

}

}

}

parentNodeOfBookMark=xmlnode.ParentNode;

parentNodeOfBookMark.RemoveChild(xmlnode);

}

foreach(XmlNodexmlnodeinbookmarknodeEndList)

{

parentNodeOfBookMark=xmlnode.ParentNode;

parentNodeOfBookMark.RemoveChild(xmlnode);

}

#endregion

//savedocument

originalXmlDocument.Save(originalDocumentPart.GetStream(FileMode.Create,FileAccess.Write));

byte[]bs=fileStream.ToArray();

apws.SPSaveBinary(newfilename,bs);

originalDocPackage.Close();

#endregion

以上只是贴出了此中主要的代码

免责声明:以上内容源自网络,版权归原作者所有,如有侵犯您的原创版权请告知,我们将尽快删除相关内容。

您可能还会对下面的文章感兴趣: