My teammate was given a task to edit html markup content stored in a database and to generate a word document from the edited html content. I used "DocumentFormat.OpenXml" to generate a word document from the html content. Happy to solve my teammate's problem :).
The below code use a template word document. Using the template, a new document is generated on server. The file can be deleted from the server once client has downloaded.
Add two dlls DocumentFormat.OpenXml.dll and WindowsBase.dll in your web application/ website.
Add references "DocumentFormat.OpenXml.Packaging" and "DocumentFormat.OpenXml.Wordprocessing" in your code.
Code:
//It will generate Word 2007 document for the html content.
public static void GenerateWordDocFromHTMLContent(string templateFilePath, string documentFilePath, DataTable data, string fileName)
{
File.Copy(templateFilePath, documentFilePath, true);
using (WordprocessingDocument wordDoc = WordprocessingDocument.Open(documentFilePath, true))
{
MainDocumentPart mainPart = wordDoc.MainDocumentPart;
int altChunkIdCounter = 1;
int blockLevelCounter = 1;
//Fetch the html content from DataTable. Let us take first record.
string htmlContent = data.Rows[0]["HTMLMarkUp"].ToString();
string html = "<html><body>" + htmlContent + "</html></body>";
string altChunkId = String.Format("AltChunkId{0}", altChunkIdCounter++);
//Import data as html content using Altchunk
AlternativeFormatImportPart chunk = mainPart.AddAlternativeFormatImportPart(AlternativeFormatImportPartType.Xhtml, altChunkId);
using (Stream chunkStream = chunk.GetStream(FileMode.Create, FileAccess.Write))
using (StreamWriter stringStream = new StreamWriter(chunkStream, Encoding.UTF8)) //Encoding.UTF8 is important to remove special characters
stringStream.Write(html);
AltChunk altChunk = new AltChunk();
altChunk.Id = altChunkId;
mainPart.Document.Body.InsertAt(altChunk, blockLevelCounter++);
mainPart.Document.Save();
}
}
Note: The content type or Mime type for word 2007 file is "application/vnd.openxmlformats-officedocument.wordprocessingml.document"
References:
http://msdn.microsoft.com/en-us/library/ee956524%28office.14%29.aspx
http://msdn.microsoft.com/en-us/library/dd469465.aspx
Thanks for sharing the code man.Today I was also given an assignment of converting html content to a MS Word file.I always worked on the viceversa but your code really saved me a lot of time and efforts.
ReplyDeletedigital signature FAQ
how ca n we convert word file to html
DeleteCan i have complete conversion code plz....
ReplyDeletefitflop footwear
ReplyDeletecoach factory outlet
wholesale jordans
abercrombie & fitch
north face sale
fitflop outlet
under armour outlet
ferragamo shoes
abercrombie kids
coach online outlet
north face clearance
nike outlet
nike online
dior outlet store
gucci shoes
michael kors outlet
gucci sunglasses outlet
gucci shoes for men
dior outlet online
michael kors bags sale
tods outlet
coach purses clearance
michael kors purses sale
fitflops sale clearance
ralph lauren outlet online
coach factory outlet
air jordan retro 6
jordan 11
cheap jordans shoes
prada shoes
ghd
fitflops sale
coach wallets outlet
jordan shoes for cheap
under armour sale
vans outlet
juicy couture
nike stores
hermes outlet online
kobe bryant shoes
cheap nike air max
abercrombie and fitch outlet store
nike nfl jerseys
lebron james basketball shoes
20151012yxj
christian louboutin outlet
ReplyDeletefitflop shoes
nike outlets
abercrombie
jordan shoes for sale
boys north face jackets
columbia outlet
louboutin outlet
coach shoes
nike air max
joes new balance outlet
lebron 12
celine bags
polo ralph lauren factory store
nike air max
mont blanc mountain
coach purses
cheap nike shoes
jordan shoes for women
coach factory outlet online
fitflop sandals for women
fitflops clearance
michael kors cheap
fitflops sale
lebron 12 shoes
celine handbags
fitflops clearance
gucci belt
nike outlet store online shopping
kobes shoes
toms shoes
christian louboutin shoes
coach handbags outlet
hollister clothing store online
jordan retro 6
salvatore ferragamo belt
louboutin outlet
michael kors outlet
fendi bags
coach factory outlet
cheap polo shirts
the north face jackets
tod's
ugg boots
20151012yxj