ASP 101 - Active Server Pages 101 - Web06
The Place ASP Developers Go!

Windows Technology Windows Technology
15 Seconds
ASP 101
ASP Wire
VB Forums
VB Wire
internet.commerce internet.commerce
Partners & Affiliates
ASP 101 is an site
ASP 101 is an site
Internet News
Small Business
Personal Technology

Corporate Info
Tech Jobs
E-mail Offers

ASP 101 News Flash ASP 101 News Flash

 Top ASP 101 Stories Top ASP 101 Stories
Connections, Commands, And Procedures
What is ASP?
VBScript Classes: Part 1 of N

Using " in your Strings
Show All Tips >>
ASP 101 RSS Feed ASP 101 Updates

Quick Tips

Saving Data to an Excel File

People really love Microsoft Excel. Many users are so comfortable with Excel they seem to want to use it for everything. Which is probably why I get so many requests about using Excel with ASP.NET. As it turns out, the real question most users want answered is how can they store data from a Web page in an Excel-readable format?

While using a single-user application like Excel in a server environment is generally a very bad idea, there's no reason you can't generate data files that Excel is happy to open. I've previously published a few examples of this type of thing which utilize Excel's ability to import HTML tables quite nicely: Displaying Data from ASP in Excel and our Excel Sample.

This time around, the goal is more getting a chunk of data into Excel in order to do something with it rather then simply using Excel as a client-side data viewer. To that end, this ASP.NET code snippet is going to take data entered by a user and save it to a CSV (comma-separated values) file. Then you can take the .csv file and open it using Excel and manipulate it to your heart's content.

<%@ Page Language="VB" %>
<%@ Import Namespace="System.IO" %>
<script runat="server">
    Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim sbTextToWrite As New StringBuilder

        sbTextToWrite.Append(Now().ToString & ",")
        sbTextToWrite.Append(txtName.Text & ",")
        sbTextToWrite.Append(txtAddress.Text & ",")
        sbTextToWrite.Append(txtCity.Text & ",")
        sbTextToWrite.Append(txtState.Text & ",")
        sbTextToWrite.Append(txtZip.Text & ",")

        File.AppendAllText(Server.MapPath("datafile.csv"), sbTextToWrite.ToString)
    End Sub
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
<html xmlns="">
    <title>Form To CSV (aka. Excel File)</title>
<form id="myForm" runat="server">

    Name: <asp:TextBox ID="txtName" runat="server" /><br />
    Address: <asp:TextBox ID="txtAddress" runat="server" /><br />
    City: <asp:TextBox ID="txtCity" runat="server" /><br />
    State: <asp:TextBox ID="txtState" runat="server" /><br />
    Zip: <asp:TextBox ID="txtZip" runat="server" /><br />
    Phone: <asp:TextBox ID="txtPhone" runat="server" /><br />
    <asp:Button ID="btnSubmit" runat="server"
        Text="Save Contact Info"


In an effort to keep things simple, I've left making the form pretty and the validation for you to implement. Aside from that and making sure the NTFS permissions on the file system are set to allow you to write to the data file, the sample script should work as it stands.

Previous      Show All Tips      Next

If you have a tip you would like to submit, please send it to:

Home |  News |  Samples |  Articles |  Lessons |  Resources |  Forum |  Links |  Search |  Feedback
The Network for Technology Professionals



Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers