excel的导出

import jxl.write.WritableCellFormat;

import jxl.write.WritableWorkbook;

import jxl.write.WritableSheet;

 

 

 

@Override
    public void exportExcel(CourseTypeModel courseTypeModel,
            HttpServletResponse response) {
        try {
            String fileName = ""; //文件名
            fileName = new String(("商品分类表.xls").getBytes("gbk"),"iso8859-1");
            OutputStream os = response.getOutputStream();
            response.reset();
            response.setContentType("application/vnd.ms-excel;charset=UTF-8");
            response.setHeader("Content-disposition","attachment; filename="+fileName);
            
            //大标题字体
            WritableCellFormat wcfTitle = new WritableCellFormat();
            WritableFont BigTitleFont = new WritableFont(WritableFont.createFont("宋体"), 18, WritableFont.BOLD,false,                 UnderlineStyle.NO_UNDERLINE,Colour.BLACK);
            wcfTitle.setAlignment(jxl.format.Alignment.CENTRE);
            wcfTitle.setVerticalAlignment(VerticalAlignment.CENTRE);
            wcfTitle.setFont(BigTitleFont);
            wcfTitle.setBorder(Border.ALL, BorderLineStyle.THIN);
            
            //表头字体
            WritableCellFormat wcfGrid = new WritableCellFormat();
            WritableFont gridTitleFont = new WritableFont(WritableFont.createFont("宋体"), 10, WritableFont.BOLD,false, UnderlineStyle.NO_UNDERLINE,Colour.BLACK);
            wcfGrid.setAlignment(jxl.format.Alignment.CENTRE);
            wcfGrid.setVerticalAlignment(VerticalAlignment.CENTRE);
            wcfGrid.setFont(gridTitleFont);
            wcfGrid.setBorder(Border.ALL, BorderLineStyle.THIN);
            wcfGrid.setWrap(true);
            
            //内容字体
            WritableCellFormat wcfL = new WritableCellFormat();
            wcfL.setAlignment(jxl.format.Alignment.LEFT);
            wcfL.setVerticalAlignment(VerticalAlignment.CENTRE);
            wcfL.setBorder(Border.ALL, BorderLineStyle.THIN);
            wcfL.setWrap(true);
            
            WritableWorkbook wb = Workbook.createWorkbook(os);
            WritableSheet sheet = wb.createSheet("商品分类表", 0);

            //大标题
            sheet.mergeCells(0, 0, 6, 0);
            sheet.setRowView(0, 1000);
            sheet.addCell(new Label(0, 0, "商品分类表",wcfTitle));
            //表头
           /* sheet.addCell(new Label(0, 1, "所属项目中文名称",wcfGrid));
            sheet.setColumnView(0, 20);
            sheet.addCell(new Label(1, 1, "所属项目英文名称",wcfGrid));
            sheet.setColumnView(1, 30);*/
            sheet.addCell(new Label(0, 1, "商品分类中文名称",wcfGrid));
            sheet.setColumnView(2, 20);
            sheet.addCell(new Label(1, 1, "商品分类英文名称",wcfGrid));
            sheet.setColumnView(3, 30);
            sheet.addCell(new Label(2, 1, "创建人",wcfGrid));
            sheet.setColumnView(4, 10);
            sheet.addCell(new Label(3, 1, "创建时间",wcfGrid));
            sheet.setColumnView(5, 10);
            sheet.addCell(new Label(4, 1, "序号",wcfGrid));
            sheet.setColumnView(6, 50);
            
            //根据条件查找
            List<CourseType> resultList = this.queryList(courseTypeModel);
            int row = 2;
            int col = 0;
            if(null != resultList && resultList.size()>0){
                //商品分类细目
                for(int i=0;i<resultList.size();i++){
                    CourseType baseInfo = resultList.get(i);
                    sheet.addCell(new Label(col++, row, baseInfo.getNameCh(),wcfL));//分类中文名
                    sheet.addCell(new Label(col++, row, baseInfo.getNameEn(),wcfL));//分类英文名
                    sheet.addCell(new Label(col++, row, baseInfo.getUserName(),wcfL));//创建人
                    sheet.addCell(new Label(col++, row, TimeTools.calendar2String(baseInfo.getLastChanged(), "yyyy-MM-dd"),wcfL));//创建时间
                    sheet.addCell(new Label(col++, row, baseInfo.getListNum().toString(),wcfL));//备注
                    row ++;
                    col = 0;
                }
            }
            wb.write();
            wb.close();
            os.flush();
            os.close();
            
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (WriteException e) {
            e.printStackTrace();
        }
    }

posted @ 2017-02-14 09:29  baby孔祥超  阅读(141)  评论(0编辑  收藏  举报