欲望 发表于 2009-2-6 09:35:15

JavaEE中把数据导出为Excel

网上又好几种用于Java环境中导入,导出Excel数据的插件。我用的是jexcelapi,下载地址:http://www.andykhan.com/jexcelapi/download.html当前最高版本为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, 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("导出文件出错");
            }
      }
页: [1]
查看完整版本: JavaEE中把数据导出为Excel