Jasper Reports Excel Output

Com certeza muitos precisarao dessa dica :slight_smile: …

try
{
// Class.forName(oracle.jdbc.driver.OracleDriver);
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
Connection conn = ........
File reportFile = new File("C:jspsample.jasper"); 
String path = reportFile.getAbsolutePath(); //tried getAbsolutePath() 
//System.out.println("Absolute Path = " + reportFile.getAbsolutePath());
Map parameters = new HashMap(); 
parameters.put("myparam",new Integer(2)); 
JasperPrint jasperPrint = JasperManager.fillReport ( path,parameters, conn); 
byte bytes[] = new byte[10];
String result = JasperRunManager.runReportToHtmlFile("C:jspsample.jasper" , parameters, conn);
JRXlsExporter exporter = new JRXlsExporter();

ByteArrayOutputStream xlsReport = new ByteArrayOutputStream();
exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, xlsReport);
exporter.setParameter(JRExporterParameter.OUTPUT_FILE, "C:JSP");
exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, "sample.xls");
exporter.exportReport();
System.out.println("Sixe of byte array:"+xlsReport.size());
bytes = xlsReport.toByteArray();
response.setContentType("application/vnd.ms-excel");
System.out.println("After JasperPrint = "+result);
response.setContentLength(bytes.length);
xlsReport.close();
OutputStream ouputStream = response.getOutputStream();
ouputStream.write(bytes, 0, bytes.length);
ouputStream.flush();
ouputStream.close();
}
catch(Exception e)
{e.printStackTrace();}

klaid dias
klaiddias@tutopia.com.br

Teu post tem quase 3 anos e foi muito √ļtil hoje.
Obrigado!

Vou aumentar para mais de 6 anos :-)‚Ķ ainda hoje continua √ļtil.

Sem d√ļvidas super √ļtil e atual

31 de Agosto de 2010 e mostrou-se √ļtil!

27/01/2012 :slight_smile:

Fora quem usou o post e não comentou…

rsrsrsrsrs

Valeu!

27/01/2012 :slight_smile:

Fora quem usou o post e não comentou…

rsrsrsrsrs

Valeu!

Funcional!!! Parabéns!!

30/01/2013 - OPA mais uma aqui mas com uma duvida

JasperManager ???
response ???

esta dando o seguinte erro : ele nao acha a varial, blz ate ai mas que tipo de variavel criar ? Desculpa a pergunta leiga. mas sou novato nesse mundo Java

       try {
// Class.forName(oracle.jdbc.driver.OracleDriver);   

            this.connection = new ConnectionFactory().getConnection();

            File reportFile = new File("C:jspsample.jasper");
            String path = reportFile.getAbsolutePath(); //tried getAbsolutePath()   
//System.out.println("Absolute Path = " + reportFile.getAbsolutePath());   
            Map parameters = new HashMap();
            parameters.put("myparam", new Integer(2));
            JasperPrint jasperPrint = JasperManager.fillReport(path, parameters, connection);
            byte bytes[] = new byte[10];
            String result = JasperRunManager.runReportToHtmlFile("C:jspsample.jasper", parameters, connection);
            JRXlsExporter exporter = new JRXlsExporter();

            ByteArrayOutputStream xlsReport = new ByteArrayOutputStream();
            exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
            exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, xlsReport);
            exporter.setParameter(JRExporterParameter.OUTPUT_FILE, "C:JSP");
            exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, "sample.xls");
            exporter.exportReport();
            System.out.println("Sixe of byte array:" + xlsReport.size());
            bytes = xlsReport.toByteArray();
            response.setContentType("application/vnd.ms-excel");
            System.out.println("After JasperPrint = " + result);
            response.setContentLength(bytes.length);
            xlsReport.close();
            OutputStream ouputStream = response.getOutputStream();
            ouputStream.write(bytes, 0, bytes.length);
            ouputStream.flush();
            ouputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

Estou tendo o seguinte erro

java.lang.RuntimeException: Uncompilable source code - Erroneous sym type: JasperManager.fillReport
	at gui.MenuGUI.exportarExcelActionPerformed(MenuGUI.java:205)
	at gui.MenuGUI.access$000(MenuGUI.java:23)
	at gui.MenuGUI$1.actionPerformed(MenuGUI.java:73)
	at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
	at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
	at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
	at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
	at javax.swing.AbstractButton.doClick(AbstractButton.java:376)
	at javax.swing.plaf.basic.BasicMenuItemUI.doClick(BasicMenuItemUI.java:833)
	at javax.swing.plaf.basic.BasicMenuItemUI$Handler.mouseReleased(BasicMenuItemUI.java:877)
	at java.awt.Component.processMouseEvent(Component.java:6505)
	at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
	at java.awt.Component.processEvent(Component.java:6270)
	at java.awt.Container.processEvent(Container.java:2229)
	at java.awt.Component.dispatchEventImpl(Component.java:4861)
	at java.awt.Container.dispatchEventImpl(Container.java:2287)
	at java.awt.Component.dispatchEvent(Component.java:4687)
	at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
	at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
	at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
	at java.awt.Container.dispatchEventImpl(Container.java:2273)
	at java.awt.Window.dispatchEventImpl(Window.java:2719)
	at java.awt.Component.dispatchEvent(Component.java:4687)
	at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:723)
	at java.awt.EventQueue.access$200(EventQueue.java:103)
	at java.awt.EventQueue$3.run(EventQueue.java:682)
	at java.awt.EventQueue$3.run(EventQueue.java:680)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
	at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
	at java.awt.EventQueue$4.run(EventQueue.java:696)
	at java.awt.EventQueue$4.run(EventQueue.java:694)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
	at java.awt.EventQueue.dispatchEvent(EventQueue.java:693)
	at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:244)
	at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:163)
	at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:151)
	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:147)
	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:139)
	at java.awt.EventDispatchThread.run(EventDispatchThread.java:97)