查询后的数据保存到excel,文件保存到客户端

lovefootball 2003-07-14 09:59:39
我用的是代码前置,怎么把查询后的数据导入Excel,并且保存到本地(使用者的机器客户端就保存到客户端,服务器就保存到服务器)
能给一份详细的代码么,谢谢了
...全文
66 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
yinya 2003-07-15
  • 打赏
  • 举报
回复
Dim ws As New WebApp.localhost4.Service1()
ds.Merge(ws.getbysxx(str))

这是填充数据集啊,因为我的查询是将条件传入WEBSERVER查询,将结果反回,
GETBYSXX(STR)就是我的查询函数,返回一个数据集,并填充一个DATAVIEW---DV
你只需把它改成你的数据源就行了,还有

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
这个是产生表头的,因为我下载的时候是让用户自定义下载字段的,用Session("tb")保存用户选择的字段名

孟子E章 2003-07-15
  • 打赏
  • 举报
回复
http://xml.sz.luohuedu.net/xml/Content.asp
lovefootball 2003-07-15
  • 打赏
  • 举报
回复
自己up
lovefootball 2003-07-15
  • 打赏
  • 举报
回复
好的,谢谢
greystar 2003-07-14
  • 打赏
  • 举报
回复
http://expert.csdn.net/Expert/topic/1883/1883370.xml?temp=.4803583
lovefootball 2003-07-14
  • 打赏
  • 举报
回复
Dim ws As New WebApp.localhost4.Service1()
ds.Merge(ws.getbysxx(str))
这两句是干嘛用的?谢谢
lovefootball 2003-07-14
  • 打赏
  • 举报
回复
谢谢 yinya(胖胖)
我试一下
abiho 2003-07-14
  • 打赏
  • 举报
回复
直接写成.xls文件,列之间用tab分开,行用换行!就是excel文件了!
要是需要排版,另外再写排版的程序,稍微有点麻烦
yinya 2003-07-14
  • 打赏
  • 举报
回复
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

Dim mm = Server.MapPath(".") + "\temp\" + Session("username") + ".xls" '服务器保存地址 ")
xlApp.ActiveWorkbook.SaveCopyAs(mm)
'xlApp.ActiveWindow.SelectedSheets.PrintOut(mm, Copies:=1, Collate:=True)
Session("mm") = "temp\" + Session("username") + ".xls"
Session("m2") = Server.MapPath(".") + "\temp\" + Session("username") + ".xls"
xlApp.Quit()

downfilefunction(Session("m2"))

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


前面是生成EXCEL文件的,后面是下载,你研究一下吗

16,549

社区成员

发帖
与我相关
我的任务
社区描述
VB技术相关讨论,主要为经典vb,即VB6.0
社区管理员
  • VB.NET
  • 水哥阿乐
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧