导出excel表格,可以不导入任何包,直接生产excel
如代码:html页面就一个简单的javascript脚本:
window.location = "<%=request.getContextPath()%>/servlet/TestServlet";
来跳转到servlet。
servlet中设置header ,直接拼装字符串table表格,并且可以赋样式打印到前台直接导出想要excel表格,不需要导入各种jar包,简单方便,可以设置简单样式。导出表格如图:
导出excel表格简单方法 - wangyue.123.com - moonstak
后台代码如下:
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setHeader("Content-type", "application/vnd.ms-excel; charset=gbk");
response.setHeader("Content-Disposition", "attachment; filename=export.xls");
//获取打印流
PrintWriter out = response.getWriter();
// 拼装table表格
String str = "<table border='1'>";
str+= "<tr><th style='width:400px;' bgcolor='#999999'>会员名称</th><th bgcolor='#999999'>操作日期</th><th bgcolor='#999999'>类型</th>";
str+= "<th bgcolor='#999999'>金额</th><th bgcolor='#999999'>支付方式</th><th bgcolor='#999999'>到款状态</th>";
str+= "<th bgcolor='#999999'>操作员</th></tr></table>";
out.print(str);
out.flush();
out.close();
}
如果有很长的数字,会自动变为科学计数法,试试这样行不行》
if($key1 == 'order_sn')
{ //sn一般是很多位数字
$data .= '<td style=\'mso-number-format:"\@";\'>' .$item. '</td>';
}
else
{
$data .= '<td>'.$item.'</td>';
}