java 导出excel文件

思路:jsp\html样式文件直接保存成excel格式的xls文件。注(文件的前后加上了Excel文件的头尾信息

<%@ page contentType="application/vnd.ms-excel;charset=GBK"%>    《《《《《=============================头文件格式
<%
response.setHeader("Content-disposition","inline; filename=test.xls");
out.clear();
%>

 

private static String AddExcelHead(){ 
        StringBuffer sb = new StringBuffer();
        sb.append("<html xmlns:x=\"urn:schemas-microsoft-com:office:excel\">");
        sb.append(" <head>");
        sb.append(" <!--[if gte mso 9]><xml>");
        sb.append("<x:ExcelWorkbook>");
        sb.append("<x:ExcelWorksheets>");
        sb.append("<x:ExcelWorksheet>");
        sb.append("<x:Name></x:Name>"); 
        sb.append("<x:WorksheetOptions>");
        sb.append("<x:Print>");            
        sb.append("<x:ValidPrinterInfo />");
        sb.append(" </x:Print>"); 
        sb.append("</x:WorksheetOptions>");
        sb.append("</x:ExcelWorksheet>");
        sb.append("</x:ExcelWorksheets>");
        sb.append("</x:ExcelWorkbook>");
        sb.append("</xml>");
        sb.append("<![endif]-->"); 
        sb.append(" </head>"); 
        sb.append("<body>");
        return sb.toString(); 
    }

    private static String AddExcelbottom(){
        StringBuffer sb = new StringBuffer();
        sb.append("</body>");
        sb.append("</html>");
        return sb.toString();
    }

posted @ 2011-09-20 14:28  大胡子荷西  阅读(444)  评论(0编辑  收藏  举报