I am new to iReport. I'm using iReport 3.7.4. My question is that when I export report to excel calling by jsp, java .file is downloaded and on opening the file it give me the error that "file and extension of .xls" don't match. The file could be corrupt and mismatched. Can anyone resolve my issues please thanks in advance.
Here is my export code of java jsp:
Connection con = null;
try {
Dao dao = new Dao();
List parm = new ArrayList();
con = adminSession.con;
String progId = request.getParameter("progId");
String termId = request.getParameter("term");
String testId=" 303";
HashMap map = new HashMap();
map.put("P_TERM_ID", termId);
String rootFolder = null;
String ServletPath = request.getServletPath();
String ServletName = ServletPath.substring(1);
String ServletPathReal = application.getRealPath(ServletPath);
String sep = "";
String ReportPath = getServletConfig().getServletContext().getRealPath("/Images/");
if (ServletPathReal.indexOf("\\") > 0) {
sep = "\\";
} else {
sep = "/";
}
ReportPath = ServletPathReal.substring(0, ServletPathReal.lastIndexOf(sep));
String reportTitle = request.getParameter("title");
if (reportTitle != null && reportTitle.length() > 0) {
reportTitle = reportTitle.toUpperCase();
}
FileInputStream input = null;
JasperPrint jasperPrint = null;
String reportName = "CandidatesUnpaidChallan";
String reportPath = java.io.File.separatorChar + "";
String subPath = getServletConfig().getServletContext().getRealPath("/");
subPath = subPath + reportPath + "reports" + reportPath;
String path = getServletConfig().getServletContext().getRealPath("/reports");
String name = "CandidatesUnpaidChallan.jrxml";
String totalPath = path + File.separator + name;
JasperReport jasperReport = null;
jasperReport = JasperCompileManager.compileReport(totalPath);
jasperPrint = JasperFillManager.fillReport(jasperReport,map, con);
String filename = path + File.separator + "CandidatesUnpaidChallan";
java.util.Date dNow = new java.util.Date();
SimpleDateFormat ft = new SimpleDateFormat("yyyyMMddhhmmss");
String dateAppend = ft.format(dNow);
String excelOutput = filename;
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
jasperPrint.setProperty("net.sf.jasperreports.export.xls.exclude.origin.keep.first.band.1", "pageHeader");
jasperPrint.setProperty("net.sf.jasperreports.export.xls.exclude.origin.keep.first.band.2", "columnHeader");
jasperPrint.setProperty("net.sf.jasperreports.export.xls.exclude.origin.band.2", "pageFooter");
JExcelApiExporter jExcelApiExporter = new JExcelApiExporter();
jExcelApiExporter.setParameter(JExcelApiExporterParameter.JASPER_PRINT, jasperPrint);
jExcelApiExporter.setParameter(JExcelApiExporterParameter.IS_WHITE_PAGE_BACKGROUND, Boolean.FALSE);
jExcelApiExporter.setParameter(JExcelApiExporterParameter.OUTPUT_STREAM, outputStream);
jExcelApiExporter.setParameter(JExcelApiExporterParameter.IS_IGNORE_CELL_BORDER,Boolean.TRUE);
jExcelApiExporter.setParameter(JExcelApiExporterParameter.IS_ONE_PAGE_PER_SHEET,Boolean.FALSE);
jExcelApiExporter.setParameter(JExcelApiExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS,Boolean.TRUE);
jExcelApiExporter.setParameter(JExcelApiExporterParameter.OFFSET_X,0);
jExcelApiExporter.setParameter(JExcelApiExporterParameter.OFFSET_Y,0 );
jExcelApiExporter.setParameter(JExcelApiExporterParameter.OUTPUT_FILE_NAME, excelOutput);
jExcelApiExporter.exportReport();
File file = new File(filename+".xlsx");
response.setContentType("application/vnd.ms-excel");
response.setHeader("Cache-Control", "max-age=30");
response.setHeader("Content-Disposition", "attachment;filename="+reportName+".xlsx");
response.setHeader("Content-disposition", "inline; filename=\"" + reportName + "_" + dateAppend + ".xlsx\"");
input = new FileInputStream(file);
int readBytes = 0;
BufferedInputStream buf = new BufferedInputStream(input);
stream = response.getOutputStream();
while ((readBytes = buf.read()) != -1) {
stream.write(readBytes);
}
stream.flush();
input.close();
stream.close();
} catch (Exception oops) {
System.out.println(oops);
con = null;
throw new Exception("Error occured while showing Evaluations~" + oops.toString());
} finally {
con = null;
}