birt 导出excel时,有38个字段,导出的excel文件中数据都叠在一起,格式非常难看,该问题怎么解决。thx

解决方案 »

  1.   

    给你看看我以前servlet导出Excel的例子public void doGet(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
            request.setCharacterEncoding("gbk");
            EmployeeVo employeeVo=new EmployeeVo();
             //»ñÈ¡Ô±¹¤±àºÅ
             employeeVo.setEmpCode(request.getParameter("empCode"));
             System.out.println("ÄãÒªµ¼³öµÄÔ±¹¤±àºÅΪ:"+request.getParameter("empCode"));
             //»ñÈ¡Ô±¹¤ÐÕÃû
             employeeVo.setEmpName(request.getParameter("empName"));
            
            //³õʼ»¯Service
            IEmployeeManageBs service=new EmployeeManageBs();
            //µôÓÃService
            List employeeList=service.queryByCondition(employeeVo);
            // ÓÃÈÕÆÚ¹¹½¨Êä³öÎļþÃû³Æ
            SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd");
            String date = sd.format(new Date());
            String excelName = "Ô±¹¤ÐÅÏ¢_" + date + ".xls";
            // ½â¾öµ¼³öÔ±¹¤Êý¾Ýʱºº×ÖÂÒÂëÎÊÌâ
            excelName = new String(excelName.getBytes(), "iso8859-1");
            // Ñ¡ÔñµØÖ·µã»÷±£´æ»òÕßÖ±½Óµã»÷²é¿´
            response.setContentType("application/vnd.ms-excel");
            response.addHeader("Content-Disposition", "attachment; filename=\""+ excelName + "\"");
            OutputStream os = response.getOutputStream();
            //ÀûÓÃOutputStream´´½¨WritableWorkbook¶ÔÏó
            WritableWorkbook wwb = Workbook.createWorkbook(os);
            //ÉèÖÃ×ÖÌå ¡¢ÑÕÉ« ¡¢´óС
            WritableFont font1=new WritableFont(WritableFont.TIMES,11,WritableFont.BOLD,false,
                    UnderlineStyle.NO_UNDERLINE,Colour.GREEN); 
            WritableFont font2=new WritableFont(WritableFont.TIMES,18,WritableFont.BOLD); 
            WritableFont font3=new WritableFont(WritableFont.ARIAL,9,WritableFont.BOLD);
            WritableFont font4=new WritableFont(WritableFont.TIMES,11,WritableFont.BOLD,false,
                    UnderlineStyle.NO_UNDERLINE,Colour.RED);
            WritableCellFormat format1=new WritableCellFormat(font1);
            WritableCellFormat format2=new WritableCellFormat(font2);
            WritableCellFormat format3=new WritableCellFormat(font3);
            WritableCellFormat format4=new WritableCellFormat(font4);
            try {
                //°Ñˮƽ¶ÔÆ뷽ʽָ¶¨Îª¾ÓÖÐ
                format1.setAlignment(jxl.format.Alignment.CENTRE);
                format2.setAlignment(jxl.format.Alignment.CENTRE);
                format3.setAlignment(jxl.format.Alignment.CENTRE);
                format4.setAlignment(jxl.format.Alignment.CENTRE);
            } catch (WriteException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            // Éú³ÉÃûΪ"Ô±¹¤ÐÅÏ¢"µÄ¹¤×÷±í£¬²ÎÊý0±íʾÕâÊǵÚÒ»Ò³
            WritableSheet sheet=wwb.createSheet("Ô±¹¤ÐÅÏ¢",0);
            //ÉèÖõ¥Ôª±í¸ñµÄ¿í¶ÈΪ£º13
            sheet.setColumnView(0,13);
            sheet.setColumnView(1,13);
            sheet.setColumnView(2,13);
            sheet.setColumnView(3,13);
            sheet.setColumnView(4,15);
            sheet.setColumnView(5,15);
            sheet.setColumnView(6,15);
            sheet.setColumnView(7,13);
            sheet.setColumnView(8,13);
            sheet.setColumnView(9,13);
            try {
            //ºÏ²¢µ¥Ôª±í¸ñ
            sheet.mergeCells(0,0,9,0);
            //Ïò±í¸ñÀï²åÈëÊý¾Ý ²¢ÉèÖà formatÊôÐÔ
            //ÔÚLabel¶ÔÏóµÄ¹¹ÔìÖ®ÖÐÖ¸Ãûµ¥Ôª¸ñλÖÃÊǵÚÒ»ÁеÚÒ»ÐÐ(0,0)
            Label label1 = new Label(0, 0, "Ô±¹¤ÐÅÏ¢±í",format2);
            Label label2 = new Label(0, 1, "¹¤ºÅ",format4);
            Label label3 = new Label(1, 1, "ÐÕÃû",format1);
            Label label4 = new Label(2, 1, "ÐÔ±ð",format1);
            Label label14 = new Label(3, 1, "ÄêÁä",format1);
            Label label15 = new Label(4, 1, "µç»°",format1);
            Label label16 = new Label(5, 1, "ÊÖ»ú",format1);
            Label label17 = new Label(6, 1, "Óʼþ",format1);
            Label label18 = new Label(7, 1, "ѧÀú",format1);
            Label label19 = new Label(8, 1, "µØÖ·",format1);
            Label label20 = new Label(9, 1, "״̬",format1);
            // ½«¶¨ÒåºÃµÄµ¥Ôª¸ñÌí¼Óµ½¹¤×÷±íÖÐ
            sheet.addCell(label1);
            sheet.addCell(label2);
            sheet.addCell(label3);
            sheet.addCell(label4);
            sheet.addCell(label14);
            sheet.addCell(label15);
            sheet.addCell(label16);
            sheet.addCell(label17);
            sheet.addCell(label18);
            sheet.addCell(label19);
            sheet.addCell(label20);
            EmployeeVo[] empVo=new EmployeeVo[employeeList.size()];
            for (int i = 0; i <employeeList.size(); i++){
                empVo[i] =(EmployeeVo) employeeList.get(i);
                Label label5 = new Label(0,i+2,""+empVo[i].getEmpCode(),format4);
                sheet.addCell(label5);
                Label label6=new Label(1,i+2,""+empVo[i].getEmpName(),format3);
                sheet.addCell(label6);
                Label label7=new Label(2,i+2,"1".equals(empVo[i].getEmpSex())?"ÄÐ":"Å®",format3);
                sheet.addCell(label7);
                jxl.write.Number label8=new jxl.write.Number(3,i+2,empVo[i].getEmpAge(),format3);
                sheet.addCell(label8);
                Label label9=new Label(4,i+2,""+empVo[i].getEmpPhone(),format3);
                sheet.addCell(label9);
                Label label10=new Label(5,i+2,""+empVo[i].getEmpMobile(),format3);
                sheet.addCell(label10);
                Label label11 = new Label(6,i+2,""+empVo[i].getEmpEmail(),format3);
                sheet.addCell(label11);
                Label label12=new Label(7,i+2,""+empVo[i].getEmpDegree(),format3);
                sheet.addCell(label12);
                Label label13=new Label(8,i+2,""+empVo[i].getEmpAddress(),format3);
                sheet.addCell(label13);
                Label label21=new Label(9,i+2,"1".equals(empVo[i].getEmpState())?"ÔÚÖ°":"ÀëÖ°",format3);
                sheet.addCell(label21);
                
            }
            System.out.println("´´½¨³É¹¦");
            // Ð´ÈëÊý¾Ý²¢¹Ø±ÕÎļþ
            wwb.write();
            wwb.close();
            } catch (RowsExceededException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (WriteException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            
            
                    
        }
    希望对你有帮助
      

  2.   

    对于这个字段(即在Excel中显示)比较多的 你可以先去定义个模板文件。。这样再用结合数据读取模板。这样就不会存在java类里面设置好多项的麻烦了、。。要修改直接改模板。
      

  3.   

    谢谢,birt是个报表插件,不是自己手动写的代码
      

  4.   

     你这里说的修改模板,那不是得修改birt源代码的模板吗,但就算是修改了birt的里面的源代码,可对其它的导出又不适用了 
      

  5.   

    lz,你可以给报表的Layout prefrence属性,设置成auto Layout;并把每列的宽度设置的差不多。可能会好些。
    对于,列比较多,导出在Excel中显示的时候,出现挤压正常的,顶多在Excel中将列拖的宽些就是了。