Html to Word Blog has a lot of articles, I put myself in the project used, relatively simple to write, easy to use later
I use C # MVC
1. Create a view page in the project,
Page content
<! DOCTYPE html>"viewport " content=" width=device-width" /> <title>TestHtmlToWord</title>
Controller creation
Public voidhtmltowordtest () {stringWordcontent =string. Empty; IView View= ViewEngines.Engines.FindPartialView (ControllerContext,"Testhtmltoworld"). View; using(varwriter =NewStringWriter ()) { varViewContext =NewViewContext (ControllerContext, view, ViewData, TempData, writer); ViewContext.View.Render (ViewContext, writer); Wordcontent=writer. ToString (); Writer. Close (); Writer. Dispose (); } stringName =string. Empty; stringuseragent = request.servervariables["http_user_agent"]. ToLower (); Response.appendheader ("content-disposition","Attachment;filename=testhtmltoworld.doc"); Response.ContentType="Application/ms-word"; Response.Charset="Utf-8"; Response.ContentEncoding= System.Text.Encoding.GetEncoding ("Utf-8"); Response.Write (wordcontent); Response.End (); }
After downloading, the default open is Web Word
But what we actually want is
So you also need to add a page
<!--[ifGTE MSO9]><xml><w:worddocument><w:view>print</w:view><w:trackmoves>false</w:TrackMoves><w:TrackFormatting/><w:ValidateAgainstSchemas/><w:SaveIfXMLInvalid>false</w:SaveIfXMLInvalid><w:IgnoreMixedContent>false</w:IgnoreMixedContent><w:AlwaysShowPlaceholderText>false</w:alwaysshowplaceholdertext><w:donotpromoteqf/><w:lidthemeother>en-us</w:lidthemeother ><W:LIDTHEMEASIAN>ZH-CN</W:LIDTHEMEASIAN><W:LIDTHEMECOMPLEXSCRIPT>X-NONE</W: Lidthemecomplexscript><w:compatibility><w:breakwrappedtables/><w:snaptogridincell/><w: wraptextwithpunct/><w:useasianbreakrules/><w:dontgrowautofit/><w:splitpgbreakandparamark/ ><w:dontvertaligncellwithsp/><w:dontbreakconstrainedforcedtables/><w:dontvertalignintxbx/ ><w:Word11KerningPairs/><w:CachedColBalance/><w:UseFELayout/></w:Compatibility>< W:browserlevel>microsoftinternetexplorer4</w:browserlevel><m:mathpr><m:mathfont m:val="Cambria Math"/><m:brkbin m:val="before"/><m:brkbinsub m:val="--"/><m:smallfrac m:val="off"/><m:dispdef/><m:lmargin m:val="0"/> <m:rmargin m:val="0"/><M:DEFJC m:val="Centergroup"/><m:wrapindent m:val="1440"/><m:intlim m:val="Subsup"/><m:narylim m:val="UNDOVR"/></m:mathpr></w:worddocument></xml><! [endif]-->
Final page
<! DOCTYPE html>"urn:schemas-microsoft-com:vml"xmlns:o="Urn:schemas-microsoft-com:office:office"xmlns:w="Urn:schemas-microsoft-com:office:word"xmlns:m="HTTP://SCHEMAS.MICROSOFT.COM/OFFICE/2004/12/OMML"xmlns="HTTP://WWW.W3.ORG/TR/REC-HTML40">ifGTE MSO9]><xml><w:worddocument><w:view>print</w:view><w:trackmoves>false</w:TrackMoves><w:TrackFormatting/><w:ValidateAgainstSchemas/><w:SaveIfXMLInvalid>false</w:SaveIfXMLInvalid><w:IgnoreMixedContent>false</w:IgnoreMixedContent><w:AlwaysShowPlaceholderText>false</w:alwaysshowplaceholdertext><w:donotpromoteqf/><w:lidthemeother>en-us</w:lidthemeother ><W:LIDTHEMEASIAN>ZH-CN</W:LIDTHEMEASIAN><W:LIDTHEMECOMPLEXSCRIPT>X-NONE</W: Lidthemecomplexscript><w:compatibility><w:breakwrappedtables/><w:snaptogridincell/><w: wraptextwithpunct/><w:useasianbreakrules/><w:dontgrowautofit/><w:splitpgbreakandparamark/ ><w:dontvertaligncellwithsp/><w:dontbreakconstrainedforcedtables/><w:dontvertalignintxbx/ ><w:Word11KerningPairs/><w:CachedColBalance/><w:UseFELayout/></w:Compatibility>< W:browserlevel>microsoftinternetexplorer4</w:browserlevel><m:mathpr><m:mathfont m:val="Cambria Math"/><m:brkbin m:val="before"/><m:brkbinsub m:val="--"/><m:smallfrac m:val="off"/><m:dispdef/><m:lmargin m:val="0"/> <m:rmargin m:val="0"/><M:DEFJC m:val="Centergroup"/><m:wrapindent m:val="1440"/><m:intlim m:val="Subsup"/><m:narylim m:val="UNDOVR"/></m:mathpr></w:worddocument></xml><! [endif]--> <meta name="Viewport"Content="Width=device-width"/> <title>TestHtmlToWorld</title>Later there may be other ways to improve ...
Html to Word (i)