【问题标题】:Maintain text formatting during Excel file download在 Excel 文件下载期间保持文本格式
【发布时间】:2018-10-09 02:43:10
【问题描述】:

我在 MySQL 数据库中有一个“Product_Decription”列,它的数据类型是文本。 当用户通过 UI 更新列时,可能存在断线。当我的功能“下载所有产品描述”将数据导出到 Excel 文件时,断线消失。如何维护类似于数据库的文本格式?

下载功能如下:

Dim forDownloadGV As New GridView()
    forDownloadGV.DataSource = ""
    forDownloadGV.DataBind()

    Dim myConnection As MySqlConnection
    Dim myDataAdapter As MySqlDataAdapter
    Dim myDataset As DataSet

    Dim strSQL As String

    myConnection = New MySqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings("for_Read").ConnectionString)

    myConnection.Close()
    myConnection.Open()

    strSQL = "SELECT REPLACE(REPLACE(`Product_Desription`, char(13), '<br/>'), CHAR(10), '<br/>') as 'Description'from `Products`"
    myDataAdapter = New MySqlDataAdapter(strSQL, myConnection)

    myDataset = New DataSet()

    myDataAdapter.Fill(myDataset, "Products")

    forDownloadGV.DataSource = myDataset
    forDownloadGV.DataBind()

    Response.Clear()
    Response.Buffer = True
    Response.AddHeader("content-disposition", "attachment;filename=Products.xls")
    Response.Charset = ""
    Response.ContentType = "application/vnd.ms-excel"
    Dim sw As New StringWriter()
    Dim hw As New HtmlTextWriter(sw)

    For i As Integer = 0 To forDownloadGV.Rows.Count - 1
        'Apply text style to each Row
        forDownloadGV.Rows(i).Attributes.Add("class", "textmode")
    Next
    forDownloadGV.RenderControl(hw)

    'style to format numbers to string
    Dim style As String = "<style> .textmode{mso-number-format: \@;}</style>"
    Response.Write(style)
    Response.Output.Write(sw.ToString())
    Response.Flush()
    Response.End()

我已尝试替换为,但下载的 Excel 文件仍无法保持原始文本格式。

有什么帮助吗?是否应该在 Select 语句中进行格式化?

【问题讨论】:

    标签: excel vb.net export-to-excel


    【解决方案1】:

    如果它可以帮助任何人。下面将是解决方案。

        Dim forDownloadGV As New GridView()
        forDownloadGV.DataSource = ""
        forDownloadGV.DataBind()
    
        Dim myConnection As MySqlConnection
        Dim myDataAdapter As MySqlDataAdapter
        Dim myDataset As DataSet
    
        Dim strSQL As String
    
        myConnection = New MySqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings("for_Read").ConnectionString)
    
        myConnection.Close()
        myConnection.Open()
    
        strSQL = "SELECT REPLACE(REPLACE(`Product_Desription`, char(13), '<br>'), CHAR(10), '<br>') as 'Description'from `Products`"
        myDataAdapter = New MySqlDataAdapter(strSQL, myConnection)
    
        myDataset = New DataSet()
    
        myDataAdapter.Fill(myDataset, "Products")
    
        forDownloadGV.DataSource = myDataset
        forDownloadGV.DataBind()
    
        Response.Clear()
        Response.Buffer = True
        Response.AddHeader("content-disposition", "attachment;filename=Products.xls")
        Response.Charset = ""
        Response.ContentType = "application/vnd.ms-excel"
        Dim sw As New StringWriter()
        Dim hw As New HtmlTextWriter(sw)
    
        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)
    
        forDownloadGV.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()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-08-10
      • 2013-02-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多