2011-11-15 80 views
2

我想將.docx文件轉換爲.html。我在C#工作。我的代碼是這樣的:將.docx轉換爲html

Microsoft.Office.Interop.Word.Application word = new Microsoft.Office.Interop.Word.Application(); 
     Microsoft.Office.Interop.Word.Document wordDoc = new Microsoft.Office.Interop.Word.Document(); 
     Object oMissing = System.Reflection.Missing.Value; 
     wordDoc = word.Documents.Add(ref oMissing, ref oMissing, ref oMissing, ref oMissing); 
     word.Visible = false; 
     Object filepath = @"C:\Users\John\Desktop\begin.docx"; 
     Object confirmconversion = System.Reflection.Missing.Value; 
     Object readOnly = false; 
     Object saveto = @"C:\Users\John\Desktop\result.html"; 
     Object oallowsubstitution = System.Reflection.Missing.Value; 

     wordDoc = word.Documents.Open(ref filepath, ref confirmconversion, ref readOnly, ref oMissing, 
             ref oMissing, ref oMissing, ref oMissing, ref oMissing, 
             ref oMissing, ref oMissing, ref oMissing, ref oMissing, 
             ref oMissing, ref oMissing, ref oMissing); 
     object fileFormat = WdSaveFormat.wdFormatHTML; 
     wordDoc.SaveAs(ref saveto, ref fileFormat, ref oMissing, ref oMissing, ref oMissing, 
         ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, 
         ref oMissing, ref oMissing, ref oMissing, ref oallowsubstitution, ref oMissing, 
         ref oMissing); 

問題是,這不包括頁眉和頁腳。它們不在.html結果文件中。我如何將它們包含在結果中?

+2

你有沒有試過手動操作?我的意思是從Word保存到HTML。它是否包含頁眉和頁腳? – Snowbear

+0

是的,我試過了,但沒有。 –

+0

是一個(商業)圖書館的選擇嗎? – Yahia

回答

2

您在Word中看到頁眉和頁腳的原因是因爲您基本上處於「打印」視圖。在HTML文檔中,您處於「草稿」樣式視圖,其中不存在頁眉和頁腳。您可以爲HTML文檔設置不同的樣式,以便在打印時稱爲print stylesheet。此打印樣式表僅用於在瀏覽器中打印文檔時使用。

另一種選擇是將其轉換爲PDF並允許用戶查看PDF,因爲大多數瀏覽器現在都支持PDF查看或具有支持PDF的插件。

您還可以將頁眉和頁腳作爲元素添加到html文件中,然後使用一些CSS技巧使元素顯示在頂部和底部。 Here is a link描述如何做到這一點。