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
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.
If you have a tip you would like to submit, please send it to:
webmaster@asp101.com.