Saving Excel from Template urgent
-
using System.IO; using excel09; using System.Reflection ; using System.Runtime.InteropServices; I am creating a new excel file of version 9 from template of excel 2003. An Image is there in the template file. The excel file is got saved and opened.But image is not getting displayed. Code is Page.Controls.Clear(); Response.ContentType = "application/vnd.ms-excel"; Response.Buffer=true; //To open excel files in lower versions also //Save the byte[] array to xl template file string xlFileName = "Template.xlt"; string xlFilePath = Server.MapPath("../Reports/") + xlFileName; string fileExcel = Server.MapPath("../Reports/" + xlFileName); //template file //Creating instance for excel 9 version //To create excel file in lower version excel09.Application ExcelObj = new excel09.Application(); ExcelObj.Visible = true; xlFileName = "Report - " + System.DateTime.Now.Ticks.ToString() +".xls"; xlFilePath = Server.MapPath("../Reports/") + xlFileName; //Save as file name //Adding template to workbook ExcelObj.Workbooks.Add(fileExcel); Workbooks workbooks = ExcelObj.Workbooks; _Workbook workbook = workbooks.get_Item(1); Sheets sheets = workbook.Worksheets; _Worksheet worksheet = (_Worksheet) sheets.get_Item(1); //Saving excel file in 9 version format workbook.SaveAs(xlFilePath, excel09.XlFileFormat.xlWorkbookNormal,null,null,false,false,excel09.XlSaveAsAccessMode.xlShared,false,false,null,null); workbook.Close(null,null,null); ExcelObj.Workbooks.Close(); ExcelObj.Quit(); System.Runtime.InteropServices.Marshal.ReleaseComObject (ExcelObj); System.Runtime.InteropServices.Marshal.ReleaseComObject (sheets); System.Runtime.InteropServices.Marshal.ReleaseComObject (workbook); ExcelObj = null; workbook = null; sheets=null; System.GC.Collect(); //Getting from excel version 9 file Response.WriteFile(xlFilePath); Response.Flush(); Response.Buffer=false; Response.End();