建体彩网|中彩网双色球连号|
?
快捷搜索:  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("http://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("http://w:bookmarkStart",xmlNamespaceManager);

XmlNodeListbookmarknodeEndList=originalXmlDocument.SelectNodes("http://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

以上只是貼出了此中主要的代碼

免責聲明:以上內容源自網絡,版權歸原作者所有,如有侵犯您的原創版權請告知,我們將盡快刪除相關內容。

您可能還會對下面的文章感興趣:

建体彩网
190足球即时指数手机版 彩票开奖设计 手机捕鱼王2游戏大厅 重庆百变王牌预测 快乐蓝球 比分直播新浪竞彩足球 上海时时彩预测 排列五走势图带连线图专业版 新浪体育nba直播表 贵州快3下载安装