导出到EXCEL,第一个数字为0会被忽略掉,请问要如何设置格式
第一个数字为0会自动被EXCEL忽略掉,或者数据太长,也不能完整显示。请问要如何设置,谢谢!
<%@ page contentType="application/vnd.ms-excel; charset=gb2312" %>
<%@ page import="java.sql.*"%>
<%
response.setHeader("Content-Disposition", "attachment;filename=H1_3.xls");
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>生成</title>
</head>
<body>
<table border="1">
<TR>
<td class="xlsText"><%out.print("F203340100000");%></td>
<td class="xlsText"><%out.print("F203340300000");%></td>
<td class="xlsText"><%out.print("F203340400000");%></td>
<td><%out.print("AA");%></td>
<td><%out.print("MY");%></td>
<td><%out.print("YB");%></td>
<td><%out.print("BMY");%></td>
</TR>
</table>
<%
Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
String url="jdbc:oracle:thin:@10.236.80.253:1521:chdb";
//orcl
String user="ch";
String password="ch";
Connection conn= DriverManager.getConnection(url,user,password);
Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
String sql="select * from vw_h1_3@c9218";
ResultSet rs=stmt.executeQuery(sql);
while(rs.next()) {%>
<table border="1">
<TR>
<td class="xlsText"><%=rs.getString(1)%> </td>
<td class="xlsText"><%=rs.getString(2)%> </td>
<td class="xlsText"><%=rs.getString(3)%> </td>
<td><%=rs.getString(4)%> </td>
<td><%=rs.getString(5)%> </td>
<td><%=rs.getString(6)%> </td>
<td><%=rs.getString(7)%> </td>
</TR>
</table>
<%}%>
<%rs.close();
stmt.close();
conn.close();
%>
</body>
</html>
------解决方案--------------------
解决思路:在Excel中作一个包含有"012457890"的内容,设定单元格的显示方式,然后保存成Html的文件,在查看源代码. 发现在 CSS格式定义中有:td{mso-number-format:"\@";}.这样问题就容易解决了. 实际伤得代码只是比常见的处理方式多了一 句而已.
LZ加上这个就可以了
<style type="text/css">
td {
mso-number-format:'\@';
}
</style>