-- 作者:卷积内核
-- 发布时间:1/9/2010 2:40:00 PM
-- C# XML导出Word方法
方法的调用: Code Dictionary<string, string> wordTexts = new Dictionary<string, string>(); Dictionary<string, DataTable> wordTable = new Dictionary<string, DataTable>(); WordMLHelper Word = new WordMLHelper(); wordTexts.Clear(); wordTexts.Add("XML中书签名称",“导出的内容”); Word.SetNodeText(wordTexts); DataTable dt=getDateTable();//获得一个数据表 wordTable.Clear(); wordTable.Add("XML中书签名称", dt); Word.SetNodeTable(wordTable); Word.Save("存储地址"); 操作类的代码如下: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Xml; using System.Drawing; using System.IO; using System.Data; using System.Web.UI; using System.Web.UI.WebControls; using System.Collections; using System.Web; /// /// 导出word /// Author:FreezeSoul&Worm /// 操作WordML(2003)根据标签插入文本、表格、图片 /// public class WordMLHelper { /// /// 最短路径图片在导出Word文件中的宽度 /// webconfig中加入节点 /// public string WordWidth { get { return System.Configuration.ConfigurationSettings.AppSettings["WordWidth"].ToString(); } } /// /// 最短路径图片在导出Word文件中的高度 /// webconfig中加入节点 /// public string WordHeight { get { return System.Configuration.ConfigurationSettings.AppSettings["WordHeight"].ToString(); } } private XmlNamespaceManager nsmgr; private XmlDocument xdoc; public void CreateXmlDom(string xmlPath) { xdoc = new XmlDocument(); xdoc.Load(xmlPath); nsmgr = new XmlNamespaceManager(xdoc.NameTable); nsmgr.AddNamespace("w", "http://schemas.microsoft.com/office/word/2003/wordml"); nsmgr.AddNamespace("v", "urn:schemas-microsoft-com:vml"); nsmgr.AddNamespace("aml", "http://schemas.microsoft.com/aml/2001/core"); nsmgr.AddNamespace("wx", "http://schemas.microsoft.com/office/word/2003/auxHint"); } string serverPath = HttpContext.Current.Server.MapPath("~");//Replace("../", "\\").Replace("/", "\\") #region 导出Word文档的方法 /// /// 设置节点的值 /// /// public void SetNodeText(Dictionary<string, string> bookmarks) { XmlNodeList nodeList = xdoc.SelectNodes("//aml:annotation[@w:type='Word.Bookmark.Start']", nsmgr); foreach (XmlNode node in nodeList) { SetBookMarkTexts(xdoc, node, bookmarks); } } /// /// 根据数据集插入数据,数据集中的数据表含有一条数据 /// /// public void SetNodeTextDataSet(DataSet ds) { Dictionary<string, string> bookmarkValues = new Dictionary<string, string>(); foreach (System.Data.DataTable dt in ds.Tables) { foreach (DataColumn dc in dt.Columns) { if (dt.Rows.Count > 0) { bookmarkValues.Add(dt.TableName + "_" + dc.ColumnName, dt.Rows[0][dc.ColumnName].ToString()); } } } SetNodeText(bookmarkValues); } /// /// 根据数据表插入数据,数据表含有一条数据 /// /// public void SetNodeTextDataTable(System.Data.DataTable dt) { Dictionary<string, string> bookmarkValues = new Dictionary<string, string>(); foreach (DataColumn dc in dt.Columns) { if (dt.Rows.Count > 0) { bookmarkValues.Add(dt.TableName + "_" + dc.ColumnName, dt.Rows[0][dc.ColumnName].ToString()); } } SetNodeText(bookmarkValues); } /// /// 插入一张表数据,表中含有多条数据 /// /// public void SetNodeTable(Dictionary<string, System.Data.DataTable> tables) { Dictionary<string, WordTable> wordtables = new Dictionary<string, WordTable>(); foreach (KeyValuePair<string, System.Data.DataTable> table in tables) { wordtables.Add(table.Key, WordTable.GetWordTabelFromDataTable(table.Value)); } XmlNodeList nodeList = xdoc.SelectNodes("//aml:annotation[@w:type='Word.Bookmark.Start']", nsmgr); foreach (XmlNode node in nodeList) { SetBookMarkTablesHorizontal(xdoc, node, wordtables); } } /// /// 插入一张表数据,表中含有多条数据 /// /// public void SetNodeTable(Dictionary<string, WordTable> wordtables) { XmlNodeList nodeList = xdoc.SelectNodes("//aml:annotation[@w:type='Word.Bookmark.Start']", nsmgr); foreach (XmlNode node in nodeList) { SetBookMarkTablesHorizontal(xdoc, node, wordtables); } } /// /// 插入一张表数据,表中含有多条数据 /// /// /// 是否为水平列表,即列名在数据上方 public void SetNodeTable(Dictionary<string, System.Data.DataTable> tables, bool IsHorizontal) { Dictionary<string, WordTable> wordtables = new Dictionary<string, WordTable>(); foreach (KeyValuePair<string, System.Data.DataTable> table in tables) { wordtables.Add(table.Key, WordTable.GetWordTabelFromDataTable(table.Value)); } XmlNodeList nodeList = xdoc.SelectNodes("//aml:annotation[@w:type='Word.Bookmark.Start']", nsmgr); foreach (XmlNode node in nodeList) { if (IsHorizontal) SetBookMarkTablesHorizontal(xdoc, node, wordtables); else SetBookMarkTablesVertical(xdoc, node, wordtables); } } /// /// 插入一张表数据,表中含有多条数据 /// /// /// 是否为水平列表,即列名在数据上方 public void SetNodeTable(Dictionary<string, WordTable> wordtables, bool IsHorizontal) { XmlNodeList nodeList = xdoc.SelectNodes("//aml:annotation[@w:type='Word.Bookmark.Start']", nsmgr); foreach (XmlNode node in nodeList) { if (IsHorizontal) SetBookMarkTablesHorizontal(xdoc, node, wordtables); else SetBookMarkTablesVertical(xdoc, node, wordtables); } } /// /// 插入一张表数据,表中含有多条数据 /// 图片的列是传入datatabele的最后一列,且最后一列为图片的绝对地址 /// /// /// 是否为水平列表,即列名在数据上方 /// 是否含有图片 public void SetNodeTable(Dictionary<string, System.Data.DataTable> tables, bool IsHorizontal, bool ExistPicture) { Dictionary<string, WordTable> wordtables = new Dictionary<string, WordTable>(); foreach (KeyValuePair<string, System.Data.DataTable> table in tables) { wordtables.Add(table.Key, WordTable.GetWordTabelFromDataTable(table.Value)); } XmlNodeList nodeList = xdoc.SelectNodes("//aml:annotation[@w:type='Word.Bookmark.Start']", nsmgr); foreach (XmlNode node in nodeList) { if (ExistPicture) { if (IsHorizontal) SetBookMarkTablesPictureHorizontal(xdoc, node, wordtables); else SetBookMarkTablesPictureVertical(xdoc, node, wordtables); } else { if (IsHorizontal) SetBookMarkTablesHorizontal(xdoc, node, wordtables); else SetBookMarkTablesVertical(xdoc, node, wordtables); } } } /// /// 插入一张表数据,表中含有多条数据 /// 图片的列是传入wordtables的最后一列,且最后一列为图片的绝对地址 /// /// /// 是否为水平列表,即列名在数据上方 /// 是否含有图片 public void SetNodeTable(Dictionary<string, WordTable> wordtables, bool IsHorizontal, bool ExistPicture) { XmlNodeList nodeList = xdoc.SelectNodes("//aml:annotation[@w:type='Word.Bookmark.Start']", nsmgr); foreach (XmlNode node in nodeList) { if (ExistPicture) { if (IsHorizontal) SetBookMarkTablesPictureHorizontal(xdoc, node, wordtables); else SetBookMarkTablesPictureVertical(xdoc, node, wordtables); } else { if (IsHorizontal) SetBookMarkTablesHorizontal(xdoc, node, wordtables); else SetBookMarkTablesVertical(xdoc, node, wordtables); } } } /// /// 设置插入的图品数据 /// /// public void SetNodePic(Dictionary<string, WordPic> picpaths) { XmlNodeList nodeList = xdoc.SelectNodes("//aml:annotation[@w:type='Word.Bookmark.Start']", nsmgr); //XmlNodeList nodeList = xdoc.SelectNodes("//w:bookmarkStart", nsmgr); foreach (XmlNode node in nodeList) { SetBookMarkPics(xdoc, node, picpaths); } } /// /// 保存导出的数据 /// /// public void Save(string docPath) { xdoc.Save(docPath); } #endregion #region 导出word文档内部方法 /// /// 根据标签插入对应的值 /// /// /// /// private void SetBookMarkTexts(XmlDocument xdoc, XmlNode node, Dictionary<string, string> bookmarks) { //找到bookmark结束标记 if (node.NextSibling.Name.ToString() == "aml:annotation") { //查找bookmarks中是否在word标签 if (bookmarks.ContainsKey(node.Attributes["w:name"].Value)) { //得到node上一个兄弟节点style ("w:rPr")的克隆,以备添给新加内容,样式继承 XmlNode nodeStyle = null; if (node.PreviousSibling != null) nodeStyle = node.PreviousSibling.CloneNode(true); else nodeStyle = node.ParentNode.SelectNodes("//w:r", nsmgr)[0].CloneNode(true); //父节点 "w:p" XmlNode bookmrkParent = node.ParentNode; XmlElement tagRun; tagRun = xdoc.CreateElement("w:r", nsmgr.LookupNamespace("w")); bookmrkParent.AppendChild(tagRun); //添加样式 if (nodeStyle.SelectSingleNode("//w:rPr", nsmgr) != null) tagRun.AppendChild(nodeStyle.SelectSingleNode("//w:rPr", nsmgr)); XmlElement tagText; tagText = xdoc.CreateElement("w:t", nsmgr.LookupNamespace("w")); tagRun.AppendChild(tagText); //插入(w:t)文本作为内容,追加至文本节点 if (bookmarks[node.Attributes["w:name"].Value] == null) return; XmlNode nodeText; nodeText = xdoc.CreateNode(XmlNodeType.Text, "w:t", nsmgr.LookupNamespace("w")); nodeText.Value = bookmarks[node.Attributes["w:name"].Value].ToString(); tagText.AppendChild(nodeText); } } } /// /// 根据标签水平插入一张表数据,即列名在数据上侧 /// /// /// /// private void SetBookMarkTablesHorizontal(XmlDocument xdoc, XmlNode node, Dictionary<string, WordTable> wordtables) { if (node.NextSibling.Name.ToString() == "aml:annotation") { //查找bookmarks中是否在word标签 if (wordtables.ContainsKey(node.Attributes["w:name"].Value)) { // "w:p"节点的父节点 XmlNode bookmrkParent = node.ParentNode; XmlNode bookmrkParentParent = node.ParentNode.ParentNode; XmlElement tagtable; tagtable = xdoc.CreateElement("w:tbl", nsmgr.LookupNamespace("w")); bookmrkParentParent.InsertAfter(tagtable, bookmrkParent); SetImportTableBorder(ref xdoc, ref tagtable); XmlElement tagtr; tagtr = xdoc.CreateElement("w:tr", nsmgr.LookupNamespace("w")); tagtable.AppendChild(tagtr); foreach (string headstr in wordtables[node.Attributes["w:name"].Value].TableHeads) { SetTableTitle(ref tagtr, headstr); } for (int i = 0; i < wordtables[node.Attributes["w:name"].Value].TableValues.GetLength(0); i++) { tagtr = xdoc.CreateElement("w:tr", nsmgr.LookupNamespace("w")); tagtable.AppendChild(tagtr); for (int j = 0; j < wordtables[node.Attributes["w:name"].Value].TableValues.GetLength(1); j++) { string content = wordtables[node.Attributes["w:name"].Value].TableValues[i, j]; SetTableContent(ref tagtr, content); } } } } } /// /// 根据标签垂直插入一张表数据,即列名在数据左侧 /// /// /// /// private void SetBookMarkTablesVertical(XmlDocument xdoc, XmlNode node, Dictionary<string, WordTable> wordtables) { if (node.NextSibling.Name.ToString() == "aml:annotation") { //查找bookmarks中是否在word标签 if (wordtables.ContainsKey(node.Attributes["w:name"].Value)) { // "w:p"节点的父节点 XmlNode bookmrkParent = node.ParentNode; XmlNode bookmrkParentParent = node.ParentNode.ParentNode; XmlElement tagtable; tagtable = xdoc.CreateElement("w:tbl", nsmgr.LookupNamespace("w")); bookmrkParentParent.InsertAfter(tagtable, bookmrkParent); //设置表格样式 SetImportTableBorder(ref xdoc, ref tagtable); for (int i = 0; i < wordtables[node.Attributes["w:name"].Value].TableValues.GetLength(0); i++) { for (int j = 0; j < wordtables[node.Attributes["w:name"].Value].TableValues.GetLength(1); j++) { XmlElement tagtr; tagtr = xdoc.CreateElement("w:tr", nsmgr.LookupNamespace("w")); tagtable.AppendChild(tagtr); if (wordtables[node.Attributes["w:name"].Value].TableHeads[j] == null) return; string headstr = wordtables[node.Attributes["w:name"].Value].TableHeads[j]; SetTableTitle(ref tagtr, headstr); if (wordtables[node.Attributes["w:name"].Value].TableValues[i, j] == null) return; string content = wordtables[node.Attributes["w:name"].Value].TableValues[i, j]; SetTableContent(ref tagtr, content); } } } } }
|