以前用到的:
<servlet> <description> </description> <display-name>ExcelServlet</display-name> <servlet-name>ExcelServlet</servlet-name> <servlet-class> *.excel.ExcelServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>ExcelServlet</servlet-name> <url-pattern>*.xls</url-pattern> </servlet-mapping>
import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.log4j.Logger; public class ExcelServlet extends HttpServlet { private final Logger log = Log4jUtil.getLogger(ExcelServlet.class); public void init() throws ServletException { } public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("application/vnd.ms-excel"); RequestPage page = new RequestPage(request, response, request.getSession()); ExcelOperator operator = null; try { operator = getOperator(request.getServletPath()); if (operator != null) { operator.execute(page); } } catch (Exception ex) { log.debug("导出excel出错:" + ex.toString()); ex.printStackTrace(); } } /** * 根据不同的请求path获取不同的导出excel操作 * * @param path * @return */ public ExcelOperator getOperator(String path) { return new ReportExcelExport(); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } public void destroy() { } }
import java.io.IOException; import jxl.write.WriteException; public abstract class ExcelOperator { jxl.write.WritableWorkbook excel = null; int fieldCount = 0; public RequestPage page; public void execute(RequestPage page) throws WriteException, IndexOutOfBoundsException, IOException { this.page = page; excel = jxl.Workbook.createWorkbook(this.page.getResponse() .getOutputStream()); excel.createSheet("sheet1", 0); setTitle(getTitle()); writeContent(getContent()); write(); } private void setTitle(String[] fields) throws IndexOutOfBoundsException, WriteException { this.fieldCount = fields.length; for (int i = 0; i < fields.length; i++) { excel.getSheet(0).addCell(new jxl.write.Label(i, 0, fields[i])); } } private void writeContent(java.util.ArrayList cells) throws IndexOutOfBoundsException, WriteException { if (cells == null) return; for (int i = 1; i <= cells.size(); i++) { Object obj = cells.get(i - 1); for (int j = 0; j < fieldCount; j++) { excel.getSheet(0).addCell( new jxl.write.Label(j, i, getValue(obj, j))); } } } public abstract String getValue(Object obj, int index); public abstract String[] getTitle(); public abstract java.util.ArrayList getContent(); private void write() throws IOException, WriteException, IOException { excel.write(); excel.close(); } }
import java.util.ArrayList; import java.util.HashMap; public class ReportExcelExport extends ExcelOperator { @Override public ArrayList getContent() { } /** * 报表表头 */ @Override public String[] getTitle() { return new String[] { "责任人ID","责任人","责任人角色","年份" }; } /** * 单元格的值 */ @Override public String getValue(Object obj, int index) { switch (index) { case 0: return values.getUserId(); default: return ""; } } }
ref:
if (request.getHeader("User-Agent").indexOf("MSIE 5.5") != -1) { response.setHeader("Content-Disposition", "filename=" + fileName); } else { response.addHeader("Content-Disposition", "attachment;filename=" + fileName); } response.setCharacterEncoding("GBK"); //response.setContentType("text/html;charset=GBK"); ServletOutputStream stream = null; BufferedInputStream buf = null; FileInputStream fis = null; try { stream = response.getOutputStream(); fis = new FileInputStream(file); buf = new BufferedInputStream(fis); byte[] readBytes = new byte[4096]; int len = 0; while ((len = buf.read(readBytes)) != -1) { stream.write(readBytes, 0, len); } stream.flush(); response.setStatus(HttpServletResponse.SC_OK); response.flushBuffer(); } catch (Exception ex) { logger.info(ex.toString()); } finally { if (stream != null) { try { stream.close(); } catch (IOException e) { logger.info(e.toString()); e.printStackTrace(); } } if (buf != null) { try { buf.close(); } catch (IOException e) { logger.info(e.toString()); e.printStackTrace(); } } if (fis != null) { try { fis.close(); } catch (IOException e) { logger.info(e.toString()); e.printStackTrace(); } } }
posted on 2011-03-09 22:19 疯子123 阅读(118) 评论(0) 编辑 收藏 举报