网络编程 | 站长之家 | 网页制作 | 图形图象 | 操作系统 | 冲浪宝典 | 软件教学 | 网络办公 | 邮件系统 | 网络安全 | 认证考试 | 系统进程
Firefox | IE | Maxthon | 迅雷 | 电驴 | BitComet | FlashGet | QQ | QQ空间 | Vista | 输入法 | Ghost | Word | Excel | wps | Powerpoint
asp | .net | php | jsp | Sql | c# | Ajax | xml | Dreamweaver | FrontPages | Javascript | css | photoshop | fireworks | Flash | Cad | Discuz!
当前位置 > 网站建设学院 > 网络编程 > C#应用
Tag:注入,存储过程,分页,安全,优化,xmlhttp,fso,jmail,application,session,防盗链,stream,无组件,组件,md5,乱码,缓存,加密,验证码,算法,cookies,ubb,正则表达式,水印,索引,日志,压缩,base64,url重写,上传,控件,Web.config,JDBC,函数,内存,PDF,迁移,结构,破解,编译,配置,进程,分词,IIS,Apache,Tomcat,phpmyadmin,Gzip,触发器,socket
网络编程:ASP教程,ASP.NET教程,PHP教程,JSP教程,C#教程,数据库,XML教程,Ajax,Java,Perl,Shell,VB教程,Delphi,C/C++教程,软件工程,J2EE/J2ME,移动开发
本月文章推荐
.发送邮件程序(Csharp2005).
.C#下从HTML文件生成DOM树.
.从Internet上抓取指定URL的源码的.
.C#代表元及事件触发.
.C#3.0 中使用扩展方法来扩展接口.
.在C#中把两个DataTable连接起来,.
.C#源码读取excel数据到程序中-SQ.
.在指定应用程序域中执行代码.
.c#中连接SqL2005数据库错误解决及.
.用C#开发智能手机软件:推箱子(五.
.C#中时间格式的转换.
.C#将文件保存到数据库中或者从数.
.通过COM来获取CookieContainer,简.
.用C#代码生成一个简单的PDF文件.
.如何让数据访问过程更透明.
.c#调用oracle存储过程返回结果集.
.关于C#和C++的重载(Overload)、.
.用C#开发智能手机软件:推箱子(二.
.c#中分割字符串的几种方法.
.C#注册表的读,写,删除,查找.

C#操作Word文档(Office 2007)

发表日期:2006-12-14


首先引入类库,Microsoft.Office.Interop.Word,然后进行编程。代码如下:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using 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 strContent = "";

        public 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);
            Object Nothing = System.Reflection.Missing.Value;
            myWordDoc = myWordApp.Documents.Add(ref Nothing, ref Nothing, ref Nothing, ref Nothing);

            #region 将数据库中读取得数据写入到word文件中

            strContent = "你好\n\n\r";
            myWordDoc.Paragraphs.Last.Range.Text = strContent;

            strContent = "这是测试程序";
            myWordDoc.Paragraphs.Last.Range.Text = strContent;


            #endregion

            //将WordDoc文档对象的内容保存为DOC文档
            myWordDoc.SaveAs(ref strFileName, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing);
            //关闭WordDoc文档对象
            myWordDoc.Close(ref Nothing, ref Nothing, ref Nothing);
            //关闭WordApp组件对象
            myWordApp.Quit(ref Nothing, ref Nothing, ref Nothing);

            this.richTextBox1.Text = strFileName + "\r\n" + "创建成功";

        }
        private void openWord()
        {
            fontDialog1.ShowDialog();
            System.Drawing.Font font = fontDialog1.Font;
            object filepath = "D:\\asp.docx";
            object oMissing = System.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.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);
        }

}

http://www.cnblogs.com/wrtandy/archive/2006/12/13/andy.html

上一篇:C#中使用TextBox控件的两个问题 人气:6118
下一篇:VS.Net C# 调用 Active 组件 人气:5204
浏览全部C#的内容 Dreamweaver插件下载 网页广告代码 祝你圣诞节快乐 2009年新年快乐