如何从SQL Server数据库导出数据时格式化Excel文件单元格

时间:2010-11-15 15:24:43

标签: .net export-to-excel

我将数据库中的一些数据导出到Excel中。虽然下面的代码工作正常,但我想知道如何操作标题,颜色以及细胞外观的方式?

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 个答案:

答案 0 :(得分:1)

我使用以下设置数字格式。搜索mso-number-format以查找更多信息。

string style = @“。text {mso-number-format:\ @;}”;

答案 1 :(得分:0)

以下代码完成了工作(我想我过早地绑定了DetailsView。如果可能,程序员会解释,谢谢你)

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

在我的DetailsView中,我现在可以修改单元格背景颜色,大小等,例如:

  <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" />

我也可以在后面的代码中修改文本,颜色等,例如:

 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