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);
}
}
}
}
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);
}
}
}
}
相关文章推荐
- C#操作Word(打开创建,选择复制,写入数据,保存关闭)
- asp.net C# 打开word文件时 命令失败
- c#之Word基本操作----创建,打开,绘制表格,插入图片等
- C#创建一个Word并打开
- C#窗体中利用richtextbox控件,打开指定word文件,并进行编辑保存等操作,出现Office或者是Interop未定义的解决方案
- java创建word打开后弹出无法打开office open xml文件....因为内容有错误提示
- C#创建一个Word并打开的方法
- C# 导出Word报”无法打开Office open xml文件。因为文件内容有错误“ 解决方法
- C#winform 选择的文件是解决方案文件,但是用此应用程序的较新版本创建的,无法打开(低版本的VS无法打开高版本项目)
- C# 导出Word报”无法打开Office open xml文件。因为文件内容有错误“ 解决方法
- C#创建Word文件,并且写入数据的程序
- C#中打开word 文件并在richTextBox中显示
- C#打开或者创建一个文件,然后向其末尾写入数据的方法
- C#打开Word/ppt/Mp3/视频等文件的代码
- C#加载word文件 一切配置正常 wordApp.Documents.Open打开文件为null的解决fang'a
- C# 打开TXT文件读取内容
- C#打开文件 ,文件夹
- C#与Access数据库文件的创建
- C#关闭word时,提示“此文件正由另一个应用程序或用户使用” normal.dot
- [原创]C#操作Excel(创建、打开、读写、保存)几种方法的总结(一)