Skip to content

Instantly share code, notes, and snippets.

@Dheer-Rajpoot
Created September 16, 2017 18:06
Show Gist options
  • Save Dheer-Rajpoot/61677f95e6e1cb06869c6898612ecd78 to your computer and use it in GitHub Desktop.
Save Dheer-Rajpoot/61677f95e6e1cb06869c6898612ecd78 to your computer and use it in GitHub Desktop.
WFFM | Customizing export form data to excel functionality
using Sitecore;
using Sitecore.Diagnostics;
using Sitecore.Jobs;
using Sitecore.Security.Accounts;
using Sitecore.WFFM.Abstractions.Analytics;
using Sitecore.WFFM.Abstractions.Data;
using Sitecore.WFFM.Abstractions.Dependencies;
using Sitecore.WFFM.Services.Pipelines;
using Sitecore.WFFM.Speak.ViewModel;
using System;
using System.Linq;
using System.Xml;
namespace Website.WFFM.Pipelines
{
public class CustomExportFormDataToExcel
{
public CustomExportFormDataToExcel()
{
}
private void BuildBody(XmlDocument doc, IFormItem item, FormPacket packet, XmlElement root)
{
foreach (FormData entry in packet.Entries)
{
root.AppendChild(this.BuildRow(entry, item, doc));
}
}
private void BuildHeader(XmlDocument doc, IFormItem item, XmlElement root)
{
XmlElement xmlElement = doc.CreateElement("Row");
string exportRestriction = DependenciesManager.FormRegistryUtil.GetExportRestriction(item.ID.ToString(), string.Empty);
if (exportRestriction.IndexOf("created", StringComparison.Ordinal) == -1)
{
XmlElement xmlElement1 = this.CreateHeaderCell("String", "Created", doc);
xmlElement.AppendChild(xmlElement1);
}
IFieldItem[] fields = item.Fields;
for (int i = 0; i < (int)fields.Length; i++)
{
IFieldItem fieldItem = fields[i];
if (exportRestriction.IndexOf(fieldItem.ID.ToString(), StringComparison.Ordinal) == -1)
{
XmlElement xmlElement2 = this.CreateHeaderCell("String", fieldItem.FieldDisplayName, doc);
xmlElement.AppendChild(xmlElement2);
}
}
//adding header for Browser Details
XmlElement xmlElement3 = this.CreateHeaderCell("String", "BROWSER DETAILS", doc);
xmlElement.AppendChild(xmlElement3);
root.AppendChild(xmlElement);
}
private XmlElement BuildRow(FormData entry, IFormItem item, XmlDocument xd)
{
XmlElement xmlElement = xd.CreateElement("Row");
string exportRestriction = DependenciesManager.FormRegistryUtil.GetExportRestriction(item.ID.ToString(), string.Empty);
if (exportRestriction.IndexOf("created") == -1)
{
DateTime localTime = entry.Timestamp.ToLocalTime();
XmlElement xmlElement1 = this.CreateCell("String", localTime.ToString("G"), xd);
xmlElement.AppendChild(xmlElement1);
}
IFieldItem[] fields = item.Fields;
for (int i = 0; i < (int)fields.Length; i++)
{
IFieldItem fieldItem = fields[i];
if (exportRestriction.IndexOf(fieldItem.ID.ToString(), StringComparison.Ordinal) == -1)
{
FieldData fieldDatum = entry.Fields.FirstOrDefault<FieldData>((FieldData f) => f.FieldId == fieldItem.ID.Guid);
XmlElement xmlElement2 = this.CreateCell("String", (fieldDatum != null ? fieldDatum.Value : string.Empty), xd);
xmlElement.AppendChild(xmlElement2);
}
}
//adding details for browser details field
FieldData fieldData = entry.Fields.FirstOrDefault<FieldData>((FieldData f) => f.FieldName == "BROWSER DETAILS");
XmlElement xmlElement3 = this.CreateCell("String", (fieldData != null ? fieldData.Value : string.Empty), xd);
xmlElement.AppendChild(xmlElement3);
return xmlElement;
}
private XmlElement CreateCell(string sType, string sValue, XmlDocument doc)
{
XmlElement xmlElement = doc.CreateElement("Cell");
XmlAttribute xmlAttribute = doc.CreateAttribute("ss", "StyleID", "xmlns");
xmlAttribute.Value = "xVerdana";
xmlElement.Attributes.Append(xmlAttribute);
XmlElement xmlElement1 = doc.CreateElement("Data");
XmlAttribute xmlAttribute1 = doc.CreateAttribute("ss", "Type", "xmlns");
xmlAttribute1.Value = sType;
xmlElement1.Attributes.Append(xmlAttribute1);
xmlElement1.InnerText = sValue;
xmlElement.AppendChild(xmlElement1);
return xmlElement;
}
private XmlElement CreateHeaderCell(string sType, string sValue, XmlDocument doc)
{
XmlElement xmlElement = doc.CreateElement("Cell");
XmlAttribute xmlAttribute = doc.CreateAttribute("ss", "StyleID", "xmlns");
xmlAttribute.Value = "xBoldVerdana";
xmlElement.Attributes.Append(xmlAttribute);
XmlElement xmlElement1 = doc.CreateElement("Data");
XmlAttribute xmlAttribute1 = doc.CreateAttribute("ss", "Type", "xmlns");
xmlAttribute1.Value = sType;
xmlElement1.Attributes.Append(xmlAttribute1);
xmlElement1.InnerText = sValue;
xmlElement.AppendChild(xmlElement1);
return xmlElement;
}
public void Process(FormExportArgs args)
{
Job job = Context.Job;
if (job != null)
{
job.Status.LogInfo(DependenciesManager.ResourceManager.Localize("EXPORTING_DATA"));
}
string item = args.Parameters["contextUser"];
Assert.IsNotNullOrEmpty(item, "contextUser");
using (UserSwitcher userSwitcher = new UserSwitcher(item, true))
{
XmlDocument xmlDocument = new XmlDocument();
XmlElement xmlElement = xmlDocument.CreateElement("ss:Workbook");
XmlAttribute xmlAttribute = xmlDocument.CreateAttribute("xmlns");
xmlAttribute.Value = "urn:schemas-microsoft-com:office:spreadsheet";
xmlElement.Attributes.Append(xmlAttribute);
XmlAttribute xmlAttribute1 = xmlDocument.CreateAttribute("xmlns:o");
xmlAttribute1.Value = "urn:schemas-microsoft-com:office:office";
xmlElement.Attributes.Append(xmlAttribute1);
XmlAttribute xmlAttribute2 = xmlDocument.CreateAttribute("xmlns:x");
xmlAttribute2.Value = "urn:schemas-microsoft-com:office:excel";
xmlElement.Attributes.Append(xmlAttribute2);
XmlAttribute xmlAttribute3 = xmlDocument.CreateAttribute("xmlns:ss");
xmlAttribute3.Value = "urn:schemas-microsoft-com:office:spreadsheet";
xmlElement.Attributes.Append(xmlAttribute3);
XmlAttribute xmlAttribute4 = xmlDocument.CreateAttribute("xmlns:html");
xmlAttribute4.Value = "http://www.w3.org/TR/REC-html40";
xmlElement.Attributes.Append(xmlAttribute4);
xmlDocument.AppendChild(xmlElement);
XmlElement xmlElement1 = xmlDocument.CreateElement("Styles");
xmlElement.AppendChild(xmlElement1);
XmlElement xmlElement2 = xmlDocument.CreateElement("Style");
XmlAttribute xmlAttribute5 = xmlDocument.CreateAttribute("ss", "ID", "xmlns");
xmlAttribute5.Value = "xBoldVerdana";
xmlElement2.Attributes.Append(xmlAttribute5);
xmlElement1.AppendChild(xmlElement2);
XmlElement xmlElement3 = xmlDocument.CreateElement("Font");
XmlAttribute xmlAttribute6 = xmlDocument.CreateAttribute("ss", "Bold", "xmlns");
xmlAttribute6.Value = "1";
xmlElement3.Attributes.Append(xmlAttribute6);
XmlAttribute xmlAttribute7 = xmlDocument.CreateAttribute("ss", "FontName", "xmlns");
xmlAttribute7.Value = "verdana";
xmlElement3.Attributes.Append(xmlAttribute7);
xmlElement2.AppendChild(xmlElement3);
xmlElement2 = xmlDocument.CreateElement("Style");
xmlAttribute5 = xmlDocument.CreateAttribute("ss", "ID", "xmlns");
xmlAttribute5.Value = "xVerdana";
xmlElement2.Attributes.Append(xmlAttribute5);
xmlElement1.AppendChild(xmlElement2);
xmlElement3 = xmlDocument.CreateElement("Font");
xmlAttribute7 = xmlDocument.CreateAttribute("ss", "FontName", "xmlns");
xmlAttribute7.Value = "verdana";
xmlElement3.Attributes.Append(xmlAttribute7);
xmlElement2.AppendChild(xmlElement3);
XmlElement xmlElement4 = xmlDocument.CreateElement("Worksheet");
XmlAttribute xmlAttribute8 = xmlDocument.CreateAttribute("ss", "Name", "xmlns");
xmlAttribute8.Value = "Sheet1";
xmlElement4.Attributes.Append(xmlAttribute8);
xmlElement.AppendChild(xmlElement4);
XmlElement xmlElement5 = xmlDocument.CreateElement("Table");
XmlAttribute xmlAttribute9 = xmlDocument.CreateAttribute("ss", "DefaultColumnWidth", "xmlns");
xmlAttribute9.Value = "130";
xmlElement5.Attributes.Append(xmlAttribute9);
xmlElement4.AppendChild(xmlElement5);
this.BuildHeader(xmlDocument, args.Item, xmlElement5);
this.BuildBody(xmlDocument, args.Item, args.Packet, xmlElement5);
XmlElement xmlElement6 = xmlDocument.CreateElement("WorksheetOptions");
XmlElement xmlElement7 = xmlDocument.CreateElement("Selected");
XmlElement xmlElement8 = xmlDocument.CreateElement("Panes");
XmlElement xmlElement9 = xmlDocument.CreateElement("Pane");
XmlElement xmlElement10 = xmlDocument.CreateElement("Number");
xmlElement10.InnerText = "1";
XmlElement xmlElement11 = xmlDocument.CreateElement("ActiveCol");
xmlElement11.InnerText = "1";
xmlElement9.AppendChild(xmlElement11);
xmlElement9.AppendChild(xmlElement10);
xmlElement8.AppendChild(xmlElement9);
xmlElement6.AppendChild(xmlElement8);
xmlElement6.AppendChild(xmlElement7);
xmlElement4.AppendChild(xmlElement6);
args.Result = string.Concat("<?xml version=\"1.0\"?>", xmlDocument.InnerXml.Replace("xmlns:ss=\"xmlns\"", ""));
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment