Normally we will export html table into word.Here we are going to export images into word using Asp.Net.
Step 1: Open Project,Right Click references and open Add Reference.
Step2: Select COM tab,and select Microsoft Excel 11.0 object library.
Step 3: Add using Microsoft.Office in yours Code.
Code:
Microsoft.Office.Interop.Word.Application wordApp = new Microsoft.Office.Interop.Word.Application();
string fileName = Server.Mappath(“~/Sample.doc”);
object fi=fileName ;
object missing = System.Reflection.Missing.Value;
Microsoft.Office.Interop.Word.Document doc = new Microsoft.Office.Interop.Word.Document();
object lTrue=true;
object lFalse=false;
doc = wordApp .Documents.Add(ref missing,ref missing,ref missing,ref missing);
Microsoft.Office.Interop.Word.Range range = doc.Content;
object ro = range;
doc.Content.Delete(ref missing,ref missing);
doc.InlineShapes.AddPicture(fileName,ref lfalse,ref lTrue,ref ro);
doc.SaveAs(ref fi,ref missing,ref missing,ref missing,ref missing,ref missing,ref missing,ref missing,ref missing,ref missing,ref missing,ref missing,ref missing,ref missing,ref missing,ref missing);
if(doc != null)
{
doc.Close(ref lTrue,ref lFalse,ref lFalse);
System.Runtime.InteropServices.Marshall.ReleaseComObject(doc);
}
if(wordApp != null)
{
wordApp.Quit(ref lTrue,ref lFalse,ref lFalse);
System.Runtime.InteropServices.Marshall.ReleaseComObject(wordApp );
}
Response.ContentType = “application/ms-word”;
Response.AddHeader(“content-disposition”,”attachment; filename=Sample.doc”);
Filestream sourceFile = new Filestream (fileName,FileMode.Open);
long FileSize;
FileSize = sourceFile.Length;
byte[] getContent = new byte[(int)FileSize];
sourceFile.Read(getContent,0,(int) sourceFile.Length);
souceFile.Close();
Response.BinaryWrite(getContent);
Hope this helps,
Happy Coding.
how wonderful. I, in practice, sort of flush this uncertain judgment.
Hey this is a great post. I’m going to email this to my buddies. I stumbled on this while surfing for some music lyrics, I’ll be sure to come back. thanks for sharing.