后台写:
public
string
url
url
=
sqlhelper.executescalar(sqlhelper.sqlconnectionstring,
commandtype.text,
"select
vc_videourl
from
tb_video
where
i_videoid="+request["id"],null).tostring()
前台按钮单击事件BtnExcel_Click2,应该看的懂吧?,自己加上错误处理机制就行了Protected Sub BtnExcel_Click2(ByVal sender As Object, ByVal e As System.EventArgs)
'sqlstrShare 全局查询的SQL语句,导出所有部门备品
Dim sqlstrShare As String = “SQL查询语句”
Dim sqlconn As New SqlConnection
Dim cmdString As String = sqlstrShare
sqlconn.ConnectionString = connclass.ConnStr
Dim Da As SqlDataAdapter = New SqlDataAdapter(cmdString, sqlconn)
Dim ds As System.Data.DataSet = New Data.DataSet()
Da.Fill(ds, "Data")
DataToExcel(ds, Me.Response, "SPARTREPORT")
ds.Dispose()
Da.Dispose()
End Sub
Sub DataToExcel(ByVal ds As DataSet, ByVal response As HttpResponse, ByVal fileName As String)
Dim str As String
Dim dt As System.Data.DataTable
dt = ds.Tables(0)
For i As Integer = 0 To dt.Columns.Count - 1
dt.Columns.Item(i).ColumnName = connclass.ReplaceStr(dt.Columns.Item(i).ColumnName)
Next
str = Format(Now(), "_yyyyMMdd") &"_" &Now.ToString("HHmmss")
fileName = fileName + str
response.Clear()
Dim myGrid As GridView = New GridView
myGrid.DataSource = dt
myGrid.DataBind()
HttpContext.Current.Response.Charset = "gb2312"
Dim oSW As System.IO.StringWriter = New System.IO.StringWriter()
Dim oHW As HtmlTextWriter = New HtmlTextWriter(oSW)
myGrid.RenderControl(oHW)
response.Write(oSW.ToString())
response.ContentType = "application/ms-excel"
response.AppendHeader("Content-Disposition", "attachmentfilename=" + fileName + ".xls")
response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312")
response.Flush()
response.Close()
End Sub
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)