gridview properties for excel export

Discussion in 'ASP .Net' started by Sergio E., Jul 25, 2007.

  1. Sergio E.

    Sergio E. Guest

    Hi, I'm looking for information about gridview's cell properties...

    I've the following case: one gridview with alternatirg row style and
    normalrowstyle, 3 o 4 custom styles that are applied to cells in the
    row_databound event, and now I nedd to do a excel export of the gridview
    exactly as is showed in the browser, but al the examples and thigs I've
    tried don't send the cell color to excel and now I think if I can read the
    gridview properties in the prerender event to get them for each cell.

    I will apreciate any help,

    --
    Greetings and so many thanks,
    Sergio E.
    Sergio E., Jul 25, 2007
    #1
    1. Advertising

  2. A few years back in one of the contracts we did something similar to this -
    needed to export to excel a grid with formatting.
    We had xsl stylesheet defined and then Transformed the incoming Dataset xml
    into html. Then you change content type to "application/vnd.xls" and let
    Excel open it.
    I think starting from 97 version (I am not sure about prior ones) Excel will
    correctly process html.
    The html will have to be self contained - means no external references (css
    files, for example) - you will have to define everything inside your
    transformation.

    "Sergio E." wrote:

    > Hi, I'm looking for information about gridview's cell properties...
    >
    > I've the following case: one gridview with alternatirg row style and
    > normalrowstyle, 3 o 4 custom styles that are applied to cells in the
    > row_databound event, and now I nedd to do a excel export of the gridview
    > exactly as is showed in the browser, but al the examples and thigs I've
    > tried don't send the cell color to excel and now I think if I can read the
    > gridview properties in the prerender event to get them for each cell.
    >
    > I will apreciate any help,
    >
    > --
    > Greetings and so many thanks,
    > Sergio E.
    >
    >
    >
    =?Utf-8?B?U2VyZ2V5IFBvYmVyZXpvdnNraXk=?=, Jul 25, 2007
    #2
    1. Advertising

  3. Sergio E.

    Sergio E. Guest

    thank's for answering me, i was trying and the big trouble is that there is
    a css stylesheet that contains al de colors for the rows, alternate rows,
    and special cells, and some cells are re-styled in the gridview_rowdatabound
    event so i must use this styles and i must convert the gridview, not the
    dataset... mi big trouble now is the fact that the color are in css
    classes...and i don't know hot to access this information...

    if i can do some like
    me.getcssclass(me.gridview1.rows(0).cells(0).cssclass).backgroundcolor then
    i have my problem solved!, but i can't find information about how to get the
    specification of some class included in an attached file to an aspx with the
    <link href="Styles/Normal.css" rel="stylesheet" type="text/css" />
    directive.

    there is the code i'm using... and i don't understand why the colors are not
    rendered ... i supose that is because is the browser itself who paints the
    colors in te last step:

    public sub Excel()
    Dim sb As StringBuilder = New StringBuilder()
    Dim sw As StringWriter = New StringWriter(sb)
    Dim hw As HtmlTextWriter = New HtmlTextWriter(sw)
    Dim pag As Page = New Page()
    Dim frm As HtmlForm = New HtmlForm()
    Dim gv As GridView = Me.GVResultado

    pag.EnableEventValidation = False
    gv.AllowPaging = False
    gv.EnableViewState = False
    gv.DataBind()
    If (Not (gv.HeaderRow) Is Nothing) Then
    PrepareControlForExport(gv.HeaderRow)
    End If
    For Each row As GridViewRow In gv.Rows
    PrepareControlForExport(row)
    Next
    If (Not (gv.FooterRow) Is Nothing) Then
    PrepareControlForExport(gv.FooterRow)
    End If

    pag.DesignerInitialize()
    pag.Controls.Add(frm)
    frm.Controls.Add(gv)
    pag.RenderControl(hw)
    Response.Clear()
    Response.Buffer = True
    Response.ContentType = "application/vnd.ms-excel"
    Response.AddHeader("Content-Disposition", "attachment;filename=data.xls")
    Response.Charset = "UTF-8"
    Response.ContentEncoding = Encoding.Default
    Response.Write(sb.ToString())
    gv.AllowPaging = True
    gv.EnableViewState = True
    gv.DataBind()
    Response.End()
    end sub



    --
    thanks again,
    Sergio E.
    Sergio E., Jul 25, 2007
    #3
  4. Sergio,

    As far as I know you cannot do anything like
    me.getcssclass(me.gridview1.rows(0).cells(0).cssclass).backgroundcolor
    this is usually quite complex process performed (often differently) by
    different browsers.
    What I would suggest you to try would be to try reading the content of your
    css file (provided it does not have @includes from other css files) and try
    to stick it into the header of your output within <style> tags - this way the
    result html will become self-contained, and hopefully contain enough
    information for Excel to correctly display your information.

    Let me know how you go.
    If you have any more questions, I would be more than happy to assist

    "Sergio E." wrote:

    > thank's for answering me, i was trying and the big trouble is that there is
    > a css stylesheet that contains al de colors for the rows, alternate rows,
    > and special cells, and some cells are re-styled in the gridview_rowdatabound
    > event so i must use this styles and i must convert the gridview, not the
    > dataset... mi big trouble now is the fact that the color are in css
    > classes...and i don't know hot to access this information...
    >
    > if i can do some like
    > me.getcssclass(me.gridview1.rows(0).cells(0).cssclass).backgroundcolor then
    > i have my problem solved!, but i can't find information about how to get the
    > specification of some class included in an attached file to an aspx with the
    > <link href="Styles/Normal.css" rel="stylesheet" type="text/css" />
    > directive.
    >
    > there is the code i'm using... and i don't understand why the colors are not
    > rendered ... i supose that is because is the browser itself who paints the
    > colors in te last step:
    >
    > public sub Excel()
    > Dim sb As StringBuilder = New StringBuilder()
    > Dim sw As StringWriter = New StringWriter(sb)
    > Dim hw As HtmlTextWriter = New HtmlTextWriter(sw)
    > Dim pag As Page = New Page()
    > Dim frm As HtmlForm = New HtmlForm()
    > Dim gv As GridView = Me.GVResultado
    >
    > pag.EnableEventValidation = False
    > gv.AllowPaging = False
    > gv.EnableViewState = False
    > gv.DataBind()
    > If (Not (gv.HeaderRow) Is Nothing) Then
    > PrepareControlForExport(gv.HeaderRow)
    > End If
    > For Each row As GridViewRow In gv.Rows
    > PrepareControlForExport(row)
    > Next
    > If (Not (gv.FooterRow) Is Nothing) Then
    > PrepareControlForExport(gv.FooterRow)
    > End If
    >
    > pag.DesignerInitialize()
    > pag.Controls.Add(frm)
    > frm.Controls.Add(gv)
    > pag.RenderControl(hw)
    > Response.Clear()
    > Response.Buffer = True
    > Response.ContentType = "application/vnd.ms-excel"
    > Response.AddHeader("Content-Disposition", "attachment;filename=data.xls")
    > Response.Charset = "UTF-8"
    > Response.ContentEncoding = Encoding.Default
    > Response.Write(sb.ToString())
    > gv.AllowPaging = True
    > gv.EnableViewState = True
    > gv.DataBind()
    > Response.End()
    > end sub
    >
    >
    >
    > --
    > thanks again,
    > Sergio E.
    >
    >
    >
    =?Utf-8?B?U2VyZ2V5IFBvYmVyZXpvdnNraXk=?=, Jul 25, 2007
    #4
  5. Sergio E.

    Sergio E. Guest

    hi, thanks for this solution, i can read the css file, but i can'f find the
    sintax to do the next part (the output to the header with the <style> tags)
    i have the full css file loaded into a stringbuilder exactly as appears in
    the physical file...

    Can you post an example of the output to the header?

    Thank's again.

    --
    greetings
    Sergio E.
    Sergio E., Jul 25, 2007
    #5
  6. Sergio E.

    Sergio E. Guest

    hi, i finally solve the problem using this code:

    Response.Write("<HEAD><STYLE type=""text/css"">" +
    ReadCss("~/Styles/Normal.css") + "<STYLE></HEAD>")

    and then write the gridview...

    where readcss is a private function wich reads a given css file and returns
    it as simple string.

    --
    greetings,
    Sergio E.
    Sergio E., Jul 25, 2007
    #6
  7. glad that you have it all worked out :)

    "Sergio E." wrote:

    > hi, i finally solve the problem using this code:
    >
    > Response.Write("<HEAD><STYLE type=""text/css"">" +
    > ReadCss("~/Styles/Normal.css") + "<STYLE></HEAD>")
    >
    > and then write the gridview...
    >
    > where readcss is a private function wich reads a given css file and returns
    > it as simple string.
    >
    > --
    > greetings,
    > Sergio E.
    >
    >
    >
    >
    =?Utf-8?B?U2VyZ2V5IFBvYmVyZXpvdnNraXk=?=, Jul 26, 2007
    #7
    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,083
    =?Utf-8?B?U3JpZGhhcg==?=
    Dec 9, 2005
  2. Replies:
    2
    Views:
    3,123
    Jacob
    Mar 7, 2006
  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,470
    syed hassan
    May 21, 2009
  4. =?Utf-8?B?UGV0ZXI=?=
    Replies:
    0
    Views:
    594
    =?Utf-8?B?UGV0ZXI=?=
    May 16, 2006
  5. Grey
    Replies:
    4
    Views:
    1,943
    Mark Rae [MVP]
    Oct 17, 2007
Loading...

Share This Page