asp.net把gridview的数据转到excel中最好的方法(代码)
展开全部
Dim resp As HttpResponse
resp = Page.Response
resp.Charset = "GB2312"
resp.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312")
resp.AppendHeader("Content-Disposition", "attachment;filename=" + fileName)
Dim colHeaders As String = ""
Dim ls_item As String = ""
'//定义表对象与行对象,同时用DataSet对其值进行初始化
Dim myRow As DataRow() = dt.Select() '可以类似dt.Select("id>10")之形式达到数据筛选目的
Dim i As Integer
Dim cl As Integer = dt.Columns.Count
For i = 0 To cl - 1
If i = (cl - 1) Then '最后一列,加n
colHeaders += dt.Columns(i).Caption.ToString() & vbCrLf
Else
colHeaders += dt.Columns(i).Caption.ToString() & vbTab
End If
Next i
resp.Write(colHeaders) ' 向HTTP输出流中写入取得的数据信息
Dim dr As DataRow
For Each dr In myRow
For i = 0 To cl - 1
If i = (cl - 1) Then '最后一列,加n
ls_item += dr(i).ToString() & vbCrLf
Else
ls_item += dr(i).ToString() & vbTab
End If
Next i
resp.Write(ls_item)
ls_item = ""
Next
resp.End()
这是不需要引用任何dll的导出!!
resp = Page.Response
resp.Charset = "GB2312"
resp.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312")
resp.AppendHeader("Content-Disposition", "attachment;filename=" + fileName)
Dim colHeaders As String = ""
Dim ls_item As String = ""
'//定义表对象与行对象,同时用DataSet对其值进行初始化
Dim myRow As DataRow() = dt.Select() '可以类似dt.Select("id>10")之形式达到数据筛选目的
Dim i As Integer
Dim cl As Integer = dt.Columns.Count
For i = 0 To cl - 1
If i = (cl - 1) Then '最后一列,加n
colHeaders += dt.Columns(i).Caption.ToString() & vbCrLf
Else
colHeaders += dt.Columns(i).Caption.ToString() & vbTab
End If
Next i
resp.Write(colHeaders) ' 向HTTP输出流中写入取得的数据信息
Dim dr As DataRow
For Each dr In myRow
For i = 0 To cl - 1
If i = (cl - 1) Then '最后一列,加n
ls_item += dr(i).ToString() & vbCrLf
Else
ls_item += dr(i).ToString() & vbTab
End If
Next i
resp.Write(ls_item)
ls_item = ""
Next
resp.End()
这是不需要引用任何dll的导出!!
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询