Read word generated using a flow distortion 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 ();
At this time, MyString became garbled like this system.io.StreamReader mystreamreader = new system.io.streamReader (system.text.Encoding.default); it is also garbled, but you can correctly read the text file. Later, according to Word, it can be converted to HTML ideas, think of another approach, uploading Word to the server, converts to text, and then reads. Debugging the code as follows: // upload HttpPostedFile ContentFile = this.ContentFile.PostedFile; string ContentName = System.IO.Path.GetFileName (ContentFile.FileName); string ContentPath = Request.MapPath ( "/ web / uploadDocs /" ContentName); ContentFile.saves (ContentPath); string newcontentPath = system.io.path.changeextension (ContentPath, "TXT"); // Convert Word for Text
WordXpControl.WordControler UploadWord = new WordXpControl.WordControler (); UploadWord.Open (ContentPath); UploadWord.SaveAsExportTye (NewContentPath, WordXpControl.WordControler.ExportType.Text); UploadWord.Quit (); // reads the corresponding text file
System.IO.StreamReader myReader = new System.IO.StreamReader (NewContentPath, System.Text.Encoding.Default); strContent = myReader.ReadToEnd (); myReader.Close (); strContent = ManageProcess.GetFormatedString (strContent);
// Decide whether to delete the uploaded Word file and the corresponding text file IF (this.cbisdelete.checked); system.io.file.delete (NewContentPath);
}
The above WordXPControl is the assembly of the Word operation shortly, aspects of the Word operation. References to Microsoft Word 10 Object Library before use, and then reference the assembly. Enumerates the EXPORTTYPE enumeration that can determine the type of conversion, such as HTML. Some other operations can also be made. The assembly core code comes from the Internet, and many features have not been used. The above code is running on VS2003, Win2000, OfficeXP, which may have problems with other versions. Office2000 has successful examples on Mencius website. Note that the SYSTEM permission is specified to the ASP.NET process to avoid insufficient permissions.
Since it is not uploaded here, finally give the code for WordXPControl, after compiling: use system;
Namespace WordXPControl {///
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);}
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,} //, file public void SaveasexPortTye (String Strfilename), according to the incoming type (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; 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.Serection.WholeStory (); OWordApplic.Selection.delete (Ref Unit, ref count);}
Public void inserttext (string string) {OWordApplic.selection.typetext (strText);}
public void InsertLineBreak () {oWordApplic.Selection.TypeParagraph ();} public void InsertLineBreak (int nline) {for (int i = 0; i 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 strappy) {copy "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.i Talic = 0; OWordApplic.selection.font.subscript = 0;} Public void setfontname (string strapplick) {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 pbit);} 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 . WdpagenumbralAlignment.wdalignpagenumberright; break; case "left": alignment = word.wdpagenumberignment.wdalignpagenumberleft; owordapplic.selection.Headerfooter.pagenumBers.Add (r ef alignment, ref bFirstPage); break;}} // build Word.WdSaveFormat enumeration and the corresponding values of: The Word.WdSaveFormat.wdFormatHTML; ExportType corresponding to the HTML public enum ExportType {Document, Template, Text, TextLineBreaks, DOSTEXT, DOSTEXTLINEBREAKS, RTF, UnicodeText, HTML, WebArchive, FilteredHtml}}}