您的位置:首页 > 移动开发 > Objective-C

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

2004-10-27 21:45 375 查看
使用流读取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
}

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