日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > asp.net >内容正文

asp.net

如何:修改 Office Open XML 文档【转载】

發(fā)布時間:2024/6/5 asp.net 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 如何:修改 Office Open XML 文档【转载】 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

全文轉(zhuǎn)載自:http://msdn.microsoft.com/zh-cn/library/bb669125.aspx

本主題顯演示一個打開、修改和保存 Office Open XML 文檔的示例。

有關(guān) Office Open XML 的更多信息,請參見 www.openxmldeveloper.org(可能為英文網(wǎng)頁)。

示例

本示例查找文檔中的第一個段落元素。 示例從段落中檢索文本,然后刪除段落中的所有文本域。 它創(chuàng)建一個由第一個段落已轉(zhuǎn)換為大寫的文本構(gòu)成的新文本域。 然后將已更改的 XML 序列化為 Open XML 包并關(guān)閉該包。

本示例使用 WindowsBase 程序集中的類。 它使用 System.IO.Packaging 命名空間中的類型。

?

public static class LocalExtensions {public static string StringConcatenate(this IEnumerable<string> source){StringBuilder sb = new StringBuilder();foreach (string s in source)sb.Append(s);return sb.ToString();}public static string StringConcatenate<T>(this IEnumerable<T> source,Func<T, string> func){StringBuilder sb = new StringBuilder();foreach (T item in source)sb.Append(func(item));return sb.ToString();}public static string StringConcatenate(this IEnumerable<string> source, string separator){StringBuilder sb = new StringBuilder();foreach (string s in source)sb.Append(s).Append(separator);return sb.ToString();}public static string StringConcatenate<T>(this IEnumerable<T> source,Func<T, string> func, string separator){StringBuilder sb = new StringBuilder();foreach (T item in source)sb.Append(func(item)).Append(separator);return sb.ToString();} }class Program {public static string ParagraphText(XElement e){XNamespace w = e.Name.Namespace;return e.Elements(w + "r").Elements(w + "t").StringConcatenate(element => (string)element);}static void Main(string[] args){const string fileName = "SampleDoc.docx";const string documentRelationshipType ="http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument";const string stylesRelationshipType ="http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles";const string wordmlNamespace ="http://schemas.openxmlformats.org/wordprocessingml/2006/main";XNamespace w = wordmlNamespace;using (Package wdPackage = Package.Open(fileName, FileMode.Open, FileAccess.ReadWrite)){PackageRelationship docPackageRelationship =wdPackage.GetRelationshipsByType(documentRelationshipType).FirstOrDefault();if (docPackageRelationship != null){Uri documentUri = PackUriHelper.ResolvePartUri(new Uri("/", UriKind.Relative),docPackageRelationship.TargetUri);PackagePart documentPart = wdPackage.GetPart(documentUri);// Load the document XML in the part into an XDocument instance.XDocument xDoc = XDocument.Load(XmlReader.Create(documentPart.GetStream()));// Find the styles part. There will only be one.PackageRelationship styleRelation =documentPart.GetRelationshipsByType(stylesRelationshipType).FirstOrDefault();PackagePart stylePart = null;XDocument styleDoc = null;if (styleRelation != null){Uri styleUri = PackUriHelper.ResolvePartUri(documentUri, styleRelation.TargetUri);stylePart = wdPackage.GetPart(styleUri);// Load the style XML in the part into an XDocument instance.styleDoc = XDocument.Load(XmlReader.Create(stylePart.GetStream()));}XElement paraNode = xDoc.Root.Element(w + "body").Descendants(w + "p").FirstOrDefault();string paraText = ParagraphText(paraNode);// remove all text runsparaNode.Descendants(w + "r").Remove();paraNode.Add(new XElement(w + "r",new XElement(w + "t", paraText.ToUpper())));// Save the XML into the packageusing (XmlWriter xw =XmlWriter.Create(documentPart.GetStream(FileMode.Create, FileAccess.Write))){xDoc.Save(xw);}Console.WriteLine("New first paragraph: >{0}<", paraText.ToUpper());}}} }本文由作者:陳希章 于 2009/7/16 10:54:50 發(fā)布在:http://www.cnblogs.com/chenxizhang/
本文版權(quán)歸作者所有,可以轉(zhuǎn)載,但未經(jīng)作者同意必須保留此段聲明,且在文章頁面明顯位置給出原文連接,否則保留追究法律責任的權(quán)利。
更多博客文章,以及作者對于博客引用方面的完整聲明以及合作方面的政策,請參考以下站點:陳希章的博客中心

轉(zhuǎn)載于:https://www.cnblogs.com/chenxizhang/archive/2009/07/16/1524655.html

總結(jié)

以上是生活随笔為你收集整理的如何:修改 Office Open XML 文档【转载】的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。