惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

J
Java Code Geeks
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Blog — PlanetScale
Blog — PlanetScale
G
Google Developers Blog
Microsoft Security Blog
Microsoft Security Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
腾讯CDC
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Jina AI
Jina AI
雷峰网
雷峰网
T
Tailwind CSS Blog
爱范儿
爱范儿
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
酷 壳 – CoolShell
酷 壳 – CoolShell
大猫的无限游戏
大猫的无限游戏
月光博客
月光博客
博客园 - 司徒正美
I
InfoQ
Engineering at Meta
Engineering at Meta
Vercel News
Vercel News
小众软件
小众软件
U
Unit 42
Google DeepMind News
Google DeepMind News
D
DataBreaches.Net

博客园 - 李昀璟

Unit Tests for ASP.NET MVC application that uses resources in code behind. IIS 不能运行asp.net Useful Expressions - Business Correspondence How to Plan a Meeting Useful Expressions for Business Interaction Useful Expressions for Business Language 使用IP或机器名而不是localhost访问DotNetNuke DotNetNuke升级中遇到的问题 DNN端口的问题 升级DotNetNuke DotNetNuke的升级路径 设置为自动启动的WindowService没有开机启动 检测是否连网 C# WinForm 边框阴影窗体 PostSubmitter~在WEB应用程序以外的其他程序里提交Web请求的类 Asp.Net部署问题 MSDTC的折磨 - 李昀璟 - 博客园 常用缩写 日本語文法勉強
Export to Excel, Word with css (Cascading Style Sheets)
李昀璟 · 2012-09-11 · via 博客园 - 李昀璟

有时,我们需要导出数据到EXCEL 或word,并且包含CSS样式。

我们可以把这个CSS样式设置成绝对路径,这样下载的文件自然也会带有样式。

把link到页面的CSS的href= "../style.css " 改成绝对路径,比如 href= "http://www.csdn.net/style.css "  

我们也可以把CSS样式文件做为一个流刷到输出流里。下面是一个简单的例子: 

.aspx page

Select File Format 

<asp:DropDownList id="ddlFile" runat="server">

  <asp:ListItem Value=".xls">ms-excel</asp:ListItem>

  <asp:ListItem Value=".doc">msword</asp:ListItem>

</asp:DropDownList><br>

<asp:Button Visible="True" id="btnExport" runat="server" Text="Export to File"></asp:Button><br>

<asp:Label id="Label1" runat="server"></asp:Label>

Stylesheet used (CSSFile.css)

#div1 td {

          font-familyVerdana, Arial, Helvetica, sans-serif;

          font-size11px;

          }

#div1 table {

          font-familyVerdana, Arial, Helvetica, sans-serif;

          font-size11px;

          border-top-width1px;

          border-right-width1px;

          border-bottom-width1px;

          border-left-width1px;

          }

#div1 th {

          font-familyVerdana, Arial, Helvetica, sans-serif;

          font-size11px;

          color:#ffffff;

          background-color#316ac5;

          }

.td1 {

          font-familyVerdana, Arial, Helvetica, sans-serif;

          font-size12px;

          color:red;

          background-colorLightSteelBlue;

}

Code-Behind page

    Private Sub Page_Load(ByVal sender As System.ObjectByVal e As System.EventArgsHandlesMyBase.Load

        Dim sb As New System.Text.StringBuilder

        sb.Append("<div id='div1'><table border='1' cellpadding='0' cellspacing='0' width='99%' align='center'>")

        sb.Append("<tr><th height='20px' colspan='2'>Reports</th></tr><tr><td colspan='2'>&nbsp;</td></tr>")

        sb.Append("<tr><td colspan='2' class='td1'><b>1</b></td></tr>")

        sb.Append("<tr><td><b>User Name</b></td><td>&nbsp;Name One</td></tr>")

        sb.Append("<tr><td><b>Location</b></td><td>&nbsp;Mumbai</td></tr>")

        sb.Append("<tr><td><b>Email</b></td><td>&nbsp;name.one@sss.com</td></tr>")

        sb.Append("<tr><td colspan='2'>&nbsp;</td></tr>")

        sb.Append("<tr><td colspan='2' class='td1'><b>2</b></td></tr>")

        sb.Append("<tr><td><b>User Name</b></td><td>&nbsp;Name Two</td></tr>")

        sb.Append("<tr><td><b>Location</b></td><td>&nbsp;Delhi</td></tr>")

        sb.Append("<tr><td><b>Email</b></td><td>&nbsp;name.two@sss.com</td></tr>")

        sb.Append("<tr><td colspan='2'>&nbsp;</td></tr>")

        sb.Append("<tr><td colspan='2' class='td1'><b>3</b></td></tr>")

        sb.Append("<tr><td><b>User Name</b></td><td>&nbsp;Name Three</td></tr>")

        sb.Append("<tr><td><b>Location</b></td><td>&nbsp;Chennai</td></tr>")

        sb.Append("<tr><td><b>Email</b></td><td>&nbsp;name.three@sss.com</td></tr>")

        sb.Append("</table></div>")

        Label1.Text = sb.ToString()

        sb.Remove(0, sb.Length)

    End Sub

Button Click event used for exporting to required format

    Private Sub btnExport_Click(ByVal sender As System.ObjectByVal e As System.EventArgs)Handles btnExport.Click

        Response.Clear()

        Response.Charset = ""

        Response.ContentEncoding = System.Text.Encoding.UTF8

        Response.Cache.SetCacheability(HttpCacheability.NoCache)

        Response.ContentType = "application/" & ddlFile.SelectedItem.Text & ddlFile.SelectedValue

        Response.AddHeader("content-disposition""attachment;filename=" & "Report" & ddlFile.SelectedValue)

        Dim sw As New System.IO.StringWriter

        Dim htw As New HtmlTextWriter(sw)

        Label1.RenderControl(htw)

        'Appendg CSS file

        Dim fi As FileInfo = New FileInfo(Server.MapPath("scripts/CSSFile.css"))

        Dim sb As New System.Text.StringBuilder

        Dim sr As StreamReader = fi.OpenText()

        Do While sr.Peek() >= 0

            sb.Append(sr.ReadLine())

        Loop

        sr.Close()

        Response.Write("<html><head><style type='text/css'>" & sb.ToString() & "</style><head>"& sw.ToString() & "</html>")

        sw = Nothing

        htw = Nothing

        Response.Flush()

        Response.End()

    End Sub