DataGrid Export To Excel - Set Landscape

Discussion in 'ASP .Net' started by klaframboise, Sep 21, 2008.

  1. klaframboise

    klaframboise Guest

    I've been searching for an answer to this for a few hours and I've found lots
    of people with the same issue, but no solution. I'm exporting a datagrid to
    excel which works fine, and I'm able to set the margins fine also, but for
    some reason the page layout won't change to landscape. I'm using .Net 2.0
    and Office 2003.

    Here is the code:

    Current.Response.Clear()
    Current.Response.Buffer = True
    Current.Response.AddHeader("content-disposition",
    String.Format("attachment; filename={0}", fileName))
    Current.Response.ContentType = "application/vnd.ms-excel"
    Current.Response.Write("<meta http-equiv=Content-Type
    content=""text/html; charset=utf-8"">" & vbNewLine)
    Current.Response.Write(GetExcelStyling())
    Current.Response.Write(sw.ToString)
    Current.Response.Write("</body>")
    Current.Response.Write("</html>")
    Current.Response.End()

    Private Shared Function GetExcelStyling() As String
    Dim sb As New StringBuilder
    sb.AppendLine("<html
    xmlns:x=""urn:schemas-microsoft-com:eek:ffice:excel"">")
    sb.AppendLine("<head>")
    sb.AppendLine("<style>")
    sb.AppendLine("@page{mso-page-orientation:landscape;")
    sb.AppendLine("margin:.5in .5in .5in .5in;")
    sb.AppendLine("mso-header-margin:.5in;")
    sb.AppendLine("mso-footer-margin:.5in}")
    sb.AppendLine("</style>")
    sb.AppendLine("</head>")
    sb.AppendLine("<body>")
    Return sb.ToString()
    End Function

    Thanks Kevin
     
    klaframboise, Sep 21, 2008
    #1
    1. Advertisements

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.