zoukankan      html  css  js  c++  java
  • 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();
            }
        }

  • 相关阅读:
    [转载]PostgreSQL 的昨天、今天和明天 Joe
    [转载]ArcObjects使用小记~Singleton objects Joe
    [小记]postgresql的系统表 Joe
    ~PostgreSQL About~ Joe
    [转]Three things you should never put in your database Joe
    经典问题之汉诺塔
    经典问题之费式级数
    ExtJs 表单和表格之间进行数据交互
    commonfileupload 上传单个或者多个文件 示例
    软件工程中10个重要思想
  • 原文地址:https://www.cnblogs.com/kongxc/p/6396214.html
Copyright © 2011-2022 走看看