使用word的com组件解决读取时乱码的问题

19 篇文章 0 订阅

使用流读取word会产生乱码
HttpPostedFile myFile=this.ContentFile.PostedFile;
System.IO.Stream myStream=myFile.InputStream;
System.IO.StreamReader myStreamReader=
          new System.IO.StreamReader(myStream,System.Text.Encoding.GetEncoding("GB2312"));
string myString=myStreamReader.ReadToEnd();

此时myString就成乱码了
这样System.IO.StreamReader myStreamReader=
          new System.IO.StreamReader(myStream,System.Text.Encoding.Default);
也是乱码,但是可以正确读取text文件。
后来根据word可以转换为html的思路,想了另外一个办法,把word上传到服务器,转换为text,然后读取text。
    调试通过的 代码如下:
//上传文件
HttpPostedFile ContentFile=this.ContentFile.PostedFile;
string ContentName=System.IO.Path.GetFileName(ContentFile.FileName);
     string ContentPath=Request.MapPath("/web/uploadDocs/"+ContentName);
     ContentFile.SaveAs(ContentPath);
     string NewContentPath=System.IO.Path.ChangeExtension(ContentPath,"txt");

//转换word为text

     WordXpControl.WordControler UploadWord=new WordXpControl.WordControler();
     UploadWord.Open(ContentPath);
     UploadWord.SaveAsExportTye(NewContentPath,WordXpControl.WordControler.ExportType.Text);
     UploadWord.Quit();

//读取对应的text文件

                    System.IO.StreamReader myReader=new System.IO.StreamReader(NewContentPath,System.Text.Encoding.Default);
     strContent=myReader.ReadToEnd();
     myReader.Close();
     strContent=ManageProcess.GetFormatedString(strContent);

//根据需要决定是否删除上传的word文件和对应的text文件
     if(this.CbIsDelete.Checked)
     {
      System.IO.File.Delete(ContentPath);
      System.IO.File.Delete(NewContentPath);

     }



上面的WordXpControl是前不久封装的对word操作的程序集,方面对word的操作。使用前要引用Microsoft Word 10 Object Library,然后再引用该程序集即可。通过ExportType枚举可以决定转换的类型,如:html。也可进行一些其他操作。该程序集核心代码来自网上,很多功能本人也未尝使用。



以上代码都是在vs2003,win2000,officeXp上调试运行,对于其他版本可能有问题。office2000在孟子的网站上有成功例子。注意给asp.net进程指定system权限运行,以免出现权限不够的异常。



由于这里不能上传,最后给出WordXpControl的代码,编译以后即可使用:
using System;

 

namespace WordXpControl
{
 /// <summary>
 /// WordXPControl 的摘要说明。
 /// </summary>
 public class WordControler
 {
  public WordControler()
  {
   //
   // TODO: 在此处添加构造函数逻辑
   //
   oWordApplic = new Word.ApplicationClass();
  }

  private Word.ApplicationClass oWordApplic; // a reference to Word application
  private Word.Document oDoc;     // a reference to the document
  

  
  public void Open( string strFileName)
  {
   object fileName = strFileName;
   object;
   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);
  }  
 
  //根据传入的类型(ExportType枚举的值)保存文件
  public void SaveAsExportTye(string strFileName,ExportType exportType)
  {
   object missing = System.Reflection.Missing.Value;
   object fileName = strFileName;
   object Format = exportType;
   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);
     
  }

  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;
   }
           
  }
  
        //建立枚举和Word.WdSaveFormat中的值对应:如Word.WdSaveFormat.wdFormatHTML;对应于ExportType中的HTML
  public enum ExportType
  {
   Document,
   Template,
   Text,
   TextLineBreaks,
   DOSText,
   DOSTextLineBreaks,
   RTF,
   UnicodeText,
   HTML,
   WebArchive,
   FilteredHTML
  }

 }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值