<% if request("action")=1 then Response.ContentType="application/ms-excel" Response.AddHeader "content-disposi" />
乔山办公网我们一直在努力
您的位置:乔山办公网 > excel表格制作 > <em>asp</em> 把表格导出excel表<em>源码</em>

<em>asp</em> 把表格导出excel表<em>源码</em>

作者:乔山办公网日期:

返回目录:excel表格制作


<%@LANGUAGE="VBSCRIPT" CODEPAGE="936"%>
<%
if request("action")=1 then
Response.ContentType="application/ms-excel"
Response.AddHeader "content-disposition","attachment;filename=/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http:///1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
<style type="text/css">
table {
border-top:1px solid #003399;
border-left:1px solid #003399;
}
td {
border-right:1px solid #003399;
border-bottom:1px solid #003399;
}
thead {
background-color:#000066;
font-weight:bold;
padding:5px;
color:#FFFFFF;
}
</style>
<script language="javascript">
function tableToExcel(){
location.href='?action=1';
}
</script>
</head>

<body>
<input type="button" value="导出数据" onclick="tableToExcel()" />
<%
ConnStr="..."
Set conn = Server.CreateObject("ADODB.Connection")
conn.Open connstr

set rs = server.CreateObject("adodb.recordset")
rs.open "select top 10 * from [你的表名]",conn,1,1
if not (rs.eof and rs.bof) then
column = rs.fields.count
response.Write("<table cellpadding='0' cellspacing='0'>")
response.Write("<thead><td>序号</td>")
for each f in rs.fields
response.Write("<td>" & f.name & "</td>")
next
response.Write("</thead>")
for j = 1 to rs.recordcount
if j > 5 then '在第五条的时候隐藏数据,经过测试如果是display为none的数7a64e78988e69d83333据是不会导出来的
response.Write("<tr style='display:none'>")
else
response.Write("<tr>")
end if
response.Write("<td>" & j & "</td>")
for i = 0 to column - 1
response.Write("<td>" & rs(i) & "</td>")
next
response.Write("</tr>")
rs.movenext
next
response.Write("</table>")
end if
rs.close
set rs = nothing
conn.close
set conn = nothing
%>
</body>
</html>

一、用Excel对象,但要有Excel模板。速度略慢

<!--#include file="../conn.asp"-->
<%
dim s,sql,filename,fs,myfile,x
Set fs = server.CreateObject("scripting.filesystemobject")
'--假设你想让生成的EXCEL文件做如下的存放
filename = Server.MapPath("users.xls")
'--如果原来的EXCEL文件存在的话删除它
if fs.FileExists(filename) then
    fs.DeleteFile(filename)
end if
'--创建EXCEL文件
set myfile = fs.CreateTextFile(filename,true)
strSql = "select djh,bmmc,jihua,mubiao from scheme "
Set rstData =conn.execute(strSql)
if not rstData.EOF and not rstData.BOF then
    dim trLine,responsestr
    strLine=""
    For each x in rstData.fields
        strLine = strLine & x.name & chr(9)
    Next
'--将表的列e79fa5e98193e58685e5aeb9335名先写入EXCEL
    myfile.writeline strLine
    Do while Not rstData.EOF
        strLine=""
        for each x in rstData.Fields
            strLine = strLine & x.value & chr(9)
        next
        myfile.writeline strLine
        rstData.MoveNext
    loop
end if
Response.Write "生成EXCEL文件成功,点击<a href=""users.xls"" target=""_blank"">下载</a>!"
rstData.Close
set rstData = nothing
Conn.Close
Set Conn = nothing
%>

二、生成假的Excel,文件本身的格式是TXT,但扩展名使用的是XLS,用Excel也可以打开。速度快

<!--#include file="../conn.asp"-->
<%
Response.ContentType="application/vnd.ms-excel "    
%>
<table border="1"  style="border-collapse:collapse;">
    <tr align="center">
        <td>编号</td>
        <td>日期</td>
        <td>部门</td>
        <td>工作计划</td>
        <td>工作目标</td>
    </tr>
<%
set rs=server.createobject("adodb.recordset")
sql="select djh,rq,bmmc,jihua,mubiao from scheme where bmmc='"&session("bmmc")&"'"
rs.open sql,conn,1,1
do while not rs.eof
%>
    <tr>
        <td><%=rs("djh")%></td>
        <td><%=rs("rq")%></td>
        <td><%=rs("bmmc")%></td>
        <td><%=rs("jihua")%></td>
        <td><%=rs("mubiao")%></td>
    </tr>
<%
    rs.movenext
loop
%>
</table>
<%
rs.close
Conn.Close
Set Conn = nothing
%>

下面这个函数你可以放在类中,随时拿来用, 这是一个讲OleDbDataReader导出EXCEL的函数, 三个参数,第一个是页面,默636f7079e799bee5baa6336认是本页this, 第二个是OleDbDataReader,第三个是导出的文件名

public void CreateExcel(System.Web.UI.Page Page, OleDbDataReader dr, string FileName)
{
HttpResponse resp;

resp = Page.Response;
resp.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
resp.AppendHeader("Content-Disposition", "attachment;filename=" + FileName);
string colHeaders = "", ls_item = "";
int i = 0;

for (i = 0; i < dr.FieldCount - 1; i++)
{
colHeaders += dr.GetName(i).ToString() + "\t";
}
colHeaders += dr.GetName(i).ToString() + "\n";
//向HTTP输出流中写入取得的数据信息
resp.Write(colHeaders);
//逐行处理数据
while (dr.Read())
{
//在当前行中,逐列获得数据,数据之间以\t分割,结束时加回车符\n
for (i = 0; i < dr.FieldCount - 1; i++)
{
ls_item += dr[i].ToString() + "\t";
}

ls_item += dr[i].ToString() + "\n";
//当前行数据写入HTTP输出流,并且置空ls_item以便下行数据
resp.Write(ls_item);
ls_item = "";

}

//写缓冲区中的数据到HTTP头文件中
resp.End();

}

'给你个例子吧。保存为 asp文件运行看看。具体就在第一句。

<%Response.ContentType = "application/vnd.ms-excel" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<meta name="GENERATOR" content="Microsoft FrontPage 4.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<title>New Page 1</title>
</head>
<body>
<table border="0" width=100%>
  <tr>
    <td colspan="22" align=middle>PJ计画书</td>
  </tr>
  <tr>
    <td colspan="4">第版</td>
    <td colspan="18"></td>
  </tr>
</table>
<table border="1" width=100%>
  <tr>
    <td  colspan="3">案件No</td>
    <td  colspan="4"></td>
    <td  colspan="2">案件名</td>
    <td  colspan="13"></td>
  </tr>
  <tr>
    <td  colspan="3" >主管部门</td>
    <td  colspan="4" ></td>
    <td  colspan="2" >顾客</td>
    <td  colspan="13" ></td>
  </tr>
  <tr>
    <td  colspan="3" >PJ责任者</td>
    <td  colspan="4" ></td>
    <td  colspan="2" >営业担当</td>
    <td  colspan="4" ></td>
    <td  colspan="2" >纳期</td>
    <td  colspan="3" ></td>
    <td  colspan="2" >事业种别</td>
    <td  colspan="2" ></td>
  </tr>
  <tr rowspan="2">
    <td colspan="3"  rowspan="2">请负金额</td>
    <td  colspan="4"  rowspan="2"></td>
    <td  colspan="2"  rowspan="2">开発原価</td>
    <td  colspan="4"  rowspan="2"></td>
    <td  colspan="2" >社员费</td>
    <td  colspan="2" >外注费</td>
    <td  colspan="2" >部材费</td>
    <td  colspan="3">冲PJ番号</td>
    <tr><td  colspan="2" ></td>
        <td  colspan="2" ></td>
        <td  colspan="2" ></td>
        <td  colspan="3"></td>
    </tr>    
  </tr>
  <tr>
    <td colspan="3" >案件の概要</td>
    <td colspan="19" ></td>
  </tr>
  <tr>
    <td   colspan="22"></td>
  </tr>
  <tr>
    <td   colspan="2">SUB案件</td>
    <td  >工程</td>
    <td   colspan="2">责任者</td>
    <td   colspan="3">期间</td>
    <td   colspan="2">総工数(H)</td>
    <td   colspan="3">アウトプット</td>
    <td  >业务管理适用e79fa5e98193e4b893e5b19e338</td>
    <td  colspan="5">要员</td>
    <td >外注</td>
    <td  colspan="2">実绩终了日</td>
  </tr>
  <tr>
    <td  colspan="2"></td>
    <td ></td>
    <td  colspan="2"></td>
    <td  colspan="3"></td>
    <td  colspan="2"></td>
    <td  colspan="3"></td>
    <td ></td>
    <td  colspan="5"></td>
    <td ></td>
    <td  colspan="2"></td>
  </tr>
  <tr>
    <td  colspan="22">マイルストーン</td>
  </tr>
  <tr>
    <td  colspan="7">设计検证完了</td>
    <td  colspan="8">予定</td>
    <td  colspan="7">実绩</td>
  </tr>
 <tr>
    <td  colspan="7"></td>
    <td  colspan="8"></td>
    <td  colspan="7"></td>
  </tr>
  <tr>
    <td  colspan="3">规模见积の根拠</td>
    <td  colspan="19"></td>
  </tr>
 <tr>
    <td  colspan="7">品质目标</td>
    <td  colspan="8">目标</td>
    <td  colspan="7">実绩</td>
  </tr>
   <tr>
    <td  colspan="7">故障率</td>
    <td  colspan="8"></td>
    <td  colspan="7"></td>
  </tr>
  <tr>
    <td  colspan="7">不稼动率</td>
    <td  colspan="8"></td>
    <td  colspan="7"></td>
  </tr>
   <tr>
    <td  colspan="7">プログラム完成度</td>
    <td  colspan="8"></td>
    <td  colspan="7"></td>
  </tr>
   <tr>
    <td  colspan="7"></td>
    <td  colspan="8"></td>
    <td  colspan="7"></td>
  </tr>
  <tr>
    <td  colspan="4">品质目标设定の根拠</td>
    <td  colspan="18"></td>
  </tr>
  <tr>
    <td  colspan="5"></td>
    <td  colspan="3">予定日</td>
    <td  colspan="4"></td>
    <td  colspan="2">実施日</td>
    <td  colspan="3"></td>
    <td  colspan="2">承认者</td>
    <td  colspan="3"></td>
  </tr>
  <tr>
    <td  colspan="5">要求事项レビュー(记録)</td>
    <td  colspan="17"></td>
  </tr>
  <tr>
    <td  colspan="5">管理手顺</td>
    <td  colspan="17"></td>
  </tr>
  <tr>
    <td  colspan="5">PJ区分</td>
    <td  colspan="17"></td>
  </tr>
  <tr>
    <td  colspan="5">PJ入力情报</td>
    <td  colspan="17"></td>
  </tr>
 <tr>
    <td  colspan="5">纳入物品</td>
    <td  colspan="17"></td>
  </tr>
  <tr>
    <td  colspan="5">合否判断基准</td>
    <td  colspan="17"></td>
  </tr>
 <tr>
    <td  colspan="5">设计制造の出荷判定责任者、またはサービスの合否判断责任者</td>
    <td  colspan="17"></td>
  </tr>
 <tr>
    <td  colspan="5">PJ终了要件</td>
    <td  colspan="17"></td>
  </tr>
  <tr>
    <td  colspan="4">作业标准</td>
    <td  colspan="18"></td>
  </tr>
  <tr>
    <td  colspan="4">作业手顺</td>
    <td  colspan="18"></td>
  </tr>
  <tr>
    <td  colspan="4">事前教育</td>
    <td  colspan="18"></td>
  </tr>
  <tr>
    <td  colspan="4">外注管理方法</td>
    <td  colspan="18"></td>
  </tr>
  <tr>
    <td  colspan="4">顾客所有物</td>
    <td  colspan="18"></td>
  </tr>
  <tr>
    <td  colspan="4">准备机器及び调达计画</td>
    <td  colspan="18"></td>
  </tr>
  <tr>
    <td  colspan="4">PJ会议</td>
    <td  colspan="18"></td>
  </tr>
 <tr>
    <td  colspan="4">计画承认</td>
    <td  colspan="14"></td>
    <td  colspan="4"></td>
  </tr>
  <tr>
    <td  colspan="4">PJ终了承认</td>
    <td  colspan="5"></td>
    <td  colspan="2">予定日</td>
    <td  colspan="5"></td>
    <td  colspan="2">実绩日</td>
    <td  colspan="4"></td>
  </tr>
  <tr>
    <td  colspan="4">记事栏</td>
    <td  colspan="18"></td>
  </tr>
  <tr>
    <td  colspan="4">変更理由</td>
    <td  colspan="18"></td>
  </tr>
</table>
</body>
</html>

相关阅读

关键词不能为空
极力推荐

ppt怎么做_excel表格制作_office365_word文档_365办公网