如何将数据库连接信息和查询参数从控制器传递到 JasperReportsMultiFormatView

发布于 2024-12-09 03:21:28 字数 2638 浏览 0 评论 0原文

我正在使用 Spring MVC 3.0 和 JasperReports 构建 Web 应用程序原型。 我已经使用 Spring + Jfreechart + iText + Apache POI 完成了报告应用程序,并且能够成功使用 Spring 提供的相应视图类来流式传输 pdf、xls 和图像。

这次我想尝试使用 JasperReports,这样我就可以在应用程序之外设计我的 pdf,而不必担心了解底层 api(jfreechart、itext 或 poi)。

问题

我有一个report1.jrxml 文件,其中包含一个queryString 标记,其中包含带有两个日期参数的查询。当我通过iReport测试报表时,它编译并成功运行。这里没有问题。

现在我正在阅读以下 Spring 文档中的 JasperReports 部分 http://static。 springsource.org/spring/docs/3.0.x/spring-framework-reference/htmlsingle/spring-framework-reference.html#view-jasper-reports 我试图让 JasperReportsMultiFormatView 正常工作,但有一点我不理解:

  1. JasperReportMultiFormatView 如何知道要连接的数据库(回想一下,我已将查询嵌入到报表本身中)?< /p>

  2. 文档说明要在视图中使用reportDataKey属性,但我不明白这是如何解决我的问题。

  3. 如何传递参数?

可以做什么

JaperReports 提供了一组 xxxManager 对象,负责编译、归档和导出报表。您可以创建一个实现 Spring View 接口的自定义类,并执行如下操作:

Connection connection;
ServletOutputStream servletOutputStream = response .getOutputStream();
InputStream reportStream = getServlet().getServletConfig().getServletContext().getResourceAsStream("/reports/report1.jasper");
response.setContentType("application/pdf");
Class.forName("com.mysql.jdbc.Driver");
 connection = DriverManager.getConnection("jdbc:mysql://localhost: 
             3306/flightstats?user=user&password=secret");
JasperRunManager.runReportToPdfStream(reportStream,  
                 servletOutputStream, new HashMap(), connection);
connection.close();
servletOutputStream.flush();
servletOutputStream.close();

我需要什么

我需要完成上面利用 Spring 类(例如 JasperReportsPdfView、JasperReportsXlsView 甚至更好)的代码的内容JasperReportsMultiFormatView

所以总而言之,我需要能够将以下内容从我的控制器传递到 jasper 报告:

  1. 参数
  2. Db 连接信息,以便 jasper 内的 queryString 知道谁运行

这就是我所拥有的,输出是一个空白的 PDF 文档,我假设是因为它不知道如何运行查询

@RequestMapping("/reports/**")
@Controller

public class ReportsController {

@RequestMapping(value ="/reports/usage/report", method = RequestMethod.GET)
public ModelAndView handleSimpleReportMulti(HttpServletRequest request, HttpServletResponse response) throws Exception {

    System.out.println("Made it here");

    Map model = new HashMap();
    //model.put("format", "pdf");
    model.put("START_DATE", new String("09-12-2011"));
    model.put("END_DATE", new String("09-17-2011"));

    return new ModelAndView("report1", model);
}
}

I am prototyping a web application using Spring MVC 3.0 with JasperReports.
I have already done reporting applications using Spring + Jfreechart + iText + Apache POI and been able to use successfully the respective view classes provided by Spring to stream pdfs, xls and images.

This time I want to try to use JasperReports so that I can design my pdfs outside of the application and not have to worry about knowing the underlying api (be that jfreechart, itext, or poi).

Problem

I have a report1.jrxml file that contains a queryString tag with my query with two date parameters. When I test the report through iReport, it compiles and runs successfully. No problems here.

Now I am reading the JasperReports section from the following Spring documentation http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/htmlsingle/spring-framework-reference.html#view-jasper-reports
and I am trying to get the JasperReportsMultiFormatView to work properly, but there is one piece that I am not understanding:

  1. How does JasperReportMultiFormatView know the database to connect to (Recall that I have the query embedded in the report itself) ?

  2. The documentation states to use the reportDataKey property in the view, but I do not see how this is the solution to my problem.

  3. How do you pass parameters?

What can be done

JaperReports provides with a set of xxxManager objects that are responsible for compiling, fiiling, and exporting the report. You could create a custom class that implements the Spring View interface and do something like this:

Connection connection;
ServletOutputStream servletOutputStream = response .getOutputStream();
InputStream reportStream = getServlet().getServletConfig().getServletContext().getResourceAsStream("/reports/report1.jasper");
response.setContentType("application/pdf");
Class.forName("com.mysql.jdbc.Driver");
 connection = DriverManager.getConnection("jdbc:mysql://localhost: 
             3306/flightstats?user=user&password=secret");
JasperRunManager.runReportToPdfStream(reportStream,  
                 servletOutputStream, new HashMap(), connection);
connection.close();
servletOutputStream.flush();
servletOutputStream.close();

What I need

I need to accomplish what the code above those leveraging the Spring classes such as JasperReportsPdfView, JasperReportsXlsView, or even better JasperReportsMultiFormatView

So in summary I need to be able to pass the following from my controller to the jasper report:

  1. Parameters
  2. Db connection information so that the queryString inside the jasper knows who to run against

This is what I have and the output is a blank PDF document, I am assuming because it does not know how to run the query

@RequestMapping("/reports/**")
@Controller

public class ReportsController {

@RequestMapping(value ="/reports/usage/report", method = RequestMethod.GET)
public ModelAndView handleSimpleReportMulti(HttpServletRequest request, HttpServletResponse response) throws Exception {

    System.out.println("Made it here");

    Map model = new HashMap();
    //model.put("format", "pdf");
    model.put("START_DATE", new String("09-12-2011"));
    model.put("END_DATE", new String("09-17-2011"));

    return new ModelAndView("report1", model);
}
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

感悟人生的甜 2024-12-16 03:21:28

我找到了我的问题的答案。我已将上面的控制器更改为:

@RequestMapping(value ="/reports/usage/report/{format}", method = RequestMethod.GET)
public ModelAndView handleSimpleReportMulti(ModelMap modelMap, @PathVariable("format") String format) throws Exception {

    //Map model = new HashMap();
    modelMap.put("format", format);
    modelMap.put("REPORT_CONNECTION", dataSource.getConnection());
    modelMap.put("START_DATE", new String("09-12-2011"));
    modelMap.put("END_DATE", new String("09-17-2011"));

    return new ModelAndView("report1", modelMap);       
}

我已将 view.properties 更改为:

#report1(class)=org.springframework.web.servlet.view.jasperreports.JasperReportsPdfView
report1(class)=org.springframework.web.servlet.view.jasperreports.JasperReportsMultiFormatView
report1.url=/WEB-INF/reports/report1.jasper

我希望这会有所帮助。

谢谢

I found the answer to my question. I have changed my controller above to this:

@RequestMapping(value ="/reports/usage/report/{format}", method = RequestMethod.GET)
public ModelAndView handleSimpleReportMulti(ModelMap modelMap, @PathVariable("format") String format) throws Exception {

    //Map model = new HashMap();
    modelMap.put("format", format);
    modelMap.put("REPORT_CONNECTION", dataSource.getConnection());
    modelMap.put("START_DATE", new String("09-12-2011"));
    modelMap.put("END_DATE", new String("09-17-2011"));

    return new ModelAndView("report1", modelMap);       
}

I have changed my view.properties to this:

#report1(class)=org.springframework.web.servlet.view.jasperreports.JasperReportsPdfView
report1(class)=org.springframework.web.servlet.view.jasperreports.JasperReportsMultiFormatView
report1.url=/WEB-INF/reports/report1.jasper

I hope this helps.

Thanks

世界等同你 2024-12-16 03:21:28

我找到了另一种方法将连接作为参数传递,然后关闭它。

问题:
我实现了上面的解决方案,问题是,每次我调用 PDF 时,都会创建一个新连接,因此当应用程序达到打开连接的最大限制时,它会崩溃。

    ReportesDAOJDBC reportes;

    public void setReportes(ReportesDAOJDBC reportes) {
            this.reportes = reportes;
    }

    public ModelAndView leoTest(HttpServletRequest request,
        HttpServletResponse response) throws Exception {

    Map < String, Object > model = new HashMap < String, Object >();
    model.put("PARAMCONTRARECIBO", new Integer(1101));
    model.put("PARAMDOCTOS", new Integer(1101));
    model.put("REPORT_CONNECTION", reportes.getConexion());

    return new ModelAndView("leoTest",model);
    }

将连接传递给 JasperReport 的参数是 REPORT_CONNECTION,但正如我所说,这样做会导致很多问题。

我的解决方案:

    ReportesDAOJDBC reportes;

    public void setReportes(ReportesDAOJDBC reportes) {
            this.reportes = reportes;
    }

    public ModelAndView leoTest(HttpServletRequest request,
        HttpServletResponse response) throws Exception {

    Map < String, Object > model = new HashMap < String, Object >();
    model.put("PARAMCONTRARECIBO", new Integer(1101));
    model.put("PARAMDOCTOS", new Integer(1101));
    model.put("OBJETO_CONEXION", reportes);

    return new ModelAndView(new PdfView("leoTest"),model);
    }

如您所见,我实现了自己的 PdfView,并在构造函数中传递了 view.properties 文件中定义的键的名称,并且还传递了对我的 PdfView 的引用。 DAO(报告)作为HashMap的参数,参数的名称是“OBJETO_CONEXION”。这是 ReportesDAOJDBC 的代码:

    public class ReportesDAOJDBC extends JdbcDaoSupport {

      public Connection getConexion() {
         Connection  con ;
         try {
                con = getDataSource().getConnection();
         }
         catch (Exception e) {
           e.printStackTrace();
           return null;
         }
         return con;
      }

      public void closeConecction(Connection con) {
         try {
           if (con != null) {
              con.close();
           }
         }
         catch (Exception e) {
            e.printStackTrace();
         }
       }
      }

然后下一步是向您展示我自己的 PdfView 实现的代码。

    import java.io.File;
    import java.io.OutputStream;
    import java.sql.Connection;
    import java.util.Map;
    import java.util.ResourceBundle;

    import javax.servlet.ServletContext;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;

    import mx.com.mexican.leinksy.dao.jdbc.ReportesDAOJDBC;
    import mx.com.mexican.leinksy.utils.Utils;
    import net.sf.jasperreports.engine.JasperExportManager;
    import net.sf.jasperreports.engine.JasperFillManager;
    import net.sf.jasperreports.engine.JasperPrint;
    import net.sf.jasperreports.engine.JasperReport;
    import net.sf.jasperreports.engine.util.JRLoader;

    import org.springframework.web.servlet.View;
    import org.springframework.web.servlet.view.ResourceBundleViewResolver;

    public class PdfView implements View {

        private static final String CONTENT_TYPE = "application/pdf";
        private String JASPER_URL;


        public PdfView(String jasperUrl){
            this.JASPER_URL = jasperUrl+".url";
        }

        @Override
        public String getContentType() {
            return CONTENT_TYPE;
        }

        @Override
        public void render(Map model, HttpServletRequest request,
                HttpServletResponse response) throws Exception {

            System.out.println(Utils.getRealPath(request));


            ResourceBundle rb       = ResourceBundle.getBundle("view");/* Se lee el archivo view.properties*/
            ReportesDAOJDBC reporte = (ReportesDAOJDBC)model.get("OBJETO_CONEXION");/* Se obtiene el objeto de conexion */
            Connection con          = reporte.getConexion();/* Se genera la conexion a la base de datos*/


            String jasperFilePath     = Utils.getRealPath(request) + rb.getString( JASPER_URL );/* Se obtiene la ruta fisica del archivo .jasper a ejectuar*/
            JasperReport jasperReport = (JasperReport)JRLoader.loadObject(new File(jasperFilePath));/* Se carga el reporte ya compilado*/
            JasperPrint  jasperPrint  = JasperFillManager.fillReport(jasperReport, model, con);/* Se llena el reporte con datos del modelo y con la conexion a la BD*/


            try{

                OutputStream out = response.getOutputStream();

                JasperExportManager.exportReportToPdfStream(jasperPrint, out);/* Se manda el contenido a la salida estandar*/

                out.flush(); 
                out.close();

            }catch(Exception e){
                e.printStackTrace();
            }

            reporte.closeConecction(con);/* Cierro la conexion a la base de datos para liberar el pool*/

        }

    }

如您所见,我使用 ResourceBoundle 类读取 view.properties 文件来获取要加载的 .jasper 文件的路径和名称,另请注意,我不编译 .jrxml 文件,我只是加载已编译的文件,我编译jrxml 与 IREPORTS。另外,我有一个实用程序来获取 .jasper 文件的路径,如果您不知道如何操作,这里是代码。

public static String getRealPath(HttpServletRequest req) {
    ServletContext context = req.getSession().getServletContext();
    String path = context.getRealPath("/");

    if (path != null) {
        if (!path.endsWith(File.separator)) {
            path += File.separator;
        }
    }
    return  path;
}

通过这个实现,我可以控制在哪里打开关闭连接,我也尊重 SPRING 的 MVC 模型,而且我仍然使用 view.properties。

此时,也许您会问如果我想要一个 EXCEL 文件我该怎么办,好吧,我还实现了一个 XlsView,(Ajuuaaaa !!!)。这是代码:

   import java.io.File;
   import java.io.OutputStream;
   import java.sql.Connection;
   import java.util.Map;
   import java.util.ResourceBundle;

   import javax.servlet.ServletContext;
   import javax.servlet.http.HttpServletRequest;
   import javax.servlet.http.HttpServletResponse;

   import mx.com.mexican.leinsky.dao.jdbc.ReportesDAOJDBC;
   import mx.com.mexican.leinksy.utils.Utils;
   import net.sf.jasperreports.engine.JasperExportManager;
   import net.sf.jasperreports.engine.JasperFillManager;
   import net.sf.jasperreports.engine.JasperPrint;
   import net.sf.jasperreports.engine.JasperReport;
   import net.sf.jasperreports.engine.export.JRXlsExporter;
   import net.sf.jasperreports.engine.export.JRXlsExporterParameter;
   import net.sf.jasperreports.engine.util.JRLoader;

   import org.springframework.web.servlet.View;
   import org.springframework.web.servlet.view.ResourceBundleViewResolver;

   public class XlsView implements View {

    private static final String CONTENT_TYPE = "application/vnd.ms-excel";
    private String JASPER_URL;
    private String FILE_NAME = "XLSFile";


    public XlsView(String jasperUrl){
        this.JASPER_URL = jasperUrl+".url";
    }

    @Override
    public String getContentType() {
        return CONTENT_TYPE;
    }

    @Override
    public void render(Map model, HttpServletRequest request,
            HttpServletResponse response) throws Exception {

        if(model.get("FILE_NAME")!=null){
            this.FILE_NAME = model.get("FILE_NAME").toString();
        }


        ResourceBundle rb       = ResourceBundle.getBundle("view");/* Se lee el archivo view.properties*/
        ReportesDAOJDBC reporte = (ReportesDAOJDBC)model.get("OBJETO_CONEXION");/* Se obtiene el objeto de conexion */
        Connection con          = reporte.getConexion();/* Se genera la conexion a la base de datos*/


        String jasperFilePath     = Utils.getRealPath(request) + rb.getString( JASPER_URL );/* Se obtiene la ruta fisica del archivo .jasper a ejectuar*/
        JasperReport jasperReport = (JasperReport)JRLoader.loadObject(new File(jasperFilePath));/* Se carga el reporte ya compilado*/
        JasperPrint  jasperPrint  = JasperFillManager.fillReport(jasperReport, model, con);/* Se llena el reporte con datos del modelo y con la conexion a la BD*/

        response.setContentType("application/vnd.ms-excel");
        response.setHeader("Content-Disposition","attachment; filename=\""+FILE_NAME+".xls\"");
        response.setHeader("Pragma", "No-cache");
        response.setDateHeader("Expires", 1);

        try{

            OutputStream out = response.getOutputStream();

        JRXlsExporter exporterXLS = new JRXlsExporter();
        exporterXLS.setParameter(JRXlsExporterParameter.JASPER_PRINT,jasperPrint);
        exporterXLS.setParameter(JRXlsExporterParameter.OUTPUT_STREAM,out);
        exporterXLS.exportReport();

        out.flush(); 
        out.close();

    }catch(Exception e){
        e.printStackTrace();
    }

    reporte.closeConecction(con);/* Cierro la conexion a la base de datos para liberar el pool*/

}

}

所以这是我的解决方案,希望它有帮助!

I found another way to pass the connection as a parameter, and then close it.

THE PROBLEM:
I implement the solution above an the problem was, that everytime I call a PDF, a new connection was created, so when the app gets to the max limit of open connections it crash.

    ReportesDAOJDBC reportes;

    public void setReportes(ReportesDAOJDBC reportes) {
            this.reportes = reportes;
    }

    public ModelAndView leoTest(HttpServletRequest request,
        HttpServletResponse response) throws Exception {

    Map < String, Object > model = new HashMap < String, Object >();
    model.put("PARAMCONTRARECIBO", new Integer(1101));
    model.put("PARAMDOCTOS", new Integer(1101));
    model.put("REPORT_CONNECTION", reportes.getConexion());

    return new ModelAndView("leoTest",model);
    }

The parameter to pass a connection to a JasperReport is REPORT_CONNECTION, but as I said, doing this way, will cause a lot of trobubles.

MY SOLUTION:

    ReportesDAOJDBC reportes;

    public void setReportes(ReportesDAOJDBC reportes) {
            this.reportes = reportes;
    }

    public ModelAndView leoTest(HttpServletRequest request,
        HttpServletResponse response) throws Exception {

    Map < String, Object > model = new HashMap < String, Object >();
    model.put("PARAMCONTRARECIBO", new Integer(1101));
    model.put("PARAMDOCTOS", new Integer(1101));
    model.put("OBJETO_CONEXION", reportes);

    return new ModelAndView(new PdfView("leoTest"),model);
    }

As you can see, I implement my own PdfView and I pass in the constructor the name of the key define in the view.properties file, and also I pass a reference to my DAO (reportes) as a parameter of the HashMap, the name of the parameter is "OBJETO_CONEXION". Here is the code for ReportesDAOJDBC:

    public class ReportesDAOJDBC extends JdbcDaoSupport {

      public Connection getConexion() {
         Connection  con ;
         try {
                con = getDataSource().getConnection();
         }
         catch (Exception e) {
           e.printStackTrace();
           return null;
         }
         return con;
      }

      public void closeConecction(Connection con) {
         try {
           if (con != null) {
              con.close();
           }
         }
         catch (Exception e) {
            e.printStackTrace();
         }
       }
      }

Then the next step is to show you the code of my own PdfView implementation.

    import java.io.File;
    import java.io.OutputStream;
    import java.sql.Connection;
    import java.util.Map;
    import java.util.ResourceBundle;

    import javax.servlet.ServletContext;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;

    import mx.com.mexican.leinksy.dao.jdbc.ReportesDAOJDBC;
    import mx.com.mexican.leinksy.utils.Utils;
    import net.sf.jasperreports.engine.JasperExportManager;
    import net.sf.jasperreports.engine.JasperFillManager;
    import net.sf.jasperreports.engine.JasperPrint;
    import net.sf.jasperreports.engine.JasperReport;
    import net.sf.jasperreports.engine.util.JRLoader;

    import org.springframework.web.servlet.View;
    import org.springframework.web.servlet.view.ResourceBundleViewResolver;

    public class PdfView implements View {

        private static final String CONTENT_TYPE = "application/pdf";
        private String JASPER_URL;


        public PdfView(String jasperUrl){
            this.JASPER_URL = jasperUrl+".url";
        }

        @Override
        public String getContentType() {
            return CONTENT_TYPE;
        }

        @Override
        public void render(Map model, HttpServletRequest request,
                HttpServletResponse response) throws Exception {

            System.out.println(Utils.getRealPath(request));


            ResourceBundle rb       = ResourceBundle.getBundle("view");/* Se lee el archivo view.properties*/
            ReportesDAOJDBC reporte = (ReportesDAOJDBC)model.get("OBJETO_CONEXION");/* Se obtiene el objeto de conexion */
            Connection con          = reporte.getConexion();/* Se genera la conexion a la base de datos*/


            String jasperFilePath     = Utils.getRealPath(request) + rb.getString( JASPER_URL );/* Se obtiene la ruta fisica del archivo .jasper a ejectuar*/
            JasperReport jasperReport = (JasperReport)JRLoader.loadObject(new File(jasperFilePath));/* Se carga el reporte ya compilado*/
            JasperPrint  jasperPrint  = JasperFillManager.fillReport(jasperReport, model, con);/* Se llena el reporte con datos del modelo y con la conexion a la BD*/


            try{

                OutputStream out = response.getOutputStream();

                JasperExportManager.exportReportToPdfStream(jasperPrint, out);/* Se manda el contenido a la salida estandar*/

                out.flush(); 
                out.close();

            }catch(Exception e){
                e.printStackTrace();
            }

            reporte.closeConecction(con);/* Cierro la conexion a la base de datos para liberar el pool*/

        }

    }

As you can see, I read the view.properties file using the ResourceBoundle class to get the path and name of the .jasper file to load, Also notice that I dont compile the .jrxml file I just load the compiled file, I compile the jrxml with IREPORTS. Also I hava a utility to get the path of my .jasper file, here is the code if you dont have idea of how to do it.

public static String getRealPath(HttpServletRequest req) {
    ServletContext context = req.getSession().getServletContext();
    String path = context.getRealPath("/");

    if (path != null) {
        if (!path.endsWith(File.separator)) {
            path += File.separator;
        }
    }
    return  path;
}

With this implementation I can control where to open an close connection, also I respect the MVC model of SPRING, and also I still using the view.properties.

And at this point, maybe you are asking WHAT CAN I DO IF I WANT AN EXCEL FILE, well, I also implement an XlsView, (Ajuuaaaa !!! ). Here is the code:

   import java.io.File;
   import java.io.OutputStream;
   import java.sql.Connection;
   import java.util.Map;
   import java.util.ResourceBundle;

   import javax.servlet.ServletContext;
   import javax.servlet.http.HttpServletRequest;
   import javax.servlet.http.HttpServletResponse;

   import mx.com.mexican.leinsky.dao.jdbc.ReportesDAOJDBC;
   import mx.com.mexican.leinksy.utils.Utils;
   import net.sf.jasperreports.engine.JasperExportManager;
   import net.sf.jasperreports.engine.JasperFillManager;
   import net.sf.jasperreports.engine.JasperPrint;
   import net.sf.jasperreports.engine.JasperReport;
   import net.sf.jasperreports.engine.export.JRXlsExporter;
   import net.sf.jasperreports.engine.export.JRXlsExporterParameter;
   import net.sf.jasperreports.engine.util.JRLoader;

   import org.springframework.web.servlet.View;
   import org.springframework.web.servlet.view.ResourceBundleViewResolver;

   public class XlsView implements View {

    private static final String CONTENT_TYPE = "application/vnd.ms-excel";
    private String JASPER_URL;
    private String FILE_NAME = "XLSFile";


    public XlsView(String jasperUrl){
        this.JASPER_URL = jasperUrl+".url";
    }

    @Override
    public String getContentType() {
        return CONTENT_TYPE;
    }

    @Override
    public void render(Map model, HttpServletRequest request,
            HttpServletResponse response) throws Exception {

        if(model.get("FILE_NAME")!=null){
            this.FILE_NAME = model.get("FILE_NAME").toString();
        }


        ResourceBundle rb       = ResourceBundle.getBundle("view");/* Se lee el archivo view.properties*/
        ReportesDAOJDBC reporte = (ReportesDAOJDBC)model.get("OBJETO_CONEXION");/* Se obtiene el objeto de conexion */
        Connection con          = reporte.getConexion();/* Se genera la conexion a la base de datos*/


        String jasperFilePath     = Utils.getRealPath(request) + rb.getString( JASPER_URL );/* Se obtiene la ruta fisica del archivo .jasper a ejectuar*/
        JasperReport jasperReport = (JasperReport)JRLoader.loadObject(new File(jasperFilePath));/* Se carga el reporte ya compilado*/
        JasperPrint  jasperPrint  = JasperFillManager.fillReport(jasperReport, model, con);/* Se llena el reporte con datos del modelo y con la conexion a la BD*/

        response.setContentType("application/vnd.ms-excel");
        response.setHeader("Content-Disposition","attachment; filename=\""+FILE_NAME+".xls\"");
        response.setHeader("Pragma", "No-cache");
        response.setDateHeader("Expires", 1);

        try{

            OutputStream out = response.getOutputStream();

        JRXlsExporter exporterXLS = new JRXlsExporter();
        exporterXLS.setParameter(JRXlsExporterParameter.JASPER_PRINT,jasperPrint);
        exporterXLS.setParameter(JRXlsExporterParameter.OUTPUT_STREAM,out);
        exporterXLS.exportReport();

        out.flush(); 
        out.close();

    }catch(Exception e){
        e.printStackTrace();
    }

    reporte.closeConecction(con);/* Cierro la conexion a la base de datos para liberar el pool*/

}

}

So this is my solution, hope It helps !!!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文