在Excel文件下载过程中保持文本格式

时间:2018-10-09 02:43:11

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

我在MySQL数据库中有一列“ Product_Decription”,其数据类型为Text。 当用户通过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语句中进行格式化?

1 个答案:

答案 0 :(得分:0)

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

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