从WORD文本生成相应的HTML网页 - Go语言中文社区

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


 使用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 = 50object 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);
版权声明:本文来源CSDN,感谢博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/laixingrong/article/details/2064570
站方申明:本站部分内容来自社区用户分享,若涉及侵权,请联系站方删除。
  • 发表于 2020-02-25 01:22:44
  • 阅读 ( 864 )
  • 分类:前端

0 条评论

请先 登录 后评论

官方社群

GO教程

推荐文章

猜你喜欢