gsalunkhe
gsalunkhe

Reputation: 41

html content into docx file

I am taking data from stringBuilder and putting it into .docx file. For .doc extension we can do using stringBuilder directly. But for .docx file there is a problem.

Like this. But its corrupting the .docx file.

strBuilder.Append("".ToString()); strBuilder.Append("".ToString()); 
strBuilder.Append("SI.No ".ToString()); strBuilder.Append("<\table>");

FileStream stream = File.Open(@"D:\wordfile3.DOCX", FileMode.Create);

System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();

byte[] binaryData = encoding.GetBytes(strBuilder.ToString());

for (int i = 0; i < binaryData.Length; i++)
{
    stream.WriteByte(binaryData[i]);
}

stream.Flush();
stream.Close();

Upvotes: 2

Views: 1803

Answers (1)

Aristos
Aristos

Reputation: 66641

You have mix up the thinks.

The Doc and the Docx have a complex struct and not just text. Open a real doc or docx file with any hex viewer to see whats inside, and then open yours to compare them

What you do here is that you make a potential text file and you just change the extension to doc or docx. Now when a file like that is going to be read by MS Word, ms word convert it to doc or docx and this is not because you make it a ms word file, but because ms word recognize that is a text file and is try to convert it and show it.

Total wrong approach.

There are some tutorial on how to make ms word documents from asp.net, but this is not a way, to write anything and save it as docx and wait to work.

https://www.google.com/search?q=how+to+create+ms+word+files+from+asp.net

http://www.codeproject.com/KB/aspnet/wordapplication.aspx

Upvotes: 2

Related Questions