Wrox Programmer Forums
|
BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3
This is the forum to discuss the Wrox book Beginning ASP.NET 3.5: In C# and VB by Imar Spaanjaars; ISBN: 9780470187593
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old April 10th, 2009, 05:39 PM
Authorized User
 
Join Date: Feb 2009
Posts: 10
Thanks: 1
Thanked 2 Times in 2 Posts
Default

Dear Imar,

to make things a bit more complete, I've given my users the option to chose between downloading the files as text file and downloading as Excel file. So why not share it here. The code is based on a short article by Eric Smith (http://www.codeguru.com/csharp/.net/...cle.php/c12659).

First I created a addional radiobuttonlist in the aspx page and an if clause in the code behind
Code:
 <%--aspx code--%>
      <asp:RadioButtonList ID="rblFormaat" runat="server">
      <asp:ListItem Value="excel">As Excel file</asp:ListItem>
      <asp:ListItem Value="text">As text file</asp:ListItem>
      </asp:RadioButtonList></td></tr>

// code behind
    protected void btnSubmit_Click(object sender, EventArgs e)
    {

      Label1.Text = ddScaNaam.SelectedValue;

      foreach (ListItem item in rblExport.Items)
      {
        if (item.Selected == true)
        {
          string connectionString = WebConfigurationManager.ConnectionStrings["ScaDatabaseConnectionString"].ConnectionString;
          string filename = "";

if (rblFormaat.Text == "text")
          {
// see code in previous post
}

 else if (rblFormaat.Text == "excel")
          {
            Response.Redirect("ExportExcel.aspx?filename=" + filename);
          }
        }

      }
    }
In case the user chooses to download as Excel file, the code redirects to a page without HTML; otherwise you won't catch the file. The page without HTML only contains a page directive:
Code:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ExportExcel.aspx.cs" Inherits="ExportExcel" %>


// code behind
protected void Page_Load(object sender, EventArgs e)
    {
      string filename = Request.QueryString["filename"];
      string connectionString = WebConfigurationManager.ConnectionStrings["ScaDatabaseConnectionString"].ConnectionString;
      using (SqlConnection cn = new SqlConnection(connectionString))
      {

        cn.Open();
        SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM " + filename, cn);
        DataTable dt = new DataTable();
        da.Fill(dt);
        cn.Close();

        Response.Clear();
        //   Response.ContentType = "application/vnd.ms-excel";

        string sep = "";
        foreach (DataColumn dc in dt.Columns)
        {
          Response.Write(sep + dc.ColumnName);
          sep = "\t";
        }
        Response.Write("\n");

        int i;
        foreach (DataRow dr in dt.Rows)
        {
          sep = "";
          for (i = 0; i < dt.Columns.Count; i++)
          {
            Response.Write(sep + dr[i].ToString());
            sep = "\t";
          }
          Response.Write("\n");
        }
        Response.AppendHeader("Content-Disposition", "attachment; filename=" + filename+".xls");
      }
    }
Hans
The Following User Says Thank You to hans For This Useful Post:
Imar (April 11th, 2009)





Similar Threads
Thread Thread Starter Forum Replies Last Post
importing and exporting database with multi-langua lenseere Oracle 0 November 30th, 2007 07:34 PM
importing and exporting database in multi-language lenseere General .NET 0 November 29th, 2007 04:47 PM
Exporting Table to other database Scootterp Access VBA 4 September 5th, 2006 06:18 PM
exporting access database to sql server Sheena SQL Server 2000 2 December 29th, 2004 04:06 AM
Exporting access database to Excel .xls Squall Leonhart Classic ASP Databases 5 December 2nd, 2003 07:42 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.