Export Form Wizard Pro To Word

If you are looking for an easy way to export Form Wizard Pro submissions in MS Word format, feel free to drop the following user control into your FormWizard folder:

<%@ Control Language="C#" AutoEventWireup="true" ClassName="ExportWordControl.ascx"
Inherits="System.Web.UI.UserControl" %>
<%@ Import Namespace="mojoPortal.Business" %>
<%@ Import Namespace="mojoPortal.Business.WebHelpers" %>
<%@ Import Namespace="mojoPortal.Web.Framework" %>
<%@ Import Namespace="mojoPortal.Web.Controls" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Globalization" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<%@ Import Namespace="System.Configuration" %>
<%@ Import Namespace="System.Text" %>
<script runat="server">

protected void btnExportWord_Click(object sender, EventArgs e)
{
SqlConnection conn = null;

string connString = ConfigurationManager.AppSettings[“MSSQLConnectionString”];
conn = new SqlConnection(connString);
string query = string.Format(“SELECT sts_WebFormQuestion.QuestionText, sts_WebFormResponse.Response, sts_WebFormResponseSet.CreatedUtc FROM sts_WebFormQuestion INNER JOIN sts_WebFormResponse ON sts_WebFormQuestion.Guid = sts_WebFormResponse.QuestionGuid INNER JOIN sts_WebFormResponseSet ON sts_WebFormResponse.ResponseSetGuid = sts_WebFormResponseSet.Guid INNER JOIN mp_Modules ON sts_WebFormQuestion.FormGuid = mp_Modules.Guid WHERE (mp_Modules.ModuleID = ‘{0}’) ORDER BY sts_WebFormResponseSet.CreatedUtc, sts_WebFormQuestion.SortOrder”, Page.Request.QueryString[“mid”].ToString());
SqlCommand cmd = new SqlCommand(query, conn);
conn.Open();
SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
DataTable dt = new DataTable();
dt.Load(dr);

string strBody = DataTable2WordString(dt);
Response.AppendHeader(“Content-Type”, “application/msword”);
Response.AppendHeader(“Content-disposition”, “attachment; filename=WordExport.doc”);
Response.Write(strBody);
}

public string DataTable2WordString(System.Data.DataTable pDT)
{
StringBuilder sbTop = new StringBuilder();

sbTop.Append(“<html ” + “xmlns:o=’urn:schemas-microsoft-com:office:office’ ” + “xmlns:w=’urn:schemas-microsoft-com:office:word'” + “xmlns=’http://www.w3.org/TR/REC-html40′>” + “<head><title>Time</title>”);

sbTop.Append(“<!–[if gte mso 9]>” + “<xml>” + “<w:WordDocument>” + “<w:View>Print</w:View>” + “<w:Zoom>90</w:Zoom>” + “</w:WordDocument>” + “</xml>” + “<![endif]–>”);

sbTop.Append(“<style>” + “<!– /* Style Definitions */” + “@page Section1″ + ”   {size:8.5in 11.0in; ” + ”   margin:1.0in 1.25in 1.0in 1.25in ; ” + ”   mso-header-margin:.5in; ” + ”   mso-footer-margin:.5in; mso-paper-source:0;}” + ” div.Section1″ + ”   {page:Section1;}” + “–>” + “</style></head>”);

sbTop.Append((“<body lang=EN-US style=’tab-interval:.5in’>” + “<div class=Section1>” + “<h1>” + pDT.TableName + “</h1>” + “<p style=’color:red’><I>”) + DateTime.Now + “</I></p></div><table border=’0′>”);

string bottom = “</table></body></html>”;

StringBuilder sb = new StringBuilder();

foreach (DataRow row in pDT.Rows)
{
sb.Append(“<tr>”);

foreach (DataColumn col in pDT.Columns)
{
if (row[col].ToString() == “Date:”)
{
sb.Append(“</tr><tr><td>&nbsp;</td><td>&nbsp;</td></tr>”);
}

sb.Append(“<td>”);
sb.Append(row[col]);
sb.Append(“</td>”);
}
sb.Append(“</tr>”);
}

string SSxml = sbTop.ToString() + sb.ToString() + bottom;

return SSxml;

}

</script>
<asp:ImageButton ID=”imgExportWord” ImageUrl=”~/Data/SiteImages/Icons/doc.png” OnClick=”btnExportWord_Click” runat=”server” />

Then you’d need to register the control within your FormResults.aspx page:

<%@ Register Src="ExportWordControl.ascx" TagName="ExportWordControl" TagPrefix="epw" %>
And finally, add an instance of the control to the page (eg, right after the ImageButton control that exports to Excel:

<epw:ExportWordControl id="ExportWord1" runat="server" />