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

发布于 2024-10-02 17:25:09 字数 1739 浏览 1 评论 0原文

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

页面.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" />

页面.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()

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()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

时光倒影 2024-10-09 17:25:09

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

字符串样式 = @" .text { mso-number-format:\@; } ";

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

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

治碍 2024-10-09 17:25:09

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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文