把数据从页面导出到EXCEL时发现数据格式不对
Private Function ConvertDtToTDF(ByVal dt As DataTable) As String
Dim dr As DataRow, ary() As Object, i As Integer
Dim iCol As Integer
' 输出列标题
For iCol = 0 To dt.Columns.Count - 1
Response.Write(dt.Columns(iCol).ToString & vbTab)
Next
Response.Write(vbCrLf)
'输出数据
For Each dr In dt.Rows
ary = dr.ItemArray
For i = 0 To UBound(ary)
Response.Write(ary(i).ToString & vbTab)
Next
Response.Write(vbCrLf)
Next
End Function
Private Sub toExcel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles del.Click
Response.Clear()
Response.Buffer = True
Response.Charset = "GB2312 "
Response.AppendHeader( "Content-Disposition ", "attachment;filename=FileName.xls ")
Response.ContentEncoding = System.Text.Encoding.GetEncoding( "GB2312 ")
'设置输出流为简体中文
Response.ContentType = "application/ms-excel "
'设置输出文件类型为excel文件。
Me.EnableViewState = False
Response.Write(ConvertDtToTDF(mytable))
Response.End()
End Sub
在datagrid绑定时小数位都正常显示,但是导出到excel后发现小数位都丢了,不知道怎么设置导出的格式?
------解决方案--------------------显式定义小数点的位数并转为string。
string.Format( "{0:###.##} ", 123.45).
------解决方案--------------------