Yo lo hago con esta clase donde solo la mando a llamar cuando necesito un metodo.
Te la dejo de ejemplo, no es igual al tuyo pero puedes hacerlo de esta forma
public class JasperUtil {
private static final String PREFIX = "/resources/reports/";
private static final String SUFFIX = ".jasper";
public JasperUtil() {
}
public static ReporteJasper jasperReportPDF(String reportName, String nombreArchivo, @SuppressWarnings("rawtypes") Map params, DataSource ds) throws SQLException {
ReporteJasper reporte = null;
String fileName;
Connection conn = ds.getConnection();
ExternalContext eContext = FacesContext.getCurrentInstance().getExternalContext();
try {
InputStream stream = eContext.getResourceAsStream(PREFIX + reportName + SUFFIX);
if (stream != null) {
ServletContext sContext = (ServletContext) eContext.getContext();
String realPath = sContext.getRealPath("/resources/reports/");
String fileSeparator = System.getProperty("file.separator");
realPath += fileSeparator;
params.put(JRParameter.REPORT_LOCALE, new Locale("es", "GT"));
JasperPrint jasperPrint = JasperFillManager.fillReport(stream, params, conn);
fileName = nombreArchivo;
JRExporter exporter = new JRPdfExporter();
exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, realPath + fileName);
exporter.setParameter(JRExporterParameter.CHARACTER_ENCODING, "UTF-8");
exporter.exportReport();
reporte = new ReporteJasper();
reporte.setFileName(fileName);
reporte.setPages(jasperPrint.getPages().size());
}
} catch (Exception ex) {
ex.printStackTrace();
} finally {
conn.close();
}
return reporte;
}
public static ReporteJasper jasperReportXLS(String reportName, String nombreArchivo, @SuppressWarnings("rawtypes") Map params, DataSource ds) throws SQLException {
ReporteJasper reporte = null;
String fileName;
Connection conn = ds.getConnection();
ExternalContext eContext = FacesContext.getCurrentInstance().getExternalContext();
try {
InputStream stream = eContext.getResourceAsStream(PREFIX + reportName + SUFFIX);
if (stream != null) {
ServletContext sContext = (ServletContext) eContext.getContext();
String realPath = sContext.getRealPath("/resources/reports/");
String fileSeparator = System.getProperty("file.separator");
realPath += fileSeparator;
params.put(JRParameter.REPORT_LOCALE, new Locale("es", "GT"));
JasperPrint jasperPrint = JasperFillManager.fillReport(stream, params, conn);
fileName = nombreArchivo;
JRXlsExporter exporter = new JRXlsExporter();
exporter.setParameter(JRXlsExporterParameter.JASPER_PRINT, jasperPrint);
exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, realPath + fileName);
exporter.setParameter(JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET, Boolean.FALSE);
exporter.setParameter(JRXlsExporterParameter.IS_DETECT_CELL_TYPE, Boolean.TRUE);
exporter.setParameter(JRXlsExporterParameter.IS_WHITE_PAGE_BACKGROUND, Boolean.FALSE);
exporter.setParameter(JRXlsExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS, Boolean.TRUE);
exporter.exportReport();
reporte = new ReporteJasper();
reporte.setFileName(fileName);
reporte.setPages(jasperPrint.getPages().size());
}
} catch (Exception ex) {
ex.printStackTrace();
} finally {
conn.close();
}
return reporte;
}
public static ReporteJasper jasperReportTxt(String reportName, String nombreArchivo, @SuppressWarnings("rawtypes") Map params, DataSource ds) throws SQLException {
ReporteJasper reporte = null;
String fileName;
Connection conn = ds.getConnection();
ExternalContext eContext = FacesContext.getCurrentInstance().getExternalContext();
try {
InputStream stream = eContext.getResourceAsStream(PREFIX + reportName + SUFFIX);
if (stream != null) {
ServletContext sContext = (ServletContext) eContext.getContext();
String realPath = sContext.getRealPath("/resources/reports/");
String fileSeparator = System.getProperty("file.separator");
realPath += fileSeparator;
JasperPrint jasperPrint = JasperFillManager.fillReport(stream, params, conn);
fileName = nombreArchivo;
JRExporter exporter = new JRTextExporter();
exporter.setExporterInput(new SimpleExporterInput(jasperPrint));
//exporter.setParameter(JRTextExporterParameter.LINE_SEPARATOR, "\r\n");
exporter.setExporterOutput(new SimpleWriterExporterOutput(realPath + fileName));
exporter.exportReport();
reporte = new ReporteJasper();
reporte.setFileName(fileName);
reporte.setPages(jasperPrint.getPages().size());
}
} catch (Exception ex) {
ex.printStackTrace();
} finally {
conn.close();
}
return reporte;
}
public static ReporteJasper jasperReportCsv(String reportName, String nombreArchivo, @SuppressWarnings("rawtypes") Map params, DataSource ds) throws SQLException {
ReporteJasper reporte = null;
String fileName;
Connection conn = ds.getConnection();
ExternalContext eContext = FacesContext.getCurrentInstance().getExternalContext();
try {
InputStream stream = eContext.getResourceAsStream(PREFIX + reportName + SUFFIX);
if (stream != null) {
ServletContext sContext = (ServletContext) eContext.getContext();
String realPath = sContext.getRealPath("/resources/reports/");
String fileSeparator = System.getProperty("file.separator");
realPath += fileSeparator;
JasperPrint jasperPrint = JasperFillManager.fillReport(stream, params, conn);
fileName = nombreArchivo;
JRExporter exporter = new JRCsvExporter();
exporter.setExporterInput(new SimpleExporterInput(jasperPrint));
//exporter.setParameter(JRTextExporterParameter.LINE_SEPARATOR, "\r\n");
exporter.setExporterOutput(new SimpleWriterExporterOutput(realPath + fileName));
exporter.exportReport();
reporte = new ReporteJasper();
reporte.setFileName(fileName);
reporte.setPages(jasperPrint.getPages().size());
}
} catch (Exception ex) {
ex.printStackTrace();
} finally {
conn.close();
}
return reporte;
}
public static ReporteJasper jasperReportNewTxt(String reportName, String nombreArchivo, @SuppressWarnings("rawtypes") Map params, DataSource ds) throws SQLException {
ReporteJasper reporte = null;
String fileName;
Connection conn = ds.getConnection();
ExternalContext eContext = FacesContext.getCurrentInstance().getExternalContext();
try {
InputStream stream = eContext.getResourceAsStream(PREFIX + reportName + SUFFIX);
if (stream != null) {
ServletContext sContext = (ServletContext) eContext.getContext();
String realPath = sContext.getRealPath("/resources/reports/");
String fileSeparator = System.getProperty("file.separator");
realPath += fileSeparator;
JasperPrint jasperPrint = JasperFillManager.fillReport(stream, params, conn);
fileName = nombreArchivo;
JRExporter exporter = new JRTextExporter();
exporter.setExporterInput(new SimpleExporterInput(jasperPrint));
//exporter.setParameter(JRTextExporterParameter.LINE_SEPARATOR, "\r\n");
exporter.setExporterOutput(new SimpleWriterExporterOutput(realPath + fileName));
exporter.exportReport();
reporte = new ReporteJasper();
reporte.setFileName(fileName);
if (jasperPrint.getPages() != null) {
reporte.setPages(jasperPrint.getPages().size());
}
}
} catch (Exception ex) {
ex.printStackTrace();
} finally {
conn.close();
}
return reporte;
}
}
Exception
en el programa Java, algo así más o menos:java.lang.NoClassDefFoundError: org/apache/poi/ss/usermodel/RichTextString at net.sf.jasperreports.view.save.JRSingleSheetXlsSaveContributor.save(JRSingleSheetXlsSaveContributor.java:111)
?