How to Export Images into Excel using ASP.NET:
Normally we will export html table into Excel.Here we are going to export images into Excel 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.Excel.Application ExcelApp = new Microsoft.Office.Interop.Excel.Application();
string fileName = Server.Mappath(“~/Sample.doc”);
object fi=fileName ;
object missing = System.Reflection.Missing.Value;
Microsoft.Office.Interop.Excel.WorkBooks WBS = ExcelApp.Workbooks;
Microsoft.Office.Interop.Excel._Workbook book = WBS.Add(missing);
Microsoft.Office.Interop.Excel.Sheets sheets = book.WorkSheets;
Microsoft.Office.Interop.Excel._Worksheet Work_Sheet = (Microsoft.Office.Interop.Excel._Worksheet ) sheets.get_item(1);
object lTrue=true;
object lFalse=false;
Work_Sheet.Shapes.AddPicture(path,Microsoft.Office.Core.MsoTriState.msoFalse,Microsoft.Office.Core.MsoTriState.msoCTrue,0,0,450,300);
Work_Sheet.SaveAs(fileName,missing ,missing ,missing ,missing ,missing ,missing ,missing ,missing ,missing );
book.Close(lTrue,fi,lFalse);
if(ExcelApp != null)
{
ExcelApp.Quit(ref lTrue,ref lFalse,ref lFalse);
System.Runtime.InteropServices.Marshall.ReleaseComObject(ExcelApp );
}
Response.ContentType = “application/ms-Excel”;
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.
I really enjoyed this post. I can tell you put in a great deal of effort and time into this post. I will be back to read more as you post more!
Great site man , just keep it .
I love your blog keep up the good work