开发者

How to format excel file cells when data exported from SQL Server database

开发者 https://www.devze.com 2023-01-24 23:26 出处:网络
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?

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-disposit开发者_如何学Pythonion", "attachment;filename=ReportOuput.xls")
    Response.ContentType = "application/vnd.ms-excel"
    Me.EnableViewState = False
    Response.Write(tw.ToString())
    Response.End()


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

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


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
0

精彩评论

暂无评论...
验证码 换一张
取 消