2010-01-16 121 views
2

如何使用VB.NET将HTML保存为ASP.NET中的Microsoft Word文档?我是否需要一个特殊的库来执行转换?从HTML导出到MS Word

回答

2

考虑到MS Word可以直接阅读HTML的事实,您可以简单地将Response.ContentType更改为application/ms-word

但是,如果您想要创建一个.doc文件,您可能需要查看Office Automation

其他方法是创建一个XML MS-Word文件,但这限制了您Word 2003和以上。

例如下面的XML是保存在word XML文件中的空文档。

<?xml version="1.0" encoding="utf-8" standalone="yes"?> 
<?mso-application progid="Word.Document"?> 
<w:wordDocument 
    xmlns:aml="http://schemas.microsoft.com/aml/2001/core" 
    xmlns:dt="uuid:C2F41010-65B3-11d1-A29F-00AA00C14882" 
    xmlns:ve="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    xmlns:o="urn:schemas-microsoft-com:office:office" 
    xmlns:v="urn:schemas-microsoft-com:vml" 
    xmlns:w10="urn:schemas-microsoft-com:office:word" 
    xmlns:w="http://schemas.microsoft.com/office/word/2003/wordml" 
    xmlns:wx="http://schemas.microsoft.com/office/word/2003/auxHint" 
    xmlns:wsp="http://schemas.microsoft.com/office/word/2003/wordml/sp2" 
    xmlns:sl="http://schemas.microsoft.com/schemaLibrary/2003/core" 
    w:macrosPresent="no" 
    w:embeddedObjPresent="no" 
    w:ocxPresent="no" 
    xml:space="preserve"> 
<w:ignoreSubtree w:val="http://schemas.microsoft.com/office/word/2003/wordml/sp2" /> 
<o:DocumentProperties> 
    <o:Author>Paulo Santos</o:Author> 
    <o:LastAuthor>Paulo Santos</o:LastAuthor> 
    <o:Revision>1</o:Revision> 
    <o:TotalTime>0</o:TotalTime> 
    <o:Created>2010-01-16T19:09:00Z</o:Created> 
    <o:LastSaved>2010-01-16T19:09:00Z</o:LastSaved> 
    <o:Pages>1</o:Pages> 
    <o:Words>0</o:Words> 
    <o:Characters>0</o:Characters> 
    <o:Company>PJ on Development</o:Company> 
    <o:Lines>1</o:Lines> 
    <o:Paragraphs>1</o:Paragraphs> 
    <o:CharactersWithSpaces>0</o:CharactersWithSpaces> 
    <o:Version>12</o:Version> 
</o:DocumentProperties> 
<w:fonts> 
    <w:defaultFonts w:ascii="Calibri" w:fareast="Calibri" 
    w:h-ansi="Calibri" w:cs="Times New Roman" /> 
    <w:font w:name="Times New Roman"> 
    <w:panose-1 w:val="02020603050405020304" /> 
    <w:charset w:val="00" /> 
    <w:family w:val="Roman" /> 
    <w:pitch w:val="variable" /> 
    <w:sig w:usb-0="E0002AFF" w:usb-1="C0007841" w:usb-2="00000009" 
    w:usb-3="00000000" w:csb-0="000001FF" w:csb-1="00000000" /> 
    </w:font> 
    <w:font w:name="Cambria Math"> 
    <w:panose-1 w:val="02040503050406030204" /> 
    <w:charset w:val="01" /> 
    <w:family w:val="Roman" /> 
    <w:notTrueType /> 
    <w:pitch w:val="variable" /> 
    <w:sig w:usb-0="00000000" w:usb-1="00000000" w:usb-2="00000000" 
    w:usb-3="00000000" w:csb-0="00000000" w:csb-1="00000000" /> 
    </w:font> 
    <w:font w:name="Calibri"> 
    <w:panose-1 w:val="020F0502020204030204" /> 
    <w:charset w:val="00" /> 
    <w:family w:val="Swiss" /> 
    <w:pitch w:val="variable" /> 
    <w:sig w:usb-0="E10002FF" w:usb-1="4000ACFF" w:usb-2="00000009" 
    w:usb-3="00000000" w:csb-0="0000019F" w:csb-1="00000000" /> 
    </w:font> 
</w:fonts> 
<w:styles> 
    <w:versionOfBuiltInStylenames w:val="7" /> 
    <w:latentStyles w:defLockedState="off" w:latentStyleCount="267"> 
    <w:lsdException w:name="Normal" /> 
    <w:lsdException w:name="heading 1" /> 
    <w:lsdException w:name="heading 2" /> 
    <w:lsdException w:name="heading 3" /> 
    <w:lsdException w:name="heading 4" /> 
    <w:lsdException w:name="heading 5" /> 
    <w:lsdException w:name="heading 6" /> 
    <w:lsdException w:name="heading 7" /> 
    <w:lsdException w:name="heading 8" /> 
    <w:lsdException w:name="heading 9" /> 
    <w:lsdException w:name="toc 1" /> 
    <w:lsdException w:name="toc 2" /> 
    <w:lsdException w:name="toc 3" /> 
    <w:lsdException w:name="toc 4" /> 
    <w:lsdException w:name="toc 5" /> 
    <w:lsdException w:name="toc 6" /> 
    <w:lsdException w:name="toc 7" /> 
    <w:lsdException w:name="toc 8" /> 
    <w:lsdException w:name="toc 9" /> 
    <w:lsdException w:name="caption" /> 
    <w:lsdException w:name="Title" /> 
    <w:lsdException w:name="Default Paragraph Font" /> 
    <w:lsdException w:name="Subtitle" /> 
    <w:lsdException w:name="Strong" /> 
    <w:lsdException w:name="Emphasis" /> 
    <w:lsdException w:name="Table Grid" /> 
    <w:lsdException w:name="Placeholder Text" /> 
    <w:lsdException w:name="No Spacing" /> 
    <w:lsdException w:name="Light Shading" /> 
    <w:lsdException w:name="Light List" /> 
    <w:lsdException w:name="Light Grid" /> 
    <w:lsdException w:name="Medium Shading 1" /> 
    <w:lsdException w:name="Medium Shading 2" /> 
    <w:lsdException w:name="Medium List 1" /> 
    <w:lsdException w:name="Medium List 2" /> 
    <w:lsdException w:name="Medium Grid 1" /> 
    <w:lsdException w:name="Medium Grid 2" /> 
    <w:lsdException w:name="Medium Grid 3" /> 
    <w:lsdException w:name="Dark List" /> 
    <w:lsdException w:name="Colorful Shading" /> 
    <w:lsdException w:name="Colorful List" /> 
    <w:lsdException w:name="Colorful Grid" /> 
    <w:lsdException w:name="Light Shading Accent 1" /> 
    <w:lsdException w:name="Light List Accent 1" /> 
    <w:lsdException w:name="Light Grid Accent 1" /> 
    <w:lsdException w:name="Medium Shading 1 Accent 1" /> 
    <w:lsdException w:name="Medium Shading 2 Accent 1" /> 
    <w:lsdException w:name="Medium List 1 Accent 1" /> 
    <w:lsdException w:name="Revision" /> 
    <w:lsdException w:name="List Paragraph" /> 
    <w:lsdException w:name="Quote" /> 
    <w:lsdException w:name="Intense Quote" /> 
    <w:lsdException w:name="Medium List 2 Accent 1" /> 
    <w:lsdException w:name="Medium Grid 1 Accent 1" /> 
    <w:lsdException w:name="Medium Grid 2 Accent 1" /> 
    <w:lsdException w:name="Medium Grid 3 Accent 1" /> 
    <w:lsdException w:name="Dark List Accent 1" /> 
    <w:lsdException w:name="Colorful Shading Accent 1" /> 
    <w:lsdException w:name="Colorful List Accent 1" /> 
    <w:lsdException w:name="Colorful Grid Accent 1" /> 
    <w:lsdException w:name="Light Shading Accent 2" /> 
    <w:lsdException w:name="Light List Accent 2" /> 
    <w:lsdException w:name="Light Grid Accent 2" /> 
    <w:lsdException w:name="Medium Shading 1 Accent 2" /> 
    <w:lsdException w:name="Medium Shading 2 Accent 2" /> 
    <w:lsdException w:name="Medium List 1 Accent 2" /> 
    <w:lsdException w:name="Medium List 2 Accent 2" /> 
    <w:lsdException w:name="Medium Grid 1 Accent 2" /> 
    <w:lsdException w:name="Medium Grid 2 Accent 2" /> 
    <w:lsdException w:name="Medium Grid 3 Accent 2" /> 
    <w:lsdException w:name="Dark List Accent 2" /> 
    <w:lsdException w:name="Colorful Shading Accent 2" /> 
    <w:lsdException w:name="Colorful List Accent 2" /> 
    <w:lsdException w:name="Colorful Grid Accent 2" /> 
    <w:lsdException w:name="Light Shading Accent 3" /> 
    <w:lsdException w:name="Light List Accent 3" /> 
    <w:lsdException w:name="Light Grid Accent 3" /> 
    <w:lsdException w:name="Medium Shading 1 Accent 3" /> 
    <w:lsdException w:name="Medium Shading 2 Accent 3" /> 
    <w:lsdException w:name="Medium List 1 Accent 3" /> 
    <w:lsdException w:name="Medium List 2 Accent 3" /> 
    <w:lsdException w:name="Medium Grid 1 Accent 3" /> 
    <w:lsdException w:name="Medium Grid 2 Accent 3" /> 
    <w:lsdException w:name="Medium Grid 3 Accent 3" /> 
    <w:lsdException w:name="Dark List Accent 3" /> 
    <w:lsdException w:name="Colorful Shading Accent 3" /> 
    <w:lsdException w:name="Colorful List Accent 3" /> 
    <w:lsdException w:name="Colorful Grid Accent 3" /> 
    <w:lsdException w:name="Light Shading Accent 4" /> 
    <w:lsdException w:name="Light List Accent 4" /> 
    <w:lsdException w:name="Light Grid Accent 4" /> 
    <w:lsdException w:name="Medium Shading 1 Accent 4" /> 
    <w:lsdException w:name="Medium Shading 2 Accent 4" /> 
    <w:lsdException w:name="Medium List 1 Accent 4" /> 
    <w:lsdException w:name="Medium List 2 Accent 4" /> 
    <w:lsdException w:name="Medium Grid 1 Accent 4" /> 
    <w:lsdException w:name="Medium Grid 2 Accent 4" /> 
    <w:lsdException w:name="Medium Grid 3 Accent 4" /> 
    <w:lsdException w:name="Dark List Accent 4" /> 
    <w:lsdException w:name="Colorful Shading Accent 4" /> 
    <w:lsdException w:name="Colorful List Accent 4" /> 
    <w:lsdException w:name="Colorful Grid Accent 4" /> 
    <w:lsdException w:name="Light Shading Accent 5" /> 
    <w:lsdException w:name="Light List Accent 5" /> 
    <w:lsdException w:name="Light Grid Accent 5" /> 
    <w:lsdException w:name="Medium Shading 1 Accent 5" /> 
    <w:lsdException w:name="Medium Shading 2 Accent 5" /> 
    <w:lsdException w:name="Medium List 1 Accent 5" /> 
    <w:lsdException w:name="Medium List 2 Accent 5" /> 
    <w:lsdException w:name="Medium Grid 1 Accent 5" /> 
    <w:lsdException w:name="Medium Grid 2 Accent 5" /> 
    <w:lsdException w:name="Medium Grid 3 Accent 5" /> 
    <w:lsdException w:name="Dark List Accent 5" /> 
    <w:lsdException w:name="Colorful Shading Accent 5" /> 
    <w:lsdException w:name="Colorful List Accent 5" /> 
    <w:lsdException w:name="Colorful Grid Accent 5" /> 
    <w:lsdException w:name="Light Shading Accent 6" /> 
    <w:lsdException w:name="Light List Accent 6" /> 
    <w:lsdException w:name="Light Grid Accent 6" /> 
    <w:lsdException w:name="Medium Shading 1 Accent 6" /> 
    <w:lsdException w:name="Medium Shading 2 Accent 6" /> 
    <w:lsdException w:name="Medium List 1 Accent 6" /> 
    <w:lsdException w:name="Medium List 2 Accent 6" /> 
    <w:lsdException w:name="Medium Grid 1 Accent 6" /> 
    <w:lsdException w:name="Medium Grid 2 Accent 6" /> 
    <w:lsdException w:name="Medium Grid 3 Accent 6" /> 
    <w:lsdException w:name="Dark List Accent 6" /> 
    <w:lsdException w:name="Colorful Shading Accent 6" /> 
    <w:lsdException w:name="Colorful List Accent 6" /> 
    <w:lsdException w:name="Colorful Grid Accent 6" /> 
    <w:lsdException w:name="Subtle Emphasis" /> 
    <w:lsdException w:name="Intense Emphasis" /> 
    <w:lsdException w:name="Subtle Reference" /> 
    <w:lsdException w:name="Intense Reference" /> 
    <w:lsdException w:name="Book Title" /> 
    <w:lsdException w:name="Bibliography" /> 
    <w:lsdException w:name="TOC Heading" /> 
    </w:latentStyles> 
    <w:style w:type="paragraph" w:default="on" w:styleId="Normal"> 
    <w:name w:val="Normal" /> 
    <w:rsid w:val="001F679D" /> 
    <w:pPr> 
     <w:spacing w:after="200" w:line="276" w:line-rule="auto" /> 
    </w:pPr> 
    <w:rPr> 
     <wx:font wx:val="Calibri" /> 
     <w:sz w:val="22" /> 
     <w:sz-cs w:val="22" /> 
     <w:lang w:val="EN-US" w:fareast="EN-US" w:bidi="AR-SA" /> 
    </w:rPr> 
    </w:style> 
    <w:style w:type="character" w:default="on" 
    w:styleId="DefaultParagraphFont"> 
    <w:name w:val="Default Paragraph Font" /> 
    </w:style> 
    <w:style w:type="table" w:default="on" w:styleId="TableNormal"> 
    <w:name w:val="Normal Table" /> 
    <wx:uiName wx:val="Table Normal" /> 
    <w:rPr> 
     <wx:font wx:val="Calibri" /> 
     <w:lang w:val="EN-US" w:fareast="EN-US" w:bidi="AR-SA" /> 
    </w:rPr> 
    <w:tblPr> 
     <w:tblInd w:w="0" w:type="dxa" /> 
     <w:tblCellMar> 
     <w:top w:w="0" w:type="dxa" /> 
     <w:left w:w="108" w:type="dxa" /> 
     <w:bottom w:w="0" w:type="dxa" /> 
     <w:right w:w="108" w:type="dxa" /> 
     </w:tblCellMar> 
    </w:tblPr> 
    </w:style> 
    <w:style w:type="list" w:default="on" w:styleId="NoList"> 
    <w:name w:val="No List" /> 
    </w:style> 
</w:styles> 
<w:shapeDefaults> 
    <o:shapedefaults v:ext="edit" spidmax="2050" /> 
    <o:shapelayout v:ext="edit"> 
    <o:idmap v:ext="edit" data="1" /> 
    </o:shapelayout> 
</w:shapeDefaults> 
<w:docPr> 
    <w:view w:val="print" /> 
    <w:zoom w:percent="100" /> 
    <w:doNotEmbedSystemFonts /> 
    <w:defaultTabStop w:val="720" /> 
    <w:punctuationKerning /> 
    <w:characterSpacingControl w:val="DontCompress" /> 
    <w:optimizeForBrowser /> 
    <w:validateAgainstSchema /> 
    <w:saveInvalidXML w:val="off" /> 
    <w:ignoreMixedContent w:val="off" /> 
    <w:alwaysShowPlaceholderText w:val="off" /> 
    <w:compat> 
    <w:breakWrappedTables /> 
    <w:snapToGridInCell /> 
    <w:wrapTextWithPunct /> 
    <w:useAsianBreakRules /> 
    <w:dontGrowAutofit /> 
    </w:compat> 
    <wsp:rsids> 
    <wsp:rsidRoot wsp:val="0063767C" /> 
    <wsp:rsid wsp:val="001F679D" /> 
    <wsp:rsid wsp:val="0063767C" /> 
    </wsp:rsids> 
</w:docPr> 
<w:body> 
    <w:p wsp:rsidR="001F679D" wsp:rsidRDefault="001F679D" /> 
    <w:sectPr wsp:rsidR="001F679D" wsp:rsidSect="001F679D"> 
    <w:pgSz w:w="12240" w:h="15840" /> 
    <w:pgMar w:top="1440" w:right="1440" w:bottom="1440" 
    w:left="1440" w:header="720" w:footer="720" w:gutter="0" /> 
    <w:cols w:space="720" /> 
    <w:docGrid w:line-pitch="360" /> 
    </w:sectPr> 
</w:body> 
</w:wordDocument> 

您可能不需要上面看到的所有项目,但它是可行的。

1

您可以将页眉中的内容类型更改为application/vnd.ms-word,这应该可以做到这一点,这当然是最简单的方法,我相信还有其他的。

0

您可以使用Office自动化或尝试自行生成本机文件格式。不过,我可以推荐使用专门的库来完成这项工作。我自己使用了一个库来导出Excel,并且很高兴有一个合适的.NET对象模型来操作,而不需要知道本机文件结构。节省大量的时间和工作。 Aspose可能会为您提供正确的组件或库。