导出CSV时,Excel中的值变为指数,有没有办法可以通过C#.Net Code停止它们?

时间:2017-11-22 19:24:06

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

我使用下面的代码,用于将数据表导出为CSV,但是在下载时我看到指数值,如何从代码中阻止它们,因为我知道这个转换是由MS-EXCEL完成的,

有什么方法可以显示确切的数字吗? 代码如下:

      '--------Download In CSV--------------------------------------------

        If dtResult.Rows.Count > 0 Then
            Response.ContentType = "text/csv"
            Response.AddHeader("Content-Disposition", "attachment;filename=report.csv")
            Response.AddHeader("Pragma", "no-cache")
            Response.AddHeader("Cache-Control", "no-cache")

            Dim myData As Byte() = csvBytesWriter(dtResult)
            Response.BinaryWrite(myData)
            Response.Flush()
            Response.SuppressContent = True
            HttpContext.Current.ApplicationInstance.CompleteRequest()

' -------- CSV Writer

Function csvBytesWriter(ByRef dTable As DataTable) As Byte()

    '--------Columns Name--------------------------------------------

    Dim sb As StringBuilder = New StringBuilder()
    Dim intClmn As Integer = dTable.Columns.Count

    Dim i As Integer = 0
    For i = 0 To intClmn - 1 Step i + 1
        sb.Append("""" + dTable.Columns(i).ColumnName.ToString() + """")
        If i = intClmn - 1 Then
            sb.Append(" ")
        Else
            sb.Append(",")
        End If
    Next
    sb.Append(vbNewLine)

    '--------Data By  Columns---------------------------------------------------------------------------

    Dim row As DataRow
    For Each row In dTable.Rows

        Dim ir As Integer = 0
        For ir = 0 To intClmn - 1 Step ir + 1
            sb.Append("""" + row(ir).ToString().Replace("""", """""") + """")
            If ir = intClmn - 1 Then
                sb.Append(" ")
            Else
                sb.Append(",")
            End If

        Next
        sb.Append(vbNewLine)
    Next

    Return System.Text.Encoding.UTF8.GetBytes(sb.ToString)

End Function

1 个答案:

答案 0 :(得分:0)

使用Gembox Spreadsheet 它会完成工作