方法一:
<%
' ============================================
' 格式化时间(显示)
' 参数:n_Flag
' 1:"yyyy-mm-dd hh:mm:ss"
' 2:"yyyy-mm-dd"
' 3:"hh:mm:ss"
' 4:"yyyy年mm月dd日"
' 5:"yyyymmdd"
' 6:"mm-dd"
' ============================================
Function Format_Time(s_Time, n_Flag)
Dim y, m, d, h, mi, s
Format_Time = ""
If IsDate(s_Time) = False Then Exit Function
y = cstr(year(s_Time))
m = cstr(month(s_Time))
If len(m) = 1 Then m = "0" & m
d = cstr(day(s_Time))
If len(d) = 1 Then d = "0" & d
h = cstr(hour(s_Time))
If len(h) = 1 Then h = "0" & h
mi = cstr(minute(s_Time))
If len(mi) = 1 Then mi = "0" & mi
&n ......
最近做一个用asp做的网站后台的数据导出功能时,google,baidu了很多,但是多数都是转载,代码大概都一样,但是就是报错,最后有人说通过设置MIME类型和建立模板,最后整理,测试问题终于解决.关键的地方有以下几点:
1):MIME类型要设置正确,如要把DB中表(或表中指定字段)的数据导出成exel格式时,头部的MEMI类型的设置是:
<%
Response.Expires = 0
Response.ContentType="application/vnd.ms-excel"
Response.AddHeader "content-disposition","inline; filename=exportDataName.xls"
%>
尾部的MIME类型的设置:
<%
Response.ContentType="application/vnd.ms-excel"
Response.AddHeader "content-disposition","attachment filename=exportDataName.xls"
rs.close
set rs=nothing
%>
注::exportDataName为导出exel时要保存的文件名,自己可以自定义,比如以yyyy-MM-dd形式保存,以上头部信息的设置,即MIME类型的设置和JAVA代码中的文件下载设置一样,其它的类型如法炮制.
2)模板的字段设置一定要和从表中读出的要迭代的一致.
xls.asp代码如下:
<!--#include file="../Inc/Conn.asp"-->
<!--#include file="../Inc/Function.asp"-->
<%
......
Excel导出函数
<%
Sub ExportToExcel
Response.ContentType = "application/vnd.ms-Excel"
Response.AddHeader "Content-Disposition", "attachment;Filename=Results.xls"
Response.Write "<body>"
Response.Write "<table border=1>"
Call WriteTableData
Response.Write "</table>"
Response.Write "</body>"
Response.Write "</html>"
End Sub
%>
Excel导入数据库
<%
dim FileName
FileName="Excel.xls" '取得文件名,来自项目经理的指定,路径固定在某个虚拟路径中
Dim conn, rs
set conn=CreateObject("ADODB.connection")
conn.Open "Driver={Microsoft Excel Driver (*.xls)};" & _
"DriverId=790;" & _
"Dbq=" & Server.mappath(""&FileName&"") & ......
Excel导出函数
<%
Sub ExportToExcel
Response.ContentType = "application/vnd.ms-Excel"
Response.AddHeader "Content-Disposition", "attachment;Filename=Results.xls"
Response.Write "<body>"
Response.Write "<table border=1>"
Call WriteTableData
Response.Write "</table>"
Response.Write "</body>"
Response.Write "</html>"
End Sub
%>
Excel导入数据库
<%
dim FileName
FileName="Excel.xls" '取得文件名,来自项目经理的指定,路径固定在某个虚拟路径中
Dim conn, rs
set conn=CreateObject("ADODB.connection")
conn.Open "Driver={Microsoft Excel Driver (*.xls)};" & _
"DriverId=790;" & _
"Dbq=" & Server.mappath(""&FileName&"") & ......
<%
function FSOFileDel(filename)
Dim objFSO,objCountFile,FiletempData
On Error Resume Next
Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
Set objCountFile =objFSO.DeleteFile(Server.MapPath(filename),true)
objCountFile.Close
Set objCountFile=Nothing
Set objFSO = Nothing
End Function
dim action,files
action=trim(request("action"))
files=trim(request("files"))
select case action
case "add"
call add(files)
case else
call default()
end select
%>
<%sub default()%>
<form action="?action=add" method="post" id="form1" name="form1">
<table border="0" cellspacing="0" cellpadding="0">
<tr class="title2">
<td colspan="2">批量上传</td>
</tr>
<tr>
<td>1.上传Excle文件 </td>
<td><input name="files" type="hidden" id="files" />
<iframe frameborder="0" scrolling="no" height="25" width="400" src="upload.asp?action=add"></iframe> ......
<%
function FSOFileDel(filename)
Dim objFSO,objCountFile,FiletempData
On Error Resume Next
Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
Set objCountFile =objFSO.DeleteFile(Server.MapPath(filename),true)
objCountFile.Close
Set objCountFile=Nothing
Set objFSO = Nothing
End Function
dim action,files
action=trim(request("action"))
files=trim(request("files"))
select case action
case "add"
call add(files)
case else
call default()
end select
%>
<%sub default()%>
<form action="?action=add" method="post" id="form1" name="form1">
<table border="0" cellspacing="0" cellpadding="0">
<tr class="title2">
<td colspan="2">批量上传</td>
</tr>
<tr>
<td>1.上传Excle文件 </td>
<td><input name="files" type="hidden" id="files" />
<iframe frameborder="0" scrolling="no" height="25" width="400" src="upload.asp?action=add"></iframe> ......
是Office的OWC版本问题。2000,2002XP,2003各不相同,加入Office的版本检查即可,完整代码如下:
<script language="javascript">
function exportExcel(atblData,dirPath)
{
//alert(dirPath);
if (typeof(atblData)=="undefined"||typeof(atblData)=="unknow")
{
alert("没有数据可以导出!");
return;
}
var mysheet;
var off2000=false;
var off2002xp=false;
var off2003=false;
//office2003
try {
mysheet=new ActiveXObject("OWC11.Spreadsheet");
off2003=true;
}
& ......
一、使用OWC
什么是OWC?
OWC是Office Web Compent的缩写,即Microsoft的Office Web组件,它为在Web中绘制图形提供了灵活的同时也是最基本的机制。在一个intranet环境中,如果可以假设客户机上存在特定的浏览器和一些功能强大的软件(如IE5和Office 2000),那么就有能力利用Office Web组件提供一个交互式图形开发环境。这种模式下,客户端工作站将在整个任务中分担很大的比重。
<%Option Explicit
Class ExcelGen
Private objSpreadsheet
Private iColOffset
Private iRowOffset
Sub Class_Initialize()
Set objSpreadsheet = Server.CreateObject("OWC.Spreadsheet")
iRowOffset = 2
iColOffset = 2
End Sub
Sub Class_Terminate()
Set objSpreadsheet = Nothing 'Clean up
End Sub
Public Property Let ColumnOffset(iColOff)
If iColOff > 0 then
iColOffset = iColOff
Else
iColOffset = 2
End If
End Property
Public Property Let RowOffset(iRowOff)
If iRowOff > 0 then
iRowOffset = iRowOff
Else
iRowOffset = 2
End If
End Property Sub GenerateWorksheet(objRS)
'Populates the Excel w ......