2010-08-13 101 views
2

如何在JSP頁面中顯示jasper報告?我正在使用iReport1.3.3工具創建
報告。我努力在JSP頁面中顯示jasper報告。JSP頁面中的Jasper報告

是否有可能將ArrayList傳遞給碧玉報告?

我需要以PDF和EXcel格式顯示報告。

+1

在JSP做這聽起來像一個非常糟糕的主意。你爲什麼不用servlet來做這件事? – 2010-08-13 08:39:26

+0

@Maurice ..這是我的要求..我正在使用struts框架,一旦 行動取得成功,我將其轉發到xx.jsp page.in 頁面我創建一些數據的超鏈接。如果我點擊 超鏈接,它應該調用jasper報告模板,並且它顯示爲PDF格式 。 請幫我,我一直在努力掙扎長周。 – Manu 2010-08-13 09:47:43

回答

2

我寫了一個呈現PDF和CSV的struts(1.1)應用程序。我會在一個行動處理器做到這一點:

public ActionForward execute(ActionMapping mapping, ActionForm form, 
     HttpServletRequest request, HttpServletResponse response) 
     throws Exception { 
    response.setContentType("application/pdf"); 
    OutputStream out = response.getOutputStream(); 
    try { 
     // generate the PDF 
    } finally { 
     out.close(); 
    } 
    return null; 
} 

UPDATE:餵養集合來JasperReports的

package reports; 

import java.lang.reflect.Method; 
import java.util.Collection; 
import java.util.Iterator; 
import java.util.Arrays; 
import net.sf.jasperreports.engine.JRDataSource; 
import net.sf.jasperreports.engine.JRField; 
import net.sf.jasperreports.engine.JRException; 

public class CollectionDataSource implements JRDataSource { 
    private Iterator iterator = null; 
    private Object current = null; 

    public CollectionDataSource(Collection col) { 
     if (col != null) { 
      iterator = col.iterator(); 
     } 
    } 

    public CollectionDataSource(Object array[]) { 
     this(Arrays.asList(array == null ? new Object[0] : array)); 
    } 

    public boolean next() throws JRException { 
     if (iterator == null || !iterator.hasNext()) { 
      return false; 
     } else { 
      current = iterator.next(); 
      return true; 
     } 
    } 

    public Object getFieldValue(JRField field) throws JRException { 
     if ("this".equals(field.getName())) { 
      return current; 
     } else if (current == null) { 
      return null; 
     } else { 
      Class<?> clazz = current.getClass(); 
      char chars[] = field.getName().toCharArray(); 
      chars[0] = Character.toUpperCase(chars[0]); 
      String name = new String(chars); 
      Method method = null; 
      try { 
       method = clazz.getMethod("get" + name); 
      } catch (NoSuchMethodException e) { 
       if (field.getValueClass() == Boolean.class) { 
        try { 
         method = clazz.getMethod("is" + name); 
        } catch (NoSuchMethodException e1) { 
        } 
       } 
      } 
      if (method == null) { 
       throw new JRException("No getter for field " + name); 
      } 
      try { 
       return method.invoke(current); 
      } catch (Exception e) { 
       throw new JRException("Exception in getter of " + name, e); 
      } 
     } 
    } 
} 
+0

@ Maurice ..就像是通過點擊jsp頁面中的超鏈接,將呈現pdf或excel格式報告 – Manu 2010-08-13 15:03:26

+0

,並且如果您不介意,可以向我發送一些完整的示例代碼 。我的電子郵件ID是:[email protected] 或[email protected] – Manu 2010-08-13 15:06:12

+0

是否可以將ArrayList傳遞給xyz.jasper(xyz是文件名) 模板並將其填充爲pdf或excel格式? – Manu 2010-08-13 15:10:07