Sub excel()
' ba()
On Error Resume Next
' Label1.Visible = True
Dim cou = Session("tb").Rows.count
If cou = 0 Then
Response.Write("<script>alert('不能进行下载!请选择要下载的数据字段。 ')</script>")
Return
End If
Dim i, rowstr
Dim dt As New DataTable()
For i = 0 To cou - 1
Dim r As DataRow = Session("tb").Rows(i)
rowstr = rowstr + " , " + r!val
Next
rowstr = Right(Trim(rowstr), Len(Trim(rowstr)) - 1)
Dim str = "select " + rowstr + " from bysxxview where (" + Session("cxstr") + ")"
Dim ws As New WebApp.localhost4.Service1()
ws.Credentials = System.Net.CredentialCache.DefaultCredentials
Dim ds As New DataSet()
ds.Merge(ws.getbysxx(str))
Dim dv As New DataView()
dv = ds.Tables(0).DefaultView
Dim xlApp As New Excel.Application()
Dim xlBook As Excel.Workbook
Dim xlSheet As Excel.Worksheet
Dim rowIndex, colIndex As Integer
rowIndex = 1
colIndex = 0
xlBook = xlApp.Workbooks().Add
xlSheet = xlBook.Worksheets("sheet1")
Dim Col1 As DataColumn
Dim Row1 As DataRow
'得到的表所有行,赋值给单元格
rowIndex = 0
colIndex = 0
rowIndex = rowIndex + 1
Dim cou1 = Session("tb").rows.count
For i = 0 To cou1 - 1
Dim r As DataRow = Session("tb").rows(i)
colIndex = colIndex + 1
xlApp.Cells(rowIndex, colIndex) = r!name ' Col1.ColumnName
xlApp.Cells(rowIndex, colIndex).Font.Name = "宋体"
xlApp.Cells(rowIndex, colIndex).Font.Bold = True
xlApp.Cells(rowIndex, colIndex).Font.size = 10
xlApp.Cells(rowIndex, colIndex).Borders.LineStyle = 1
Next
For Each Row1 In dv.Table.Rows
colIndex = 0
For Each Col1 In dv.Table.Columns
colIndex = colIndex + 1
xlApp.Cells(rowIndex + 1, colIndex) = "'" + Trim(Row1(Col1.ColumnName))
xlApp.Cells(rowIndex + 1, colIndex).Font.Name = "宋体"
xlApp.Cells(rowIndex + 1, colIndex).Font.size = 10
xlApp.Cells(rowIndex + 1, colIndex).Borders.LineStyle = 1
Next
rowIndex = rowIndex + 1
Next
xlApp.ActiveSheet.protect()
xlApp.Visible = True
xlBook.Saved = True
xlApp.UserControl = False
End Sub
Sub downfilefunction(ByVal sender As String)
Dim fi As System.IO.FileInfo = New System.IO.FileInfo(sender)
Response.Clear()
Response.ClearHeaders()
Response.Buffer = False
Response.ContentType = "application/octet-stream"
Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(fi.FullName, System.Text.Encoding.UTF8))
Response.AppendHeader("Content-Length", fi.Length.ToString())
Response.WriteFile(fi.FullName)
Response.Flush()
Response.End()
End Sub