I want Convert a Html page with all its content i.e. images like .png,.gif to ms word document with the same look and feel
Can any one help me to convert a html page and its content to Microsoft Word file . Basically what i want is a ms word page which looks like my html page in Browser.
i am using this code can any one suggest me something else.
object filename1 = @"html file path";
object oMissing = System.Reflection.Missing.Value;
object readOnly = false;
object oFalse = false;
Microsoft.Office.Interop.Word.Application oWord = new Microsoft.Office.Interop.Word.Application();
Microsoft.Office.Interop.Word.Document oDoc = new Microsoft.Office.Interop.Word.Document();
oDoc = oWord.Documents.Add(ref oMissing, ref oMissing, ref oMissing, ref oMissing);
oWord.Visible = false;
oDoc = oWord.Documents.Open(ref filename1, ref oMissing, 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, ref oMissing);
if (!Directory.Exists(@"D:\FileConverter\Temp\new.doc"))//path of destination file.
{
Directory.CreateDirectory(@"D:\FileConverter\Temp");
}
if (!File.Exists(@"D:\FileConverter\Temp\new.doc"))
{
File.Create(@"D:\FileConverter\Temp\new.doc");
}
filename1 = @"D:\FileConverter\Temp\new.doc";
object fileFormat = Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatDocument;
oDoc.SaveAs(ref filename1, ref fileFormat, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref o开发者_如何学编程Missing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
oDoc.Close(ref oFalse, ref oMissing, ref oMissing);
oWord.Quit(ref oMissing, ref oMissing, ref oMissing);
here is the Code which any one can use to convert the html page and get the Images also..
const string filename = "C:/Temp/test.docx";
Response.ContentEncoding = System.Text.Encoding.UTF7;
System.Text.StringBuilder SB = new System.Text.StringBuilder();
System.IO.StringWriter SW = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlTW = new System.Web.UI.HtmlTextWriter(SW);
tbl.RenderControl(htmlTW);
string strBody = "<html>" +
"<body>" + "<div><b>" + htmlTW.InnerWriter.ToString() + "</b></div>" +
"</body>" +
"</html>";
string html = strBody;
if (File.Exists(filename)) File.Delete(filename);
using (MemoryStream generatedDocument = new MemoryStream())
{
using (WordprocessingDocument package = WordprocessingDocument.Create(generatedDocument, WordprocessingDocumentType.Document))
{
MainDocumentPart mainPart = package.MainDocumentPart;
if (mainPart == null)
{
mainPart = package.AddMainDocumentPart();
new Document(new Body()).Save(mainPart);
}
HtmlConverter converter = new HtmlConverter(mainPart);
converter.BaseImageUrl = new Uri("http://localhost:portnumber/");
Body body = mainPart.Document.Body;
var paragraphs = converter.Parse(html);
for (int i = 0; i < paragraphs.Count; i++)
{
body.Append(paragraphs[i]);
}
mainPart.Document.Save();
}
File.WriteAllBytes(filename, generatedDocument.ToArray());
}
System.Diagnostics.Process.Start(filename);
TO get the .dll files ..use Link You can download notesfor.dll from here.
It may be the name HTMLtoOpenXML.
Not sure if this fits your problem domain, but...
You could just render your html as usual, but change the response content type to application/msword
and make sure the filename ends with .doc
. That'll prompt your browser to download the file, and prompt your OS to open it as a word document. In my experience, MS Word does a decent job of converting other formats to something that looks like a word document.
You might end up getting some popups in Word that would look annoying, but if that's not an issue, this would be a good bang-for-your-buck solution.
精彩评论