我用这种方式把数据导出到excel里面,在一个新的页面里放入要导出的表单,然后在这个页面的最顶部放上下面的两句话:<%@ page language="java" contentType="application/.vnd.ms-excel;charset=UTF-8"%>
<%response.setContentType("application/.vnd.ms-excel");%>中间的表格我是这样显示的:<table border='1'><tr> <td>编号</td> <td>姓名</td> <td>email</td> </tr><c:foreach var="co" items="${ublist}"> <tr> <td> ${co.uid} </td> <td>${co.uname} </td> <td>${co.email} </td> </tr> </c:foreach>
但是导出文件后,打开文件后,导出的数据如果是全数字的并且长度大于11位,则用科学记数法显示在单元格里,也就是说,在导出的时候,只要是全数字的值,自动被转换为了数字型数据。要怎么使他保持原来的字符型,完全显示呢?
<%response.setContentType("application/.vnd.ms-excel");%>中间的表格我是这样显示的:<table border='1'><tr> <td>编号</td> <td>姓名</td> <td>email</td> </tr><c:foreach var="co" items="${ublist}"> <tr> <td> ${co.uid} </td> <td>${co.uname} </td> <td>${co.email} </td> </tr> </c:foreach>
但是导出文件后,打开文件后,导出的数据如果是全数字的并且长度大于11位,则用科学记数法显示在单元格里,也就是说,在导出的时候,只要是全数字的值,自动被转换为了数字型数据。要怎么使他保持原来的字符型,完全显示呢?
为了解决这个问题,我只能把查询的数据标识成文本型数据,下面是部份代码:
<%
'导出到excel
set rs = server.createobject("adodb.recordset")
sqlstr="select * from tabname where"&searhsql '查询语句
rs.open sqlstr,oconn,1,3
%>
<%set fs = server.createobject("script_ing.filesystemobject")
'存放文件
filename="info.xls"
temp=filename
filename=request.servervariables("appl_physical_path")&"\"+filename
'删除存在的excel文件
'if fs.fileexists(filename) then
'fs.deletefile(filename)
'end if
'-创建excel文件
set myfile = fs.createtextfile(filename,true)
dim strline,responsestr
strline=""
for each x in rs.fields
strline= strline & x.name & chr(9)
next
'将表的列名先写入excel
myfile.writeline strline
while not rs.eof
strline=""
for each x in rs.fields
'这里把数据标识成文本类型strline= strline & "="""&x.value &""""& chr(9)
next
'将表的数据写入excel
myfile.writeline strline
rs.movenext
wend
link="<a href=\" & temp & " title=将数据保存至eecel表中><font color=red><b>导出excel文件</b></font></a>"
response.write link
set myfile = nothing
set fs=nothing
rs.close
%>
而你的在你输出值里的前面多加一个空格就可以了,在excel中默认以空格开头的值为字符串而非数字
htp://zhanqi.me/archives/287
http://zhanqi.me/archives/287