• create Excel file


    http://www.codepal.co.uk/show/Line_breaks_lost_and_br_tags_show_when_exporting_to_Excel_file

     Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
    
        Dim dt As New DataTable
        dt.Columns.Add(New DataColumn("Text1", GetType(System.String)))
    
        Dim dr As DataRow = dt.NewRow
        dr.Item("Text1") = String.Format("This is line one{0}This is line two{0}This is line three", "<br/>")
        dt.Rows.Add(dr)
    
        Response.Clear()
        Response.AddHeader("content-disposition", "attachment;filename=exported.xls")
        Response.AddHeader("Pragma", "public")
        Response.AddHeader("Cache-Control", "max-age=0")
        Response.ContentType = "application/vnd.ms-excel"
        Response.ContentEncoding = System.Text.Encoding.UTF8
    
        Dim gvEx As New GridView
        gvEx.AutoGenerateColumns = True
        gvEx.AllowSorting = False
        gvEx.AllowPaging = False
        Dim sw As New System.IO.StringWriter
        Dim hw As New System.Web.UI.HtmlTextWriter(sw)
        gvEx.DataSource = dt
        gvEx.DataBind()
        hw.AddAttribute("xmlns:x", "urn:schemas-microsoft-com:office:excel") ' optional'
        hw.RenderBeginTag(HtmlTextWriterTag.Html)
        hw.RenderBeginTag(HtmlTextWriterTag.Head)
        hw.RenderBeginTag(HtmlTextWriterTag.Style)
        hw.Write("br {mso-data-placement:same-cell;}")
        hw.RenderEndTag() ' /Style'
        hw.RenderEndTag() ' /Head'
        hw.RenderBeginTag(HtmlTextWriterTag.Body)
        gvEx.RenderControl(hw)
        hw.RenderEndTag() ' /Body'
        hw.RenderEndTag() ' /Html'
        Response.Write(HttpUtility.HtmlDecode(sw.ToString))    ' turns &lt;br/&gt; back into <br/>'
        Response.Flush()
        Response.End()
    
    End Sub
  • 相关阅读:
    SQL的join使用图解
    归并排序的JAVA实现
    java 快速排序 时间复杂度 空间复杂度 稳定性
    哈希表(HashMap)分析及实现(JAVA)
    外部排序
    海量数据面试题整理
    《CSS3秘籍》第6、7章
    《CSS3秘籍》第3-5章
    《CSS3秘籍》第1、2章
    《HTML5与CSS3基础教程》第11、14-16、18章
  • 原文地址:https://www.cnblogs.com/dufu/p/6627860.html
Copyright © 2020-2023  润新知