0

I Have export Gridview data to excel, here's my code :

 Protected Sub btnExport_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnExport.Click
        Try
            Dim dt As New DataTable
            If CReversal.SearchReversal2(txtAccount.Text, txtCustName.Text, txtAmount.Text, dropResponse.SelectedValue.ToString, txtRefNo.Text, txtDate.Text) Then
                dt = CReversal.DT
            Else
                eMessage("System failure: ", CReversal.eMsg)
            End If
            Dim DataGrd As New DataGrid()
            DataGrd.DataSource = dt.DefaultView
            DataGrd.DataBind()

            Dim attachment As String
            attachment = "attachment; filename=Inquiry_Report" & Format(Now, "ddMMMyyyy") & ".xls"
            Response.Buffer = True
            Response.ClearContent()
            Response.ClearHeaders()
            Response.AddHeader("content-disposition", attachment)
            Response.ContentType = "application/ms-excel"
            Dim sw As New StringWriter()
            Dim htw As New HtmlTextWriter(sw)
            DataGrd.RenderControl(htw)
            Response.Write(sw.ToString())
            Response.End()
        Catch ex As Exception
            eMessage("Export Data failure: ", ex.ToString())
        End Try

    End Sub

the export process is run as i want. but when i open the excel file, i got some strings number character become like this :

enter image description here but actually the datagridvie display is like this : should

is it possible to format the string number character when we want to export to excel programatically? thk you

4

1 回答 1

1

您的方法的问题是,您没有编写“真实”的 Excel 数据。这是一个 html 表格,可以通过 excel 读取,但您并不能真正影响 excel 读取这种混乱的方式。像 EPPLus 这样的库在编写 excel 文件方面要好得多,并且可以将您的头痛减少几个数量级。它是免费的,即使在商业环境中也是如此,所以使用它时没有问题。只是更好的结果

附带说明:Epplus 支持“LoadFromDataTable”方法——运气好的话,你所需要的一切......

于 2013-05-30T09:49:45.427 回答