导出Excel,只要指向这个页面,设置几个参数就可以导出了

<%@ page contentType="application/vnd.ms-excel;charset=GBK"%>
<%@ page import="com.isoftstone.util.poi.*"%>
<%@ page import="java.util.*" %>
<%@ page import="org.apache.poi.hssf.usermodel.*"%>
<%@ page import="java.io.*"%>

<%
			ExcelParameterInterface excelParam = (ExcelParameterInterface) request.getSession()
					.getAttribute("EXCELPARAM");
			if (excelParam == null) {
				throw new RuntimeException("操作出错.");
			}
			response.reset();
			response.setContentType("application/vnd.ms-excel;charset=GBK");
			String fileName = excelParam.getFileName();
			response.setHeader("Content-Disposition", "attachment; filename="
					+ fileName + ".xls");

			OutputStream httpOut = response.getOutputStream();

			List dataTitle = excelParam.getDataHead();
			List dataDetail = excelParam.getDataDetail();
			try {
				HSSFWorkbook wb = ExcelSimple.createWorkbook();
				ExcelSimple excelSimple = new ExcelSimple(); 
				excelSimple.insertRowsOneSheel(wb, dataTitle, dataDetail);
				
				wb.write(httpOut);
				httpOut.close();
			} catch (Exception ex) {
				response.setContentType("text/html");

				out.println("<html>");
				out.println("<head>");
				out.println("<title>数据导出</title>");
				out
						.println("<link rel=\"stylesheet\" type=\"text/css\" href=\"../stylesheet.css\" title=\"Style\">");
				out.println("</head>");

				out.println("<body bgcolor=\"white\">");

				out.println("<span class=\"bnew\">数据导出出错 :</span>");
				out.println("<pre>");

				ex.printStackTrace();

				out.println("</pre>");

				out.println("</body>");
				out.println("</html>");

				return;
			}
		%>

上面是JSP中的代码
 

|

|

|

|

 下面是Action中的代码

       List resultList = new Vector();//结果集合,里面放的元素是一行
        
        List titleList = new Vector();//标题行
          //指向指定页面导出
            ExcelParameterInterface excelParam = new ExcelParam("exportReport",
                    titleList, resultList);
            request.getSession().setAttribute("EXCELPARAM", excelParam);
            request.setAttribute("reportList", reportList);
            response.sendRedirect(request.getContextPath()
                    + "/excelexport/excelexport.jsp");
            return mapping.findForward("");
        }
        else
        {
            request.setAttribute("wrongPrompt1", "没有满足条件的数据");//为空,返回给页面显示
        }
        return mapping.findForward("success");


 

你可能感兴趣的:(exception,vector,list,Excel,import,stylesheet)