导出Excel

来源:互联网 发布:淘宝二手房翻新 编辑:程序博客网 时间:2024/06/08 18:28

在控制器里面 写入进入jsp页面

public String writeExcel (HttpServletRequest request) throws Exception{

       List<Object[]> list = whmsManager.findMaterial(selectTypeOne, day);

       request.setAttribute("list", list);

       return "whms/whms.jsp";

}

这里的whms.jsp 是指进入的页面

 

 

<%@ page language="java" contentType="text/html; charset=UTF-8"%>

<%@ page  import="jxl.*,jxl.write.*,java.util.*,java.io.*"  %>

<%@page import="com.iit.whms.webapp.controller.AAA"%>

<%@page import="java.text.*;"%>

<%

      response.reset();

      SimpleDateFormat  df=new SimpleDateFormat("yyyy-MM-dd");

              String date="whmsLazyInfo_"+df.format(new Date());

              byte[] bdate=date.getBytes();

              String d=new String(bdate,"GBK");

              response.setCharacterEncoding("utf8");

              response.setHeader("Connection", "close");

              response.setHeader("Context-Type", "application/vnd.ms-excel");

              response.setHeader("Content-Disposition", "attachment;filename="+d+".xls");  

    List list=(List) request.getAttribute("list");

    OutputStream resOut=response.getOutputStream();

   WritableWorkbook  writableWorkbook =AAA.getWritableWorkbookLazyWhms(resOut,listWhmsMaterialDetail);

   try{

   writableWorkbook.write();

   writableWorkbook.close();

   resOut.flush();

   resOut.close();

   resOut=null;

   response.flushBuffer();

 

   }catch(Exception e){

   e.toString();

   }   out.clear();

   out=pageContext.pushBody();

%>

 

下面返回到getWritableWorkbookLazyWhms这个控制器里面

 

public static WritableWorkbook getWritableWorkbookLazyWhms(OutputStream out, List<Object[]> listWhmsMaterialDetail) {

       WritableWorkbook book;

       try {

           book = Workbook.createWorkbook(out);

           WritableSheet sheet = book.createSheet("呆滞料号信息", 0);

           try {

              WritableFont fon = new WritableFont(WritableFont.COURIER);

              // fon.setColour(Colour.DEFAULT_BACKGROUND);

              WritableCellFormat wcf = new WritableCellFormat(fon);

              // wcf.setBackground(Colour.SKY_BLUE);

              CellView view = new CellView();

              view.setSize(5000);

              sheet.setColumnView(0, view);

              sheet.addCell(new Label(0, 1 , "编号",wcf));

              sheet.addCell(new Label(1, 1 , "中文名称", wcf));

             

              for (int i = 0; i < list.size(); i++) {

                  Object[] obj = list.get(i);

                  sheet.addCell(new Label(0, 1 + i+1, String.valueOf(i + 1), wcf));

                  String temp = "";

                  if(obj[0]!=null){

                     temp = obj[0].toString();

                  }

                  sheet.addCell(new Label(1, 1 + i+1, temp, wcf));

              }

           } catch (RowsExceededException e) {

              e.printStackTrace();

           } catch (WriteException e) {

              e.printStackTrace();

           }

       } catch (IOException e) {

           book = null;

           e.printStackTrace();

       }

 

       return book;

    }