2010-09-13 53 views

回答

1

嘗試使用Office互操作

private void _convetHTML2Doc(string FileNameUpload) 
{ 
    string filePath = Server.MapPath("~/htmlfile"); 
    object missing = Type.Missing; 
    object FileName = @"D:\" + "\\" + FileNameUpload; 
    object readOnly = true; 
    m_word = new Application(); 

    m_word.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 missing, ref missing, ref missing, ref missing, ref missing); 
    string newfilename = filePath + "\\" + FileNameUpload.Replace(".html", ".doc"); 
    object o_newfilename = newfilename; 

    object o_encoding = Microsoft.Office.Core.MsoEncoding.msoEncodingUTF8; 

    object o_format = Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatDocument; 

    object o_endings = Microsoft.Office.Interop.Word.WdLineEndingType.wdCRLF; 
    m_word.ActiveDocument.SaveAs(ref o_newfilename, ref o_format, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, 
            ref missing, ref missing, ref o_encoding, ref missing, ref missing, ref o_endings, ref missing); 
    m_word.Quit(ref missing, ref missing, ref missing); 
    System.Runtime.InteropServices.Marshal.ReleaseComObject(m_word); 
    } 
+3

在服務器上自動運行Office是一個_bad_想法。 – SLaks 2010-09-13 17:29:11

+0

@SLaks:我不認爲OP說他們在服務器上這麼做... – 2010-09-13 20:52:30

+0

@Otaku:答案是。 ('Server.MapPath') – SLaks 2010-09-13 23:18:14

1

MS警告不要當它沒有被觀看/監督/由最終用戶控制,例如自動運行Word因爲它可能會彈出一個消息框;所以如果你想在服務器上這樣做,那麼第三方組件可能比自動化Word更好。你也可以自己學習編寫Word文檔格式(這是一種記錄的XML格式),但是(學習和編寫該格式)可能比你想要的更麻煩。

此外,請注意,Word可以打開HTML:因此對於某些(可能略有)範圍,HTML已經是一個Word文檔。