Office Open XML is an XML-based file format specification for electronic documents such as memos, reports, books, spreadsheets, charts, presentations and word processing documents. The specification has been developed by Microsoft as a successor of its binary office file formats and was published by Ecma International as the Ecma 376 standard in December 2006. The format specification is available for free at Ecma International.
Let me introduce you the technique I have used to generate the Microsoft Excel 2007 sheet from Microsoft Dynamics CRM 3 data. I have downloaded an Excel package. ExcelPackage provides server-side generation of Excel 2007 spreadsheets. See http://www.codeplex.com/ExcelPackage for details.
Specifying Output Directory and Template Directory
Output directory is where you place the generated excel file and Template directory is where you place the template file. Template file is simply a .xlsx file without data. Use following code to specify both directories.
DirectoryInfo outputDir = new DirectoryInfo(@”\GenerateExcel\output”);
DirectoryInfo templateDir = new DirectoryInfo(@”\GenerateExcel\templates”);
FileInfo newFile = new FileInfo(outputDir.FullName + @”\File-” + strRecordID + “.xlsx”);
FileInfo template = new FileInfo(templateDir.FullName + @”\TempReport.xlsx”);
throw new Exception(”Template file does not exist! i.e. template.xlsx”);
Write data to Excel File
ExcelPackage contains library functions for accessing worksheet, cells etc. For details please have a look into the Excel package code. Uses following code to write data to individual cell in excel worksheet.
using (ExcelPackage xlPackage = new ExcelPackage(newFile, template))
ExcelWorksheet worksheet = xlPackage.Workbook.Worksheets[”Sheet1″];
if (worksheet != null)
worksheet.Cell(2, 9).Value = ClearText(strData);
catch (Exception ex)
Adding Rows to Sheet
Return Excel File for Download
Simply redirect the response object in ASP.NET to the excel file generated.
Response.Redirect(”http://” + Request.Url.Host + “:” + Request.Url.Port + “/” + “GenerateExcel/output/” + newFile.Name);
All cell styles, formats and pictures objects that you set once in Template will remain the same in the generated file.
Please find below my sample quote generation application. I have placed a button at Quotes toolbar named Generate Quote. It will generate quote report in excel 2007 with the Quote data from CRM currently selected.
My program fetches data from Microsoft Dynamics CRM 3 using web services and populates my Excel sheet. It then returns my generated Microsoft Excel sheet with options to open or download.