在 Excel 文件下载期间保持文本格式
Maintain text formatting during Excel file download
我在 MySQL 数据库中有一列 "Product_Decription",它的数据类型是文本。
当用户通过 UI 更新列时,可能存在分界线。当我的函数 "Download all product description" 将数据导出到 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 语句中完成吗?
如果对任何人有帮助。下面将是解决方案。
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 <br/> back into <br/>'
Response.Flush()
Response.End()
我在 MySQL 数据库中有一列 "Product_Decription",它的数据类型是文本。 当用户通过 UI 更新列时,可能存在分界线。当我的函数 "Download all product description" 将数据导出到 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 语句中完成吗?
如果对任何人有帮助。下面将是解决方案。
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 <br/> back into <br/>'
Response.Flush()
Response.End()