Export to Excel --but also Set Print Area

Discussion in 'ASP .Net Building Controls' started by Plateriot, Mar 8, 2009.

  1. Plateriot

    Plateriot Guest

    I have a handy routine I tweaked from an example that Exports an Excel from a
    gridview.

    It's almost perfect except for the fact that I'd also like to set the page
    to fit as many columns wide to each page. - For example, I have some
    gridviews that have data in 12 columns, but only 10 make it in the printout,
    so I get the broken overflow into undesired pages.

    So of course, the header will need to repeat in those other pages.

    Is there a way to set this in the code below? Or do I need to break out an
    Excel Object model for dot net?


    Public Shared Sub ExcelExport(ByVal gv As GridView, ByVal strFileName As
    String)
    'Exports an Excel spreadsheet of the named 'gv' gridview with the
    named strFileName

    'Requires this at the top of the module:
    'Public Overrides Sub VerifyRenderingInServerForm(ByVal control As
    System.Web.UI.Control)
    ' empty, needed for export2Excel to work.
    'End Sub

    HttpContext.Current.Response.Clear()
    HttpContext.Current.Response.AddHeader("content-disposition",
    "attachment;filename=" & strFileName & ".xls")
    HttpContext.Current.Response.Charset = ""

    HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache)
    HttpContext.Current.Response.ContentType = "application/vnd.xls"

    Dim stringWrite As New System.IO.StringWriter()
    Dim htmlWrite As System.Web.UI.HtmlTextWriter = New
    HtmlTextWriter(stringWrite)

    stringWrite.Write("<html><head><meta http-equiv=""content-type""
    content=""text/html; charset=utf-8""></head><body>")
    'Temporariilly remove paging and sorting to allow for plain
    formatting of
    'Spreadsheet
    gv.AllowPaging = False
    gv.AllowSorting = False
    gv.DataBind()

    gv.RenderControl(htmlWrite)
    stringWrite.Write("</body></html>")

    HttpContext.Current.Response.Write(stringWrite.ToString())
    HttpContext.Current.Response.Write("<x:FitToPage/>")
    HttpContext.Current.Response.[End]()

    End Sub
     
    Plateriot, Mar 8, 2009
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?U3JpZGhhcg==?=

    using Microsoft Excel image for Export to Excel button

    =?Utf-8?B?U3JpZGhhcg==?=, Dec 9, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    3,117
    =?Utf-8?B?U3JpZGhhcg==?=
    Dec 9, 2005
  2. Mark
    Replies:
    1
    Views:
    611
    Larry Barowski
    Jun 27, 2005
  3. =?Utf-8?B?SGVtYW50IFNpcGFoaW1hbGFuaQ==?=

    Export to Excel (Default File Type - Excel)

    =?Utf-8?B?SGVtYW50IFNpcGFoaW1hbGFuaQ==?=, Apr 24, 2006, in forum: ASP .Net
    Replies:
    15
    Views:
    17,539
    syed hassan
    May 21, 2009
  4. Grey
    Replies:
    4
    Views:
    2,001
    Mark Rae [MVP]
    Oct 17, 2007
  5. Erik

    Bulk export to Excel and force print

    Erik, Sep 16, 2006, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    260
Loading...

Share This Page