我在struts2下用jasperreport时候,html显示总是很多红叉,查了些网上的解决方案,比如加入px.gif都不能解决。看到还有一个方案是改源码,请问怎么样修改。
另外,在网页生成excel格式显示时候,我的表格等也都乱了,如果选择另存为保存,下载后打开excel后也是这样,关闭时还会报错,无法正常关闭excel,请问下是怎么样解决。

解决方案 »

  1.   

    第二个问题要是用jasperreport可以把背景设为透明,Field撑满网格
    exporter.setParameter(JRXlsExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS, Boolean.TRUE);
    exporter.setParameter(JRXlsExporterParameter.IS_WHITE_PAGE_BACKGROUND,Boolean.FALSE);
    但是你用的struts2好像已经被封装了,看看怎么设置了哦,没用过,不过jasperreport出的excel报表效果都不敢恭维,看看有没有高手有宝贵的建议了
      

  2.   

    附上我之前做的项目,希望能帮到你......如有不懂的可以联系我: qq:326582858
    ------------------------------------------------------------------------------------------------------------pdf-----------------------------------------------------------------------------------------------------------------:sSQL = String.format(sSQL, sWhere);
    Connection con = null;
    String strMessage="";
    ResultSet rs=null;
    //String path1=request.getContextPath();
    //String path2="E:\\workspace\\IreportDemo\\WebRoot\\report\\SecondDemo.jasper";
    String path= request.getRealPath("/reports/jb7.jasper");
    System.out.println(path);
    File reportFile = new File(path);
    byte[] bytes=new byte[200];
    try {
    con = DBConnection.currentConnection();
    Statement stat = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
    ResultSet.CONCUR_READ_ONLY);
    // ResultSet rs=DBConnection.getConnection();
     rs=stat.executeQuery(sSQL);
    } catch (SQLException e2) {
    // TODO Auto-generated catch block
    e2.printStackTrace();
    }
    JRResultSetDataSource ds = new JRResultSetDataSource(rs);

    System.out.println("-------------------------------------------------------------");
    try {
    System.out.println(ds.next());
    } catch (JRException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
    }
    System.out.println("-------------------------------------------------------------");
    try {
    //生成pdf文件
    bytes = JasperRunManager.runReportToPdf(reportFile.getPath(),null, ds);
    } catch (JRException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    //pdf设置
        response.setContentType("application/pdf");
     response.setContentLength(bytes.length);
        ServletOutputStream ouputStream;
    try {
    ouputStream = response.getOutputStream();
    ouputStream.write(bytes, 0, bytes.length);
    ouputStream.write(0);
        ouputStream.flush();
        ouputStream.close();
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    return null; ------------------------------------------------------------------------------------------------------------html-----------------------------------------------------------------------------------------------------------------:
    sSQL = String.format(sSQL, sWhere);
    Connection con = null;
    String strMessage="";
    ResultSet rs=null;
    response.setCharacterEncoding("UTF-8");
    File reportFile = new File(request.getRealPath("/reports/jb7.jasper"));
        if (!reportFile.exists())
    throw new JRRuntimeException("File WebappReport.jasper not found. The report design must be compiled first."); JasperReport jasperReport = null;
    try {
    jasperReport = (JasperReport)JRLoader.loadObject(reportFile.getPath());

    } catch (JRException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
    }

    Map parameters = new HashMap();
    parameters.put("ReportTitle", "Address Report");
    parameters.put("BaseDir", reportFile.getParentFile());
    try
    {

    con = DBConnection.currentConnection();
    Statement stat = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
    ResultSet.CONCUR_READ_ONLY);
    rs=stat.executeQuery(sSQL);
    System.out.println(rs.next());
    JRResultSetDataSource jrds = new JRResultSetDataSource(rs);
    System.out.println("--------------------------------------------------");
    System.out.println(jrds.next());

    JasperPrint jasperPrint = 
    JasperFillManager.fillReport(
    jasperReport, 
    parameters, 
    jrds
    );
    response.setContentType("text/html");  
    JRHtmlExporter exporter = new JRHtmlExporter();
    PrintWriter out=response.getWriter();
    request.setAttribute(ImageServlet.DEFAULT_JASPER_PRINT_SESSION_ATTRIBUTE, jasperPrint);
    exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
    exporter.setParameter(JRExporterParameter.OUTPUT_WRITER, out);
    exporter.setParameter(JRHtmlExporterParameter.IMAGES_URI, "/test/images/px.");
    exporter.exportReport();
    } catch (Exception e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }


    return null;------------------------------------------------------------------------------------------------------------excel-----------------------------------------------------------------------------------------------------------------:sSQL = String.format(sSQL, sWhere);
    Connection con = null;
    String strMessage="";
    ResultSet rs=null;
    //html版本方法
    //String path1=request.getContextPath();
    //将文件保存在工程目录下,会有bug,必须先生成,然后才能做转向跳转,那么就不同步。
    //String path="E:\\workspace\\test\\WebRoot\\reports\\jb7.jasper";
    //System.out.println(path);
    //现在考虑将文件保存在服务器端
    String path= request.getRealPath("/reports/jb7.jasper");
    System.out.println(path);
    File reportFile = new File(path);
    try {
    con = DBConnection.currentConnection();
    Statement stat = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
    ResultSet.CONCUR_READ_ONLY);
    rs=stat.executeQuery(sSQL);
    } catch (SQLException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
    }
    JRDataSource dataSource=new JRResultSetDataSource(rs);
    try {
    System.out.println(dataSource.next());
    } catch (JRException e1) {
    e1.printStackTrace();
    }
    //生成EXCEL
    try {
    Map parameters = new HashMap();
    JasperPrint jasperPrint = JasperFillManager.fillReport(reportFile.getPath(),null,dataSource);
        JRXlsExporter exporter = new JRXlsExporter(); 
        byte[]   bytes;   
        ByteArrayOutputStream   baos   =   new   ByteArrayOutputStream();   
        exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
        exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, response.getOutputStream());
        exporter.setParameter(JRXlsExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS,Boolean.TRUE); // 删除记录最下面的空行
        exporter.setParameter(JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET,Boolean.FALSE);// 删除多余的ColumnHeader
        exporter.setParameter(JRXlsExporterParameter.IS_WHITE_PAGE_BACKGROUND,Boolean.FALSE);// 显示边框
        exporter.setParameter(JRExporterParameter.JASPER_PRINT,jasperPrint);   
        exporter.setParameter(JRExporterParameter.OUTPUT_STREAM,baos);
        exporter.exportReport();   
        
        bytes   =   baos.toByteArray();   
         if   (bytes   !=   null   &&   bytes.length   >   0){   
          response.reset();   
          response.setCharacterEncoding("utf-8");
          response.setContentType("application/vnd.ms-excel;charset=GB2312");   
          response.setHeader("Content-disposition","attachment;   filename=\"" + URLEncoder.encode("jb7", "UTF-8") + ".xls\"");   
          response.setContentLength(bytes.length);   
          ServletOutputStream   ouputStream   =   response.getOutputStream();   
          ouputStream.write(bytes,   0,   bytes.length);   
          ouputStream.flush();   
          ouputStream.close();   
         } } catch (Exception e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }

       //跳转
        return null;
        


      

  3.   

    看一下我的博文http://redsnow-fenglin.javaeye.com/blog/453063,也许有帮助~~