如何讀取Doc類型的文件

jastion 2003-08-20 10:20:17
下面的我寫的讀取的txt類型的文件,但是讀不出Doc類型的文件

private void ReadTxt()
{
FileStream fs=new FileStream(@"C;/test.txt",FileMode.Open,FileAccess.Read);
StreamReader m_streamreader = new StreamReader(fs,Encoding.Default);
m_streamreader.BaseStream.Seek(0, SeekOrigin.Begin);
string strLine = m_streamreader.ReadLine();
string strView="";
while (strLine != null)
{
strLine = m_streamreader.ReadLine();
strView += strLine;
}
m_streamreader.Close();
richTextBox1.Text=strView;

}
請教各位大蝦
...全文
65 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
chang110cn 2003-08-20
  • 打赏
  • 举报
回复
public void CopyAll()
{
oWordApplic.Selection.WholeStory();
oWordApplic.Selection.Copy();

}
public void PasetAll()
{

oWordApplic.Selection.PasteAndFormat(Word.WdRecoveryType.wdPasteDefault);


}
public void Clear()
{

object Unit=(int)Word.WdUnits.wdCharacter;
object Count=1;
oWordApplic.Selection.WholeStory();
oWordApplic.Selection.Delete(ref Unit,ref Count);
}



public void InsertText( string strText)
{
oWordApplic.Selection.TypeText(strText);
}

public void InsertLineBreak( )
{
oWordApplic.Selection.TypeParagraph();
}
public void InsertLineBreak( int nline)
{
for (int i=0; i<nline; i++)
oWordApplic.Selection.TypeParagraph();
}


public void SetAlignment(string strType )
{
switch (strType)
{
case "Center" :
oWordApplic.Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;
break;
case "Left" :
oWordApplic.Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphLeft;
break;
case "Right" :
oWordApplic.Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphRight;
break;
case "Justify" :
oWordApplic.Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphJustify;
break;
}

}



public void SetFont( string strType )
{
switch (strType)
{
case "Bold":
oWordApplic.Selection.Font.Bold = 1;
break;
case "Italic":
oWordApplic.Selection.Font.Italic = 1;
break;
case "Underlined":
oWordApplic.Selection.Font.Subscript = 0;
break;
}

}


public void SetFont( )
{
oWordApplic.Selection.Font.Bold = 0;
oWordApplic.Selection.Font.Italic = 0;
oWordApplic.Selection.Font.Subscript = 0;

}

public void SetFontName( string strType )
{
oWordApplic.Selection.Font.Name = strType;

}

public void SetFontSize( int nSize )
{
oWordApplic.Selection.Font.Size = nSize;

}

public void InsertPagebreak()
{

object pBreak= (int)Word.WdBreakType.wdPageBreak;
oWordApplic.Selection.InsertBreak(ref pBreak );
}



public void GotoBookMark( string strBookMarkName)
{

object missing = System.Reflection.Missing.Value;

object Bookmark = (int)Word.WdGoToItem.wdGoToBookmark;
object NameBookMark = strBookMarkName;
oWordApplic.Selection.GoTo(ref Bookmark, ref missing, ref missing,ref NameBookMark);
}

public void GoToTheEnd( )
{

object missing = System.Reflection.Missing.Value;
object unit ;
unit = Word.WdUnits.wdStory ;
oWordApplic.Selection.EndKey ( ref unit, ref missing);

}
public void GoToTheBeginning( )
{

object missing = System.Reflection.Missing.Value;
object unit ;
unit = Word.WdUnits.wdStory ;
oWordApplic.Selection.HomeKey ( ref unit, ref missing);

}

public void GoToTheTable(int ntable )
{

object missing = System.Reflection.Missing.Value;
object what;
what = Word.WdUnits.wdTable ;
object which;
which = Word.WdGoToDirection.wdGoToFirst;
object count;
count = 1 ;
oWordApplic.Selection.GoTo( ref what, ref which, ref count, ref missing);
oWordApplic.Selection.Find.ClearFormatting();

oWordApplic.Selection.Text = "";


}

public void GoToRightCell( )
{

object missing = System.Reflection.Missing.Value;
object direction;
direction = Word.WdUnits.wdCell;
oWordApplic.Selection.MoveRight(ref direction,ref missing,ref missing);
}

public void GoToLeftCell( )
{

object missing = System.Reflection.Missing.Value;
object direction;
direction = Word.WdUnits.wdCell;
oWordApplic.Selection.MoveLeft(ref direction,ref missing,ref missing);
}

public void GoToDownCell( )
{

object missing = System.Reflection.Missing.Value;
object direction;
direction = Word.WdUnits.wdLine;
oWordApplic.Selection.MoveDown(ref direction,ref missing,ref missing);
}

public void GoToUpCell( )
{

object missing = System.Reflection.Missing.Value;
object direction;
direction = Word.WdUnits.wdLine;
oWordApplic.Selection.MoveUp(ref direction,ref missing,ref missing);
}
public void InsertPageNumber( string strType, bool bHeader )
{
object missing = System.Reflection.Missing.Value;
object alignment ;
object bFirstPage = false;
object bF = true;
switch (strType)
{
case "Center":
alignment = Word.WdPageNumberAlignment.wdAlignPageNumberCenter;
oWordApplic.Selection.HeaderFooter.PageNumbers.Item(1).Alignment = Word.WdPageNumberAlignment.wdAlignPageNumberCenter;
break;
case "Right":
alignment = Word.WdPageNumberAlignment.wdAlignPageNumberRight;
oWordApplic.Selection.HeaderFooter.PageNumbers.Item(1).Alignment = Word.WdPageNumberAlignment.wdAlignPageNumberRight;
break;
case "Left":
alignment = Word.WdPageNumberAlignment.wdAlignPageNumberLeft;
oWordApplic.Selection.HeaderFooter.PageNumbers.Add(ref alignment,ref bFirstPage);
break;
}

}


}
}
chang110cn 2003-08-20
  • 打赏
  • 举报
回复
别人写的,自己看看吧。
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;


namespace WordApplication
{
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;
private System.Windows.Forms.RichTextBox richTextBox1;
private System.Windows.Forms.OpenFileDialog openFileDialog1;
private System.Windows.Forms.Button button2;
private System.ComponentModel.Container components = null;

public Form1()
{
InitializeComponent();
}

protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.richTextBox1 = new System.Windows.Forms.RichTextBox();
this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
this.button2 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(72, 296);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(120, 32);
this.button1.TabIndex = 0;
this.button1.Text = "开始读取";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// richTextBox1
//
this.richTextBox1.Location = new System.Drawing.Point(16, 16);
this.richTextBox1.Name = "richTextBox1";
this.richTextBox1.Size = new System.Drawing.Size(432, 264);
this.richTextBox1.TabIndex = 1;
this.richTextBox1.Text = "";
//
// openFileDialog1
//
this.openFileDialog1.DefaultExt = "*.doc";
this.openFileDialog1.Filter = "Word文件|*.doc";
//
// button2
//
this.button2.Location = new System.Drawing.Point(248, 296);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(128, 32);
this.button2.TabIndex = 2;
this.button2.Text = "修改后保存";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(496, 365);
this.Controls.Add(this.button2);
this.Controls.Add(this.richTextBox1);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "这是一个用于读取WORD文件到RICHEDIT的例子";
this.ResumeLayout(false);

}
#endregion

[STAThread]
static void Main()
{
Application.Run(new Form1());
}

private void button1_Click(object sender, System.EventArgs e)
{
richTextBox1.Clear();
openFileDialog1.ShowDialog();
if(openFileDialog1.FileName!="")
{
CCWordApp test;
test = new CCWordApp();
test.Open (openFileDialog1.FileName);
test.CopyAll();
richTextBox1.Paste();
test.Quit();
}

}

private void button2_Click(object sender, System.EventArgs e)
{

richTextBox1.SelectAll();
richTextBox1.Copy();

CCWordApp test ;

test = new CCWordApp();

//上面代码正常 

test.Open (openFileDialog1.FileName);
test.Clear();
test.PasetAll();

test.Save();
test.Quit();


}
}

public class CCWordApp
{
private Word.ApplicationClass oWordApplic; // a reference to Word application
private Word.Document oDoc; // a reference to the document


public CCWordApp()
{

oWordApplic = new Word.ApplicationClass();
}


public void Open( string strFileName)
{
object fileName = strFileName;
object readOnly = false;
object isVisible = true;
object missing = System.Reflection.Missing.Value;


oDoc = oWordApplic.Documents.Open(ref fileName, ref missing,ref readOnly,
ref missing, ref missing, ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref isVisible,ref missing,ref missing,ref missing);

oDoc.Activate();
}



public void Open( )
{
object missing = System.Reflection.Missing.Value;
oDoc = oWordApplic.Documents.Add(ref missing, ref missing,ref missing, ref missing);

oDoc.Activate();
}


public void Quit( )
{
object missing = System.Reflection.Missing.Value;
oWordApplic.Application.Quit(ref missing, ref missing, ref missing);
}

public void Save( )
{
oDoc.Save();
}

public void SaveAs(string strFileName )
{
object missing = System.Reflection.Missing.Value;
object fileName = strFileName;

oDoc.SaveAs(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);
}


public void SaveAsHtml(string strFileName )
{
object missing = System.Reflection.Missing.Value;
object fileName = strFileName;
object Format = (int)Word.WdSaveFormat.wdFormatHTML;
oDoc.SaveAs(ref fileName, ref Format,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);
oDoc.Close(ref missing ,ref missing,ref missing);

}

jiezhi 2003-08-20
  • 打赏
  • 举报
回复
希望有幫助:
http://www.microsoft.com/china/msdn/library/dnexcl2k2/html/odc_offcs.asp

110,499

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧