C#操作保存成word文档

上传人:ji****72 文档编号:37521877 上传时间:2018-04-17 格式:DOC 页数:8 大小:43.50KB
返回 下载 相关 举报
C#操作保存成word文档_第1页
第1页 / 共8页
C#操作保存成word文档_第2页
第2页 / 共8页
C#操作保存成word文档_第3页
第3页 / 共8页
C#操作保存成word文档_第4页
第4页 / 共8页
C#操作保存成word文档_第5页
第5页 / 共8页
点击查看更多>>
资源描述

《C#操作保存成word文档》由会员分享,可在线阅读,更多相关《C#操作保存成word文档(8页珍藏版)》请在金锄头文库上搜索。

1、C#C# saveFileDialog1saveFileDialog1 保存成保存成 wordword 文档,文档, 并写入内容并写入内容浏览: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 = (Sys

2、tem.IO.FileStream)saveFileDialog1.OpenFile();/输出文件 可是打开文件里面没有写入的内容,是怎么回事?我在网上收到的代码,大家看看。 首先引入类库,Microsoft.Office.Interop.Word,然后进行编程。代码如下:Copy to clipboard CODE:using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; us

3、ing System.Windows.Forms; using Microsoft.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

4、Form1() InitializeComponent(); private 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);

5、 Object Nothing = System.Reflection.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 /将 W

6、ordDoc 文档对象的内容保存为 DOC 文档 myWordDoc.SaveAs(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, re

7、f Nothing, ref Nothing); /关闭 WordApp 组件对象 myWordApp.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 = Syst

8、em.Reflection.Missing.Value; myWordDoc = myWordApp.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

9、.Content.Font.Size = font.Size; myWordDoc.Content.Font.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 控件呈现 在相同层级中前后/上下移动

10、节点 删除指定值的节点 把 TreeView 的节点存储到 XML 文件中 开发环境:Windows XP SP2, Visual Studio 2005 with SP1 XML 数据的组织结构 我的 XML 数据结构如下所示:其中“nodeText“为在 TreeView 中显示的节点文本,“nodeValue“为该节点实际存储的值,而 “ a,b,c,d,e,f “为预定义的 6 个测试值 用 TreeView 控件呈现 XML 数据 把上述的 XML 文件的数据呈现到 TreeView 控件中主要使用了如下的递归处理:变量#region 变量private bool enabled =

11、 false;private string strXmlPath = null;private XmlDocument xmlDoc = null; #endregion 方法#region 方法/ / 根据 XML 文件加载树节点/ / 要加载的 XML 文件路径private void LoadTreeNodes(string xmlPath).this.xmlDoc = new XmlDocument();try.this.xmlDoc.Load(xmlPath);XmlNodeList nodes = this.xmlDoc.SelectNodes(“TestTreeView/Test

12、Node“);this.treeMain.BeginUpdate();this.treeMain.Nodes.Clear();this.ConvertXmlNodeToTreeNode(nodes, this.treeMain.Nodes);this.treeMain.EndUpdate();catch (Exception ex).MessageBox.Show(“程序发生错误:“ + ex.Message, “异常“, MessageBoxButtons.OK, MessageBoxIcon.Error);private void ConvertXmlNodeToTreeNode(XmlN

13、odeList xmlNodes, TreeNodeCollection treeNodes).foreach (XmlNode xmlNode in xmlNodes).string nodeText = xmlNode.Attributes“nodeText“.Value;string nodeValue = xmlNode.Attributes“nodeValue“.Value;TreeNode newTreeNode = new TreeNode(nodeText);newTreeNode.Tag = nodeValue;if (xmlNode.HasChildNodes).this.

14、ConvertXmlNodeToTreeNode(xmlNode.ChildNodes, newTreeNode.Nodes);treeNodes.Add(newTreeNode); #endregion 在相同层级中前后/上下移动节点 这里的移动节点没有使用常见的定义一个 Temp 变量来存储临时数据,然后把要移动的 2 个数据交换位置。以“上移”为例,先把要移动的节点删除,再在该节点的前一个节点的位 置插入该节点,在删除前把该节点存储在一个临时变量中,而“下移”则类似,下面是主要 的代码:/ 在同级中上移当前节点private void btnMoveUp_Click(object sen

15、der, EventArgs e).TreeNode current = this.treeMain.SelectedNode;TreeNode temp = 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

16、.Nodes.Remove(current);this.treeMain.Nodes.Insert(prev.Index, 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 parent = current.Parent;pa

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 行业资料 > 其它行业文档

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号