《C#操作保存成word文档.doc》由会员分享,可在线阅读,更多相关《C#操作保存成word文档.doc(8页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、C# saveFileDialog1 保存成word文档,并写入内容 浏览:2次 时间:2012-02-16 10:40:44 我是这样写的if (saveFileDialog1.ShowDialog() = DialogResult.OK) StreamWriter sw = new StreamWriter(saveFileDialog1.FileName, true); sw.Write(那些年,一起追的女孩儿!); sw.Flush(); sw.Close(); System.IO.FileStream fs = (System.IO.FileStream)saveFileDialog
2、1.OpenFile();/输出文件 可是打开文件里面没有写入的内容,是怎么回事?我在网上收到的代码,大家看看。首先引入类库,Microsoft.Office.Interop.Word,然后进行编程。代码如下:Copy to clipboardCODE: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using M
3、icrosoft.Office.Interop.Word; namespace WordTest public partial class Form1 : Form object strFileName; Object Nothing; Microsoft.Office.Interop.Word.Application myWordApp = new Microsoft.Office.Interop.Word.ApplicationClass(); Document myWordDoc; string strC; public Form1() InitializeComponent(); pr
4、ivate void button1_Click(object sender, EventArgs e) createWord(); /openWord(); private void createWord() strFileName = System.Windows.Forms.Application.StartupPath + test.doc; if (System.IO.File.Exists(string)strFileName) System.IO.File.Delete(string)strFileName); Object Nothing = System.Reflection
5、.Missing.Value; myWordDoc = myWordApp.Documents.Add(ref Nothing, ref Nothing, ref Nothing, ref Nothing); #region 将数据库中读取得数据写入到word文件中 strC; myWordDoc.Paragraphs.Last.Range.Text = strContent; strC; myWordDoc.Paragraphs.Last.Range.Text = strContent; #endregion /将WordDoc文档对象的内容保存为DOC文档 myWordDoc.SaveAs
6、(ref strFileName, ref Nothing, ref Nothing, refNothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, refNothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, refNothing, ref Nothing, ref Nothing); /关闭WordDoc文档对象 myWordDoc.Close(ref Nothing, ref Nothing, ref Nothing); /关闭WordApp组件对象 myW
7、ordApp.Quit(ref Nothing, ref Nothing, ref Nothing); this.richTextBox1.Text = strFileName + rn + 创建成功; private void openWord() fontDialog1.ShowDialog(); System.Drawing.Font font = fontDialog1.Font; object filepath = D:asp.docx; object oMissing = System.Reflection.Missing.Value; myWordDoc = myWordApp.
8、Documents.Open(ref filepath, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing); myWordDoc.Content.Font.Size = font.Size; myWordDoc.Content.F
9、ont.Name = font.Name; myWordDoc.Save(); richTextBox1.Text = myWordDoc.Content.Text; myWordDoc.Close(ref oMissing, ref oMissing, ref oMissing); myWordApp.Quit(ref oMissing, ref oMissing, ref oMissing); 读取XML文件中的树状结构数据,并用TreeView控件呈现在相同层级中前后/上下移动节点删除指定值的节点把TreeView的节点存储到XML文件中开发环境:Windows XP SP2, Visu
10、al Studio 2005 with SP1XML数据的组织结构我的XML数据结构如下所示: 其中nodeText为在TreeView中显示的节点文本,nodeValue为该节点实际存储的值,而 a,b,c,d,e,f 为预定义的6个测试值用TreeView控件呈现XML数据把上述的XML文件的数据呈现到TreeView控件中主要使用了如下的递归处理:变量#region 变量 private bool enabled = false; private string strXmlPath = null; private XmlDocument xmlDoc = null;#endregion方
11、法#region 方法 / / 根据XML文件加载树节点 / / 要加载的XML文件路径 private void LoadTreeNodes(string xmlPath) . this.xmlDoc = new XmlDocument(); try . this.xmlDoc.Load(xmlPath); XmlNodeList nodes = this.xmlDoc.SelectNodes(TestTreeView/TestNode); this.treeMain.BeginUpdate(); this.treeMain.Nodes.Clear(); this.ConvertXmlNod
12、eToTreeNode(nodes, this.treeMain.Nodes); this.treeMain.EndUpdate(); catch (Exception ex) . MessageBox.Show(程序发生错误: + ex.Message, 异常, MessageBoxButtons.OK, MessageBoxIcon.Error); private void ConvertXmlNodeToTreeNode(XmlNodeList xmlNodes, TreeNodeCollection treeNodes) . foreach (XmlNode xmlNode in xm
13、lNodes) . string nodeText = xmlNode.AttributesnodeText.Value; string nodeValue = xmlNode.AttributesnodeValue.Value; TreeNode newTreeNode = new TreeNode(nodeText); newTreeNode.Tag = nodeValue; if (xmlNode.HasChildNodes) . this.ConvertXmlNodeToTreeNode(xmlNode.ChildNodes, newTreeNode.Nodes); treeNodes
14、.Add(newTreeNode); #endregion 在相同层级中前后/上下移动节点这里的移动节点没有使用常见的定义一个Temp变量来存储临时数据,然后把要移动的2个数据交换位置。以“上移”为例,先把要移动的节点删除,再在该节点的前一个节点的位置插入该节点,在删除前把该节点存储在一个临时变量中,而“下移”则类似,下面是主要的代码: / 在同级中上移当前节点 private void btnMoveUp_Click(object sender, EventArgs e) . TreeNode current = this.treeMain.SelectedNode; TreeNode te
15、mp = null; if (current != null) . temp = current; TreeNode prev = current.PrevNode; if (current.Parent != null) . TreeNode parent = current.Parent; parent.Nodes.Remove(current); parent.Nodes.Insert(prev.Index, temp); else . this.treeMain.Nodes.Remove(current); this.treeMain.Nodes.Insert(prev.Index,
16、temp); this.treeMain.SelectedNode = temp; / 在同级中下移当前节点 private void btnMoveDown_Click(object sender, EventArgs e) . TreeNode current = this.treeMain.SelectedNode; if (current != null) . TreeNode next = current.NextNode; if (next != null) . TreeNode temp = next; if (current.Parent != null) . TreeNode
17、 parent = current.Parent; parent.Nodes.Remove(next); parent.Nodes.Insert(current.Index, temp); else . this.treeMain.Nodes.Remove(next); this.treeMain.Nodes.Insert(current.Index, temp); this.treeMain.SelectedNode = temp; this.treeMain.SelectedNode = current; 删除指定值的节点TreeView节点实际存储的值保存在TreeNode.Tag中,选
18、择要删除的值,然后搜索定位所有节点,最后删除这些节点,重点是遍历所有TreeView的节点搜索具有该值的节点,下面是主要代码: / 定位指定值的节点,其中listNodesLocated是一个ListBox控件 / 列出所有定位到的节点的名称 private void LocateNodeWithValue(TreeNodeCollection nodes, string nodeValue) . foreach (TreeNode node in nodes) . if (node.Tag.ToString() = nodeValue) . this.listNodesLocated.Ite
19、ms.Add(node.Text); if (node.Nodes.Count 0) . this.LocateNodeWithValue(node.Nodes, nodeValue); / 删除上述方法定位到的节点 private void DeleteNode(TreeNodeCollection nodes, string valueToDelete) . foreach (TreeNode node in nodes) . if (node.Tag.ToString() = valueToDelete) . nodes.Remove(node); this.listNodesLocat
20、ed.Items.Remove(node.Text); else if (node.Nodes.Count 0) . this.DeleteNode(node.Nodes, valueToDelete); 保存TreeView的节点到XML文件中把编辑后的TreeView中的TreeNode类型的节点转换成XmlNode类型节点保存到XML文件中,该XML文件就是最初选择加载的XML文件,在保存之前要先删除XML中TestTreeView根节点下的所有子节点,主要方法如下: / 保存主方法 private void SaveTreeNodes(string filePath) . if (th
21、is.xmlDoc != null) . XmlNode root = this.xmlDoc.SelectSingleNode(TestTreeView); / 删除根节点下的所有原有节点 root.RemoveAll(); this.ConvertTreeNodesToXmlNodes(this.treeMain.Nodes, root); this.xmlDoc.Save(this.strXmlPath); MessageBox.Show(保存成功!, 信息, MessageBoxButtons.OK, MessageBoxIcon.Information); / 把TreeNode转换
22、成对应的XmlNode private void ConvertTreeNodesToXmlNodes(TreeNodeCollection treeNodes, XmlNode xmlNode) . XmlDocument doc = xmlNode.OwnerDocument; foreach (TreeNode treeNode in treeNodes) . XmlNode newElement = null; XmlAttribute attrValue = null; newElement = doc.CreateNode(XmlNodeType.Element, TestNode
23、, ); / Set nodeText attrValue = doc.CreateAttribute(nodeText); attrValue.Value = treeNode.Text; newElement.Attributes.Append(attrValue); / Set nodeValue attrValue = doc.CreateAttribute(nodeValue); attrValue.Value = treeNode.Tag.ToString(); newElement.Attributes.Append(attrValue); xmlNode.AppendChild(newElement); if (treeNode.Nodes.Count 0) . this.ConvertTreeNodesToXmlNodes(treeNode.Nodes, newElement);