Blog ( Of Ryan Olshan)

Recent Posts

News

About Ryan Olshan

LinkedIn Profile

Follow Ryan via twitter

Join StrongTypes - Free Generics Help By Email
Join StrongTypes

Community Credit Winner

View my MVP profile

The posts on this weblog are provided "as is" with no warranties and confer no rights. The opinions expressed herin are my own personal opinions and do not represent my employer's view in any way.

Subscribe to Rolog via My AOL
Subscribe to Rolog via Bloglines


Subscribe to Rolog via Feedburner

Furl Rolog




Subscribe to Rolog via Newsburst


Subscribe to Rolog via Pluck

Add Rolog to Technorati Favorites




Thycotic Secret Server

Receive Email Updates


.NET Websites

  • => StrongTypes.com <=
  • Community Credit
  • DotNetCharity.com
  • DotNetSlackers
  • EasyListBox
  • LearnAsp.com
  • SystemNetMail.com
  • SystemWebMail.com

Blogroll

  • Alex Campbell
  • Carlos Magalhaes
  • Charles Carroll
  • Chris Taylor
  • Darren Neimke
  • David Betz
  • Eric Smith
  • Rob Caron
  • Rob Howard
  • Sam Gentile
  • Scott Guthrie
  • Scott Mitchell
  • Tess
  • Wesner Moise
  • Jason Alexander
  • Bill Ryan
  • Steve Smith
  • Sahil Malik
  • David Silverlight

Other Websites

  • 911 Pigeon Alert
  • Pacific Wildlife Project

Strong Coder Bloggers

  • Dean Fiala
  • Doug Nelson
  • Kevin Jones

Yahoo! Groups

  • => StrongTypes <=
  • AspNet2
  • AspNet20GridView
  • PayPalDevDotNet
  • RubyOnRails
  • VSTS

Useful export helper class

Below is a helper class that I use for exporting a DataGrids contents to an external file. The below code references gridViewText, but I haven't tested it out on a GridView yet as the code is from an application I'm working on upgrading. But, it works like a charm on the DataGrid.

public sealed class ExportHelper
{
    public static void ExportToCsv( string gridViewText, string contentType, HttpResponse response)
    {
        const string m_Delimiter_Column = "," ;
        string m_Delimiter_Row = Environment .NewLine;

        response.ContentType = contentType;

        Regex m_RegEx = new Regex (@ "(>\s+<)" , RegexOptions .IgnoreCase);
        gridViewText = m_RegEx.Replace(gridViewText, "><" );

        gridViewText = gridViewText.Replace(m_Delimiter_Row, String .Empty);
        gridViewText = gridViewText.Replace( "" , m_Delimiter_Row);
        gridViewText = gridViewText.Replace( " " , String .Empty);
        gridViewText = gridViewText.Replace(m_Delimiter_Column, "\\" + m_Delimiter_Column);
        gridViewText = gridViewText.Replace( " " , m_Delimiter_Column);

        m_RegEx = new Regex (@ "<[^>]*>" , RegexOptions .IgnoreCase);
        gridViewText = m_RegEx.Replace(gridViewText, String .Empty);

        gridViewText = HttpUtility .HtmlDecode(gridViewText);
        response.Write(gridViewText);
        response.End();
    }

    public static void ExportToExcelWord( string gridViewText, string contentType, HttpResponse response)
    {
        response.ContentType = contentType;
        response.Write(gridViewText);
        response.End();
    }
}

Share this post: | bookmark it! | digg it! | reddit! | kick it! | live it!
Posted: Jan 24 2006, 06:18 AM by Ryan Olshan | with no comments
Filed under: ,

Comments

No Comments