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

.net 从SQL Server数据库导出数据时如何格式化excel文件单元格,.net,export-to-excel,.net,Export To Excel,我正在将数据库中的一些数据导出到Excel中。虽然下面的代码工作正常,但我想知道如何操作标题、颜色以及单元格的外观 page.aspx <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" SelectCommand="SELECT ID, Number, Title, Name FRO

我正在将数据库中的一些数据导出到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()
Dim tw As New System.IO.StringWriter()
Dim hw作为新系统.Web.UI.HtmlTextWriter(tw)
Dim dgGrid作为新的DetailsView()
dgGrid.DataSource=SqlDataSource1
hw.WriteLine(“此处标题”)
dgGrid.HeaderStyle.Font.Bold=True
dgGrid.DataBind()
dgGrid.RenderControl(hw)
AddHeader(“内容处置”、“附件;文件名=reportoutput.xls”)
Response.ContentType=“应用程序/vnd.ms excel”
Me.EnableViewState=False
Response.Write(tw.ToString())
答复:End()

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


字符串样式=@“。文本{mso编号格式:\@;}”

下面的代码完成了这项工作(我想我绑定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
  <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