从WORD文本生成相应的HTML网页

本文介绍了如何利用word.application对象读取Word文件内容,并转换为HTML格式,创建网页。目前作者正在探索处理Word中的复杂控件对象。此外,提到了一个向Word文件插入图片的示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 使用word.application对象从word文件中读取出文本信息,然后进行编码转换,再构造HTML网页并且存储。

操作word中其他控件对象相对麻烦一些,正在学习研究中。

 

         // 打开文件读取内容
        Microsoft.Office.Interop.Word.Application myWordApp  =   new  Microsoft.Office.Interop.Word.ApplicationClass();
        
int  num = myWordApp.Documents.Count;
        
object  path  =   @" D:RogerWithLogWORD est.doc " ;
        Document myWordDoc;
            
object  oMissing  =  System.Reflection.Missing.Value;
            
object  read  =   true ;
            myWordDoc 
=  myWordApp.Documents.Open( ref  path,  ref  oMissing,  ref  read,  ref  oMissing,  ref  oMissing,
   
ref  oMissing,  ref  oMissing,  ref  oMissing,  ref  oMissing,  ref  oMissing,  ref  oMissing,
   
ref  oMissing,  ref  oMissing,  ref  oMissing,  ref  oMissing,  ref  oMissing);
           
            
string  temp  =  myWordDoc.Content.Text;
            
this .Literal1.Text = temp;


            myWordDoc.Close(
ref  oMissing,  ref  oMissing,  ref  oMissing);
            myWordApp.Quit(
ref  oMissing,  ref  oMissing,  ref  oMissing);

            StreamWriter sw 
=   new  StreamWriter(FilePath);
            sw.Write(
" <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"  "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=gb2312"><title>无标题文档</title></head><body> " + myWordDoc.Content.Text.Trim().ToString() + " </body></html> " );                                    
            sw.Close();


            myWordDoc.Close(
ref  oMissing,  ref  oMissing,  ref  oMissing);
            myWordApp.Quit(
ref  oMissing,  ref  oMissing,  ref  oMissing); */


            
string  FilePath  =   @" D:RogerWithLogsubFileTest.html " ;
            StreamWriter sw 
=   new  StreamWriter(FilePath);


            
// 进行编码之间的转换
             string  unicodeString  =  temp;

            
//  Create two different encodings.
            Encoding ascii  =  Encoding.ASCII;
            Encoding unicode 
=  Encoding.Unicode;

            
//  Convert the string into a byte[].
             byte [] unicodeBytes  =  unicode.GetBytes(unicodeString);

            
//  Perform the conversion from one encoding to the other.
             byte [] asciiBytes  =  Encoding.Convert(unicode, ascii, unicodeBytes);

            
//  Convert the new byte[] into a char[] and then into a string.
            
//  This is a slightly different approach to converting to illustrate
            
//  the use of GetCharCount/GetChars.
             char [] asciiChars  =   new   char [ascii.GetCharCount(asciiBytes,  0 , asciiBytes.Length)];
            ascii.GetChars(asciiBytes, 
0 , asciiBytes.Length, asciiChars,  0 );
            
string  test  =   new   string (asciiChars);



            
// 构造HTML文件
            sw.Write( " <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"  "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=Unicode"> <title>无标题文档</title> </head> <body> "   +  test  +   " </body> </html> " );
            sw.Close();

            Server.Transfer(
@" ../sub/FileTest.html " );

 

 

补充一个往word文件中添加图片的示例:

 

         string  FileName  =   @" D:RogerWithLogWORDNewTest2.doc " ;
        
string  fileName0  =  FileName;
        
object  fileName1  =  FileName;
        
object  readOnly  =   false ;
        
object  isVisible  =   true ;

        
object  missing  =  System.Reflection.Missing.Value;

        Microsoft.Office.Interop.Word.ApplicationClass WordApp 
=   new  Microsoft.Office.Interop.Word.ApplicationClass();
        WordApp.Visible 
=   true ;
        WordApp.



        
// 打开word文档   
        Microsoft.Office.Interop.Word.Document aDoc  =  WordApp.Documents.Open( ref    fileName1,  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,  ref    missing);


        aDoc.Activate();

        
// 插入图片   


        
object  a  =   true ;
        
object  b  =   true ;
        
// 定义位置   
         object  left  =   50 object  top  =   650 ;
        
object  width  =   400 ;
        
object  height  =   450 ;

        aDoc.Shapes.AddPicture(
@" D:Rogerdesktops.jpg " ref    a,  ref    b,  ref    left,  ref    top,  ref      width,  ref      height,  ref    missing);
        aDoc.Save();
        aDoc.Close(
ref    missing,  ref    missing,  ref    missing);
        WordApp.Quit(
ref    a,  ref    missing,  ref    missing);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值