Categories
Web Design & Development

Web Form Data Spreadsheet Download

Tagged with: , ,

Usually, a website owner has the data from their contact forms emailed to them.  But sometimes it can be useful for the site to automatically add that info to an ongoing list of contacts.  One way that most people like to be able to see and sort such data is in a spreadsheet.  It is quite easy to make a “fake” spreadsheet using an HTML table that Excel will open like any other .xls file.

Here are the basic steps:

    • Create and save a “spreadsheet” that defines the header row
    • Upon each contact form submittal, write an HTML row to that file 

You can use a simple text editor, such as notepad, to create the “.xls header file” that is ready to accept new data rows. Also shown below are some code snippets to add to the webform and the code behind the page. 

In the contacts.xls file (initially):

<html xmlns:o=”urn:schemas-microsoft-com:office:office”

xmlns:x=”urn:schemas-microsoft-com:office:excel”

xmlns=”http://www.w3.org/TR/REC-html40″>

<!–[if gte mso 9]><xml>

 <x:ExcelWorkbook>

  <x:ExcelWorksheets>

   <x:ExcelWorksheet>

    <x:Name>Sheet1</x:Name>

    <x:WorksheetOptions>

     <x:Selected/>

     <x:ProtectContents>False</x:ProtectContents>

     <x:ProtectObjects>False</x:ProtectObjects>

     <x:ProtectScenarios>False</x:ProtectScenarios>

    </x:WorksheetOptions>

   </x:ExcelWorksheet>

  </x:ExcelWorksheets>

  <x:WindowHeight>10365</x:WindowHeight>

  <x:WindowWidth>17100</x:WindowWidth>

  <x:WindowTopX>360</x:WindowTopX>

  <x:WindowTopY>105</x:WindowTopY>

  <x:ProtectStructure>False</x:ProtectStructure>

  <x:ProtectWindows>False</x:ProtectWindows>

 </x:ExcelWorkbook>

</xml><![endif]–>

</head>

<table>

<tr style=”font-weight:bold; text-align:center;”>

<td>Date</td>

<td>First Name</td>

<td>Last Name</td>

<td>Email</td>

<td>Phone</td>

</tr> 

In the contact-us.aspx file:

First Name: <asp:TextBox runat=”server” ID=”txtFirstName” /><br />

Last Name: <asp:TextBox runat=”server” ID=”txtLastName” /><br />

Email: <asp:TextBox runat=”server” ID=”txtEmail” /> <br />

Phone: <asp:TextBox runat=”server” ID=”txtPhone” /><br />

In the contact-us.aspx.cs file:

    protected void SaveData()

    {

        String strFilePath = Server.MapPath(“contacts.xls”);

        if (File.Exists(strFilePath))

        {

            StreamWriter objSW = new StreamWriter(strFilePath, true);   // true = append

            objSW.WriteLine(“<tr>”);

            objSW.WriteLine(“<td>” + DateTime.Now + “</td>”);

            objSW.WriteLine(“<td>” + txtFirstName.Text + “</td>”);

            objSW.WriteLine(“<td>” + txtLastName.Text + “</td>”);

            objSW.WriteLine(“<td>” + txtEmail.Text + “</td>”);

            objSW.WriteLine(“<td>” + txtPhone.Text + “</td>”);

            objSW.WriteLine(“</tr>\n”);

            objSW.Close();

        }

    }

Then all you have to do is link to that .xls file and Excel will open it right up. 

Note: Your server permissions may need to change to allow your site to write to files. Also remember that unless you write your “.xls” into a secure area, then it will be a publicly available file.