已有56人关注
《C#开发实战宝典》第29章企业人事管理系统
发表在C#图书答疑 2011-03-04
是否精华
版块置顶:
F_ManFile.cs 如果使用的是office 2007 ,word 引用的不是9.0,11.0 ,而是12.0,请问老师有没有相应的代码,谢谢!
分享到:
精彩评论 2
东方_mrkj
学分:0 LV1
2011-03-07
沙发
读者你好:
关于C#操作offic2007,看看下面的实例代码对你是否有帮助
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);
        }

}

donlad
学分:0 LV1
TA的每日心情
奋斗
2022-01-25 10:09:43
2011-03-07
板凳
谢谢老师
首页上一页 1 下一页尾页 2 条记录 1/1页
手机同步功能介绍
友情提示:以下图书配套资源能够实现手机同步功能
明日微信公众号
明日之星 明日之星编程特训营
客服热线(每日9:00-17:00)
400 675 1066
mingrisoft@mingrisoft.com
吉林省明日科技有限公司Copyright ©2007-2022,mingrisoft.com, All Rights Reserved长春市北湖科技开发区盛北大街3333号长春北湖科技园项目一期A10号楼四、五层
吉ICP备10002740号-2吉公网安备22010202000132经营性网站备案信息 营业执照