導出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>';
}