The Microsoft Dynamics CRM Blog
News and views from the Microsoft Dynamics CRM Team

OpenXML in Microsoft Dynamics CRM: Generating Excel Reports Using Microsoft Excel 2007

OpenXML in Microsoft Dynamics CRM: Generating Excel Reports Using Microsoft Excel 2007

  • Comments 3

Today our guest poster is CRM MVP Ayaz Ahmad who also runs the MSCRM Pakistan User Group on Yahoo.

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”);
if (!template.Exists)         
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))     

{           
try           
{               
 ExcelWorksheet worksheet = xlPackage.Workbook.Worksheets[”Sheet1″];               
if (worksheet != null)               
{                   
worksheet.Cell(2, 9).Value = ClearText(strData);                                       
xlPackage.Save();               
}           
}           
catch (Exception ex)           
{            }         
}

Adding Rows to Sheet          

worksheet.InsertRow(iRow);

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.

clip_image002

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.

clip_image003

 

Ayaz Ahmad

Page 1 of 1 (3 items)
Leave a Comment
  • Please add 2 and 5 and type the answer here:
  • Post