站长资讯网
最全最丰富的资讯网站

ASP页面将数据库中检索数据生成到本地报表的解决方案

–
Web系统的开发过程中,我们经常会碰到一些这样的需求。客户要求将DB检索出的数据导出,并下载到本地的Excel文件中,做成一定样式的报表。这里我想通过一个实际的例子,讲讲我们是如何在ASP中实现这一需求的。 

 

  

一、下载处理中,CSV文件生成和Excel模板拷贝的实现代码如下:

 1、上图为数据检索页面,检索的数据一览显示。并且在该页面中将检索用的sql文保存在
Session(“strEXCELSQL”)中。

2、点击Excel export按钮,提交到下面的下载页面。

3、现在页面重新进行数据库检索,生成CSV数据文件。代码如下:
<%
‘变量定义
Dim fso, Fld, delf, tmpf, fmd, today

‘创建文件处理对象
set fso = CreateObject(“Scripting.FileSystemObject”)
‘对应web服务器csv目录
set Fld = fso.GetFolder(Server.MapPath(“csv”))
set delf = Fld.Files
‘将web服务器csv目录中当天以前的所有文件删除。
For Each tmpf in delf
 fmd = tmpf.DateLastModified
 today = date()
 if(year(fmd) < year(today)) then
  tmpf.delete
 elseif(month(fmd) < month(today)) then
  tmpf.delete
 elseif(day(fmd) < day(today)) then
  tmpf.delete
 end if
next
‘对应web服务器Excel下载目录
set Fld = fso.GetFolder(Server.MapPath(“excel_download”))
set delf = Fld.Files
‘将web服务器Excel下载目录中当天以前的所有文件删除。
For Each tmpf in delf
 fmd = tmpf.DateLastModified
 today = date()
 if(year(fmd) < year(today)) then
  tmpf.delete
 elseif(month(fmd) < month(today)) then
  tmpf.delete
 elseif(day(fmd) < day(today)) then
  tmpf.delete
 end if
next

‘变量定义
Dim rsPtn, strTitle, PtnNo
Dim rsData
Dim rsData_numRows
Dim csvFilePath, strFileNm
Dim strSelectSQL
Dim tempof, f1, csvfile
Dim csvLine, lngLineCnt
Dim i
Dim nowTime, strH, strM, strS
Dim strDateBuf
Dim strCol
Dim strOwner

‘当前时间的取得
nowTime = Time()
strH = Hour(nowTime)
if(0 <= strH and strH <= 9) then
 strH = “0” & CStr(strH)
end if
strM = Minute(nowTime)
if(0 <= strM and strM <= 9) then
 strM = “0” & CStr(strM)
end if
strS = Second(nowTime)
if(0 <= strS and strS <= 9) then
 strS = “0” & CStr(strS)
end if

‘检索页面设定的SQL文取得
strSelectSQL = Session(“strEXCELSQL”)
‘数据库访问
Set rsData = Server.CreateObject(“ADODB.Recordset”)
rsData.ActiveConnection = CONNECT_STRING
rsData.Source = strSelectSQL
rsData.CursorType = 0
rsData.CursorLocation = 2
rsData.LockType = 1
rsData.Open()
rsData_numRows = 0
‘CSV文件名生成‘Excel_ +用户ID+时+分+秒
strFileNm = “Excel_” & Session.SessionID & strH & strM & strS
csvFilePath = Server.MapPath(“csv”) & “” & strFileNm & “.csv”
‘文件存在性的判断
If(fso.FileExists(csvFilePath) = false) then
 ‘打开文件
  Set csvfile = fso.OpentextFile(csvFilePath, 2, True)
 lngLineCnt = 0
 ‘循环数据集,将数据写入CSV文件
 Do While rsData.EOF = false
  ‘Excel 最大行数超过
  If lngLineCnt > 65535 Then Exit Do
  csvLine = “”
  For i = 0 To rsData.Fields.Count-1
   csvLine = csvLine & rsData.Fields.Item(i).Value & “,”
  Next
  csvLine = Left(csvLine, len(csvLine)-1)
  csvfile.WriteLine( “””*””,” & csvLine )
  lngLineCnt = lngLineCnt + 1
  rsData.MoveNext
 Loop
End if
‘Excel模板文件生成
dim f2
set f2 = fso.getfile(Server.MapPath(“templatestemplate1.xls”))
if(fso.FileExists(Server.MapPath(“excel_download”) & “” & strFileNm & “.xls”) = false) then
 ‘文件从web服务器的templates目录拷贝到excel_download目录
 ‘文件名与CSV文件名相同。
 f2.copy(Server.MapPath(“excel_download”) & “” & strFileNm & “.xls”)
end if

rsData.Close()
Set rsData = Nothing
%>

 

 

分享到: 更多

赞(0)
分享到: 更多 (0)

网站地图   沪ICP备18035694号-2    沪公网安备31011702889846号