从SQL Server数据库导出数据时如何格式化Excel文件单元格
我正在将数据库中的一些数据导出到 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我使用以下内容来设置数字格式。搜索 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:\@; } ";
下面的代码完成了这项工作(我认为我太早绑定了DetailsView。程序员如果可能的话请解释一下,谢谢)
在我的DetailsView中,我现在可以修改单元格背景颜色,大小等,例如:
我还可以修改文本,颜色等在我的代码后面,例如:
The following code does the work (I think I was binding the DetailsView too soon. Programmers explain if possible, thank u)
In my DetailsView, I now can modify cells background colour, size, etc, example:
I can also modify text, colour, etc in my code behind, example: