我用C#编文本编辑器,想用openFileDialog打开doc格式的文件,在richtextbox 中显示出来
比如源程序中openFileDialog1.RestoreDirectory=true;if((openFileDialog1.ShowDialog()==DialogR...
比如源程序中
openFileDialog1.RestoreDirectory = true;
if ((openFileDialog1.ShowDialog() == DialogResult.OK) && openFileDialog1.FileName != "")
{
richTextBox1.LoadFile(openFileDialog1.FileName);,但是总是无法打开word啊,说总是文件格式错误 展开
openFileDialog1.RestoreDirectory = true;
if ((openFileDialog1.ShowDialog() == DialogResult.OK) && openFileDialog1.FileName != "")
{
richTextBox1.LoadFile(openFileDialog1.FileName);,但是总是无法打开word啊,说总是文件格式错误 展开
3个回答
展开全部
richTextBox1不能打开word文件的,只能将word文件转换成.rtf格式的文件才能载入richTextBox1中。
下面是将word文件转换成.rtf格式的C#代码,希望对你有帮助。
OpenFileDialog f = new OpenFileDialog();
if ((f.ShowDialog() == DialogResult.OK) && f.FileName != "")
{
//创建一个word的实例
Microsoft.Office.Interop.Word.Application newApp = new Microsoft.Office.Interop.Word.Application();
// 指定源文件和目标文件
object Source = f.FileName;
object Target = f.FileName.Replace(".doc", ".rtf");
object Unknown = Type.Missing;
// 打开要转换的Word文件
newApp.Documents.Open2000(ref Source, ref Unknown,
ref Unknown, ref Unknown, ref Unknown,
ref Unknown, ref Unknown, ref Unknown,
ref Unknown, ref Unknown, ref Unknown,
ref Unknown);
// 指定文档的类型
object format = Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatRTF;
//改变文档类型
newApp.ActiveDocument.SaveAs2000(ref Target, ref format,
ref Unknown, ref Unknown, ref Unknown,
ref Unknown, ref Unknown, ref Unknown,
ref Unknown, ref Unknown, ref Unknown);
//关闭word实例
newApp.Quit(ref Unknown, ref Unknown, ref Unknown);
}
下面是将word文件转换成.rtf格式的C#代码,希望对你有帮助。
OpenFileDialog f = new OpenFileDialog();
if ((f.ShowDialog() == DialogResult.OK) && f.FileName != "")
{
//创建一个word的实例
Microsoft.Office.Interop.Word.Application newApp = new Microsoft.Office.Interop.Word.Application();
// 指定源文件和目标文件
object Source = f.FileName;
object Target = f.FileName.Replace(".doc", ".rtf");
object Unknown = Type.Missing;
// 打开要转换的Word文件
newApp.Documents.Open2000(ref Source, ref Unknown,
ref Unknown, ref Unknown, ref Unknown,
ref Unknown, ref Unknown, ref Unknown,
ref Unknown, ref Unknown, ref Unknown,
ref Unknown);
// 指定文档的类型
object format = Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatRTF;
//改变文档类型
newApp.ActiveDocument.SaveAs2000(ref Target, ref format,
ref Unknown, ref Unknown, ref Unknown,
ref Unknown, ref Unknown, ref Unknown,
ref Unknown, ref Unknown, ref Unknown);
//关闭word实例
newApp.Quit(ref Unknown, ref Unknown, ref Unknown);
}
展开全部
需要创建实例,以及文件需要转化
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
openFileDialog1.Filter = "文本文件(*.txt)|*.txt|RTF文件|*.rtf|所有文件(*.*)|*.*";
openFileDialog1.FilterIndex = 1;
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
try
{
string filename = openFileDialog1.FileName;
string fileext = new System.IO.FileInfo(filename).Extension;
switch (fileext.ToLower())
{
case ".txt":
this.richTextBox1.LoadFile(filename, RichTextBoxStreamType.PlainText);
break;
case ".rtf":
this.richTextBox1.LoadFile(filename, RichTextBoxStreamType.RichText);
break;
}
}
catch (Exception)
{
MessageBox.Show("Oh,您的打开不成功!");
}
}
openFileDialog1.FilterIndex = 1;
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
try
{
string filename = openFileDialog1.FileName;
string fileext = new System.IO.FileInfo(filename).Extension;
switch (fileext.ToLower())
{
case ".txt":
this.richTextBox1.LoadFile(filename, RichTextBoxStreamType.PlainText);
break;
case ".rtf":
this.richTextBox1.LoadFile(filename, RichTextBoxStreamType.RichText);
break;
}
}
catch (Exception)
{
MessageBox.Show("Oh,您的打开不成功!");
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询