您的位置:首页 > 编程语言 > C#

C#创建和打开word文件

2012-12-27 17:37 357 查看
using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Windows.Forms;

using System.Threading;

using MSWord = Microsoft.Office.Interop.Word;//添加上对Microsoft Word 12.0 object library的引用

using System.IO;

using System.Reflection;

namespace AddLetter

{

    public partial class Frm_Main : Form

    {

        public Frm_Main()

        {

            InitializeComponent();

        }

        private void button1_Click(object sender, EventArgs e)

        {

            FolderBrowserDialog pFolderBrowserDialog = new FolderBrowserDialog();//选择保存路径

            if (pFolderBrowserDialog.ShowDialog() == DialogResult.OK)

            {

                textBox1.Text = pFolderBrowserDialog.SelectedPath;

            }

        }

        private void button2_Click(object sender, EventArgs e)

        {

            OpenFileDialog pOpenFileDialog = new OpenFileDialog();

            pOpenFileDialog.InitialDirectory = textBox1.Text;

            pOpenFileDialog.Title = "打开新建的Word文件";

            pOpenFileDialog.Filter
4000
= "Word文件|*.doc";

            DialogResult pDialogResult = pOpenFileDialog.ShowDialog();

            if (pDialogResult == DialogResult.Cancel)

            {

                return;

            }

            else

            {

                //建立Word类的实例,缺点:不能正确读取表格,图片等等的显示  

                MSWord.ApplicationClass app = new MSWord.ApplicationClass();

                MSWord.Document doc = null;

                object missing = System.Reflection.Missing.Value;

                object FileName = pOpenFileDialog.FileName;

                try

                {

                    doc = app.Documents.Open(ref FileName, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);

                }

                catch

                { }

            }

        }

        private void button3_Click(object sender, EventArgs e)

        {

            if (this.textBox2.Text == "")

            {

                return;

            }

            if (textBox1.Text == "")

            {

                return;

            }

            string FileName = textBox1.Text+"\\";

            FileName += DateTime.Now.ToString("yyyy年M月d日h时s分m秒fff毫秒") + ".doc";

            try

            {

                MSWord.ApplicationClass word = new MSWord.ApplicationClass();

                MSWord.Document doc;

                object nothing = System.Reflection.Missing.Value;

                doc = word.Documents.Add(ref nothing, ref nothing, ref nothing, ref nothing);

                doc.Paragraphs.Last.Range.Text = this.textBox2.Text;

                object myfileName = FileName;

                object format = MSWord.WdSaveFormat.wdFormatDocument97;

                doc.SaveAs(ref myfileName, ref format, 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);

                doc.Close(ref nothing, ref nothing, ref nothing);

                word.Quit(ref nothing, ref nothing, ref nothing);

                MessageBox.Show("Word文件保存成功", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }

            catch (System.Exception ex)

            {

                MessageBox.Show(this, ex.Message.ToString(), "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }

        }

    }

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: