• JXL生成Excel,并提供下载(1:生成Excel)


    public String exportExcel(long id) {
        String preeReviewName = "文件名";
        
        String filePath = 路径名;
        WritableWorkbook wwb =null;
        try {
            wwb = Workbook.createWorkbook(new File(filePath + preeReviewName +".xls"));
            writeReviewGeneral(wwb, id);
            //一个Excel表若有多个sheet页,可以加多个
            wwb.write();
            wwb.close();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        } 
        return preeReviewName;
    }
    
    private void writeReviewGeneral(WritableWorkbook wwb,long id){
        //获取数据库的数据
        PreeReviewInfo result = preeReviewInfoDS.getPreeReviewInfo(id).getT();
        //数据库各个字段的值放进List
        List<String> list = new ArrayList<String>();
        list.add(result.getPreeReviewName());//项目名称
        list.add(result.getPreeReviewTarget());//评审对象
        list.add(ReviewTypes.getDescription(result.getPreeReviewType()));//评审方式
        list.add(result.getPreeReviewScale());//评审规模
        list.add(result.getProcessName(););//项目当前阶段:所处过程
        list.add(result.getPreeCompere());//主持人
        list.add(result.getDocumentCreator());//作者
        list.add(result.getPreeRegistrar());//记录员
        list.add(result.getPreeActor());//评审员
        list.add(result.getExpert());//关键资源:专家
        try {
            WritableSheet sheet = wwb.createSheet("评审概况",0);
            //设置头部格式
            WritableCellFormat headerFormat = getExcelHeadStyle();
            //设置内容格式
            WritableCellFormat contentFormat = getExcelContentStyle();
            for(int i =0; i < list.size(); i++){
                sheet.setColumnView(0, 25);//列宽
                sheet.setColumnView(1, 25);//列宽
                
                sheet.setRowView(i, 300);    //行高
                sheet.addCell(new Label(0,i,ExportSummary.reviewGeneral[i],headerFormat));
                sheet.addCell(new Label(1,i,list.get(i),contentFormat));
            }
    
        } catch (RowsExceededException e) {
            e.printStackTrace();
        } catch (WriteException e) {
            e.printStackTrace();
        } 
    }
    
    
    private WritableCellFormat getExcelHeadStyle(){
        WritableFont font = new WritableFont(WritableFont.createFont("宋体"), 
                11, WritableFont.NO_BOLD, false, UnderlineStyle.NO_UNDERLINE, jxl.format.Colour.RED);
        WritableCellFormat headerFormat = new WritableCellFormat(NumberFormats.TEXT); 
        headerFormat.setFont(font);
        try {
            //内容水平居中显示
            headerFormat.setAlignment(jxl.format.Alignment.CENTRE);
        } catch (WriteException e) {
            e.printStackTrace();
        } 
        
        return headerFormat;
    }
    
    private WritableCellFormat getExcelContentStyle(){
        WritableFont font = new WritableFont(WritableFont.createFont("宋体"), 
                11, WritableFont.NO_BOLD, false, UnderlineStyle.NO_UNDERLINE, jxl.format.Colour.BLACK);
        WritableCellFormat contentFormat = new WritableCellFormat(NumberFormats.TEXT); 
        contentFormat.setFont(font);
        try {
            //允许换行
            contentFormat.setWrap(true);
        } catch (WriteException e) {
            e.printStackTrace();
        }
        return contentFormat;
    }
  • 相关阅读:
    第1件事 产品经理工作的8个核心步骤
    产品心经:产品经理应该知道的60件事
    漫画中国式项目管理摘录
    敏捷落地的会议和工具
    UML的9种图例解析
    UML类图关系(泛化 、继承、实现、依赖、关联、聚合、组合)-转
    UML类图与类的关系详解
    eclipse-4.4.2安装Groovy插件(其他版本eclipse可参考)
    Eclipse 安装Groovy插件
    Groovy安装与入门实例
  • 原文地址:https://www.cnblogs.com/Donnnnnn/p/8444555.html
Copyright © 2020-2023  润新知