0

I'm exporting some data I have in a database into Excel. Although the below code works fine, I would like to know how to manipulate headers, colours, and generally the way the cells look?

page.aspx

   <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
    ConnectionString="<%$ ConnectionStrings:ConnectionString %>"  SelectCommand="SELECT ID, Number, Title, Name FROM Requests">
 <SelectParameters>
        <asp:QueryStringParameter DefaultValue="" Name="ID" 
            QueryStringField="ID" Type="Int32" />
    </SelectParameters>
</asp:SqlDataSource>

  <asp:DetailsView ID="DetailsView1" runat="server" Height="50px" Width="125px" 
    AutoGenerateRows="False" DataKeyNames="RequestID" DataSourceID="SqlDataSource1">
    <Fields>
           <asp:BoundField DataField="ID" HeaderText="ID" 
            SortExpression="ID" InsertVisible="False" ReadOnly="True" />
        <asp:BoundField DataField="Number" HeaderText="Number" 
            SortExpression="Number" />
        <asp:BoundField DataField="Title" HeaderText="Title" SortExpression="Title" />
        <asp:BoundField DataField="Name" HeaderText="Name" 
            SortExpression="Name" />

page.aspx.vb

    Dim tw As New System.IO.StringWriter()
    Dim hw As New System.Web.UI.HtmlTextWriter(tw)

    Dim dgGrid As New DetailsView()

    dgGrid.DataSource = SqlDataSource1

    hw.WriteLine("<b>Title here</b>")

    dgGrid.HeaderStyle.Font.Bold = True
    dgGrid.DataBind()
    dgGrid.RenderControl(hw)

    Response.AddHeader("content-disposition", "attachment;filename=ReportOuput.xls")
    Response.ContentType = "application/vnd.ms-excel"
    Me.EnableViewState = False
    Response.Write(tw.ToString())
    Response.End()

2 Answers 2

1

I use the following to set the number format. Search for mso-number-format to find more information.

string style = @" .text { mso-number-format:\@; } ";

Sign up to request clarification or add additional context in comments.

Comments

0

The following code does the work (I think I was binding the DetailsView too soon. Programmers explain if possible, thank u)

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    Response.Clear()
    Response.Buffer = True
    Response.AddHeader("content-disposition", "attachment;filename=ReportOutput.xls")
    Response.Charset = ""
    Response.ContentType = "application/vnd.ms-excel"

    Dim tw As New System.IO.StringWriter()
    Dim hw As New System.Web.UI.HtmlTextWriter(tw)

    hw.WriteLine("<h3>Output Form</h3>")
    DetailsView1.DataSource = SqlDataSource1
    DetailsView1.DataBind()
    DetailsView1.RenderControl(hw)
    Dim style As String = "<style> .textmode { mso-number-format:\@; } </style>"

    Response.Write(style)
    Response.Output.Write(tw.ToString())
    Response.Flush()
    Response.End()
    end Sub

In my DetailsView, I now can modify cells background colour, size, etc, example:

  <asp:TemplateField HeaderStyle-BorderColor="LightGray" ItemStyle-BorderColor="LightGray" HeaderStyle-BackColor="LightGray" ItemStyle-BackColor="LightGray" HeaderText="Personal Details"></asp:TemplateField>   
  <asp:BoundField DataField="Title" HeaderText="Dependants" ItemStyle-HorizontalAlign="Right" SortExpression="Dependants" />

I can also modify text, colour, etc in my code behind, example:

 Protected Sub DetailsView1_DataBound(ByVal sender As Object, ByVal e As System.EventArgs) Handles DetailsView1.DataBound
If DetailsView1.Rows(12).Cells(1).Text = "False" Then
        DetailsView1.Rows(12).Cells(1).Text = "This is false"
    ElseIf DetailsView1.Rows(12).Cells(1).Text = "True" Then
        DetailsView1.Rows(12).Cells(1).Text = "This is true"
    End If
   end sub

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.