Chinaunix首页 | 论坛 | 博客
  • 博客访问: 820677
  • 博文数量: 780
  • 博客积分: 7000
  • 博客等级: 少将
  • 技术积分: 5010
  • 用 户 组: 普通用户
  • 注册时间: 2008-09-12 09:11
文章分类

全部博文(780)

文章存档

2011年(1)

2008年(779)

我的朋友
最近访客

分类:

2008-09-12 09:14:26

   网上又好几种用于环境中导入,导出Excel数据的插件。我用的是jexcelapi,地址:  当前最高版本为2.6

    程序实例如下:

    view plaincopy to clipboardprint?
    public void exportClassroom(OutputStream os) throws PaikeException {

 

            try {

                WritableWorkbook wbook = Workbook.createWorkbook(os); //建立excel文件

                WritableSheet wsheet = wbook.createSheet("教室信息表", 0); //工作表名称

                //设置Excel字体

                WritableFont wfont = new WritableFont(WritableFont.ARIAL, 16,

                        WritableFont.BOLD, false,

                        jxl.format.UnderlineStyle.NO_UNDERLINE,

                        jxl.format.Colour.BLACK);

                WritableCellFormat titleFormat = new WritableCellFormat(wfont);

 

                String[] title = { "教室名", "容 量", "类 型", "其他说明" };

 

                //设置Excel表头

                for (int i = 0; i < title.length; i++) {

                    Label excelTitle = new Label(i, 0, title[i], titleFormat);

                    wsheet.addCell(excelTitle);

                }

 

                int c = 1; //用于循环时Excel的行号

                ClassroomService cs = new ClassroomService();

                List list = cs.findAllClassroom();       //这个是从数据库中取得要导出的数据

                Iterator it = list.iterator();

 

                while (it.hasNext()) {

                    ClassroomDTO crdto = (ClassroomDTO) it.next();

                    Label content1 = new Label(0, c, crdto.getRoomname());

                    Label content2 = new Label(1, c, crdto.getCapicity().toString());

                    Label content3 = new Label(2, c, crdto.getRoomTypeId()

                            .toString());

                    Label content4 = new Label(3, c, crdto.getRemark());

                    wsheet.addCell(content1);

                    wsheet.addCell(content2);

                    wsheet.addCell(content3);

                    wsheet.addCell(content4);

                    c++;

                }

                wbook.write(); //写入文件

                wbook.close();

                os.close();

            } catch (Exception e) {

                throw new PaikeException("导出文件出错");

            }

        }

【责编:landy】

--------------------next---------------------

阅读(476) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~