I am new to iReport 3.7.4 with Java, I get errors when I export my report to Excel, below is the complete code. I am using Excel 2016.
What I get when I try my code is, the Excel file is downloaded, but when I open the file it shows me the error that data is not in correct format and I receive errors on the console. The images below is the output of my excel sheet.
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_PROG_ID", progId);
map.put("P_TERM_ID", termId);
//map.put("sectionId", sectionId);
// map.put("section", section);
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 = "/";
}
OutputStream stream = null;
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";//.xlsx";
// String excelOutput = filename + "_" + dateAppend + ".xlsx";
java.util.Date dNow = new java.util.Date();
SimpleDateFormat ft = new SimpleDateFormat("yyyyMMddhhmmss");
String dateAppend = ft.format(dNow);
String excelOutput = filename;// + "_" + dateAppend + ".xlsx";
//ByteArrayOutputStream outputByteArray = new ByteArrayOutputStream();
/* JRXlsExporter exporterXLS = new JRXlsExporter();
exporterXLS.setParameter(JRXlsExporterParameter.JASPER_PRINT, jasperPrint);
exporterXLS.setParameter(JRXlsExporterParameter.OUTPUT_STREAM,outputByteArray );//outputByteArray);
exporterXLS.setParameter(JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET, Boolean.FALSE);
exporterXLS.setParameter(JRXlsExporterParameter.IS_DETECT_CELL_TYPE, Boolean.TRUE);
exporterXLS.setParameter(JRXlsExporterParameter.IS_IGNORE_GRAPHICS, Boolean.TRUE);
exporterXLS.setParameter(JRXlsExporterParameter.IS_WHITE_PAGE_BACKGROUND, Boolean.FALSE);
exporterXLS.setParameter(JRXlsExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS, Boolean.TRUE);
exporterXLS.exportReport();*/
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
jasperPrint.setProperty("net.sf.jasperreports.export.xls.exclude.origin.keep.first.band.1", "pageHeader");
// Remove the column headers except the first one
jasperPrint.setProperty("net.sf.jasperreports.export.xls.exclude.origin.keep.first.band.2", "columnHeader");
// Remove the pageFooter from all the pages
jasperPrint.setProperty("net.sf.jasperreports.export.xls.exclude.origin.band.2", "pageFooter");
// set the JXL parameters to generate Excel report
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(jasperPrint, filename);
// jExcelApiExporter.setParameter(JExcelApiExporterParameter.JASPER_PRINT,filename);
jExcelApiExporter.exportReport();
File file = new File(filename+".xlsx");
// SET THE MIME TYPE.
response.setContentType("application/vnd.ms-excel");
response.setHeader("Cache-Control", "max-age=30");
// set content dispostion to attachment in with file name.
// case the open/save dialog needs to appear.
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;
}
The error on the console is:
020 10:57:40.569 SEVERE [http-nio-8084-exec-9] org.apache.catalina.core.StandardWrapperValve.invoke Servlet.service() for servlet [jsp] in context with path [/MAJUKHI] threw exception [java.lang.IllegalStateException: getOutputStream() has already been called for this response] with root cause
java.lang.IllegalStateException: getOutputStream() has already been called for this response
at org.apache.catalina.connector.Response.getWriter(Response.java:578)
at org.apache.catalina.connector.ResponseFacade.getWriter(ResponseFacade.java:212)
at javax.servlet.ServletResponseWrapper.getWriter(ServletResponseWrapper.java:109)
at org.apache.jasper.runtime.JspWriterImpl.initOut(JspWriterImpl.java:115)
at org.apache.jasper.runtime.JspWriterImpl.flushBuffer(JspWriterImpl.java:108)
at org.apache.jasper.runtime.PageContextImpl.release(PageContextImpl.java:181)
at org.apache.jasper.runtime.JspFactoryImpl.internalReleasePageContext(JspFactoryImpl.java:120)
at org.apache.jasper.runtime.JspFactoryImpl.releasePageContext(JspFactoryImpl.java:75)
at org.apache.jsp.Admin.AdminProcessCandidateUnpaidChallans_jsp._jspService(AdminProcessCandidateUnpaidChallans_jsp.java:546)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:725)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:396)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:340)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:725)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
Here is my jrxmlcode containing the query:
<?xml version="1.0" encoding="UTF-8"?>
<jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="CandidatesUnpaidChallan" language="groovy" pageWidth="842" pageHeight="595" orientation="Landscape" columnWidth="822" leftMargin="10" rightMargin="10" topMargin="10" bottomMargin="10">
<property name="ireport.zoom" value="1.5"/>
<property name="ireport.x" value="0"/>
<property name="ireport.y" value="0"/>
<parameter name="P_TERM_ID" class="java.lang.String"/>
<queryString>
<![CDATA[SELECT T.TERM_NME, ' '||P.PROG_ABBREV PROG_ABBREV,S.FORM_NBR,' '||C.FIRST_NME||' '||C.MIDDLE_NME||' '||C.LAST_NME NME,C.L_PHONE1_NBR,' '||C.P_ADDRESS_TXT P_ADDRESS_TXT,' '||C.P_EMAIL_TXT P_EMAIL_TXT,FCN.INSTALLMENT_DUE_DTE
FROM MAJUMSKHI.TERM T, MAJUMSKHI.CANDIDATE C,MAJUMSKHI.SELECTION S,MAJUMSKHI.FEE_CHALLAN_NEW FCN,MAJUMSKHI.FEE_CANDIDATE FC,MAJUMSKHI.BATCH B,MAJUMSKHI.TERM_PROGRAM TP,MAJUMSKHI.PROGRAM P
WHERE C.CAND_ID=S.CAND_ID
AND C.TERM_ID = T.TERM_ID
AND S.CAND_ID=FCN.CAND_ID
AND FCN.FEE_CHALLAN_NEW_ID=FC.FEE_CHALLAN_NEW_ID
AND S.BATCH_ID=B.BATCH_ID
AND B.TERM_PROG_ID=TP.TERM_PROG_ID
AND TP.PROG_ID=P.PROG_ID
AND S.REG_NBR IS NULL
AND FCN.PAID_DTE IS NULL
AND FC.COUNT_NBR IS NOT NULL
AND FC.DISCOUNTED_AMT>0
AND C.TERM_ID=$P{P_TERM_ID}
ORDER BY P.PROG_ABBREV,S.FORM_NBR]]>
</queryString>
<field name="TERM_NME" class="java.lang.String"/>
<field name="PROG_ABBREV" class="java.lang.String"/>
<field name="FORM_NBR" class="java.lang.String"/>
<field name="NME" class="java.lang.String"/>
<field name="L_PHONE1_NBR" class="java.lang.String"/>
<field name="P_ADDRESS_TXT" class="java.lang.String"/>
<field name="P_EMAIL_TXT" class="java.lang.String"/>
<field name="INSTALLMENT_DUE_DTE" class="java.sql.Timestamp"/>
<variable name="TCHR_NME_1" class="java.lang.Integer" calculation="Count">
<variableExpression><![CDATA[]]></variableExpression>
</variable>
<variable name="COURSE_NME_1" class="java.lang.Integer" calculation="Count">
<variableExpression><![CDATA[]]></variableExpression>
</variable>
<variable name="Sr#" class="java.lang.Integer" calculation="Count">
<variableExpression><![CDATA[$V{Sr#}]]></variableExpression>
</variable>
<title>
<band height="10" splitType="Stretch"/>
</title>
<pageHeader>
<band height="96">
<staticText>
<reportElement x="0" y="12" width="822" height="20"/>
<textElement textAlignment="Center">
<font fontName="SansSerif" size="14" isBold="true" pdfFontName="Helvetica-Bold"/>
</textElement>
<text><![CDATA[Mohammad Ali Jinnah University Karachi]]></text>
</staticText>
<line>
<reportElement x="0" y="89" width="822" height="1"/>
</line>
<staticText>
<reportElement x="0" y="32" width="822" height="20"/>
<textElement textAlignment="Center" verticalAlignment="Middle">
<font fontName="SansSerif" size="11" isBold="true" pdfFontName="Helvetica-Bold"/>
</textElement>
<text><![CDATA[Candidates Unpaid Challan]]></text>
</staticText>
<staticText>
<reportElement x="358" y="57" width="45" height="20"/>
<textElement textAlignment="Right" verticalAlignment="Middle">
<font isBold="true"/>
</textElement>
<text><![CDATA[Term:]]></text>
</staticText>
<textField>
<reportElement x="405" y="57" width="100" height="20"/>
<textElement verticalAlignment="Middle">
<font isBold="true"/>
</textElement>
<textFieldExpression class="java.lang.String"><![CDATA[$F{TERM_NME}]]></textFieldExpression>
</textField>
</band>
</pageHeader>
<columnHeader>
<band height="30" splitType="Stretch">
<staticText>
<reportElement x="1" y="0" width="26" height="30"/>
<box>
<pen lineWidth="0.5"/>
<topPen lineWidth="0.5"/>
<leftPen lineWidth="0.5"/>
<bottomPen lineWidth="0.5"/>
<rightPen lineWidth="0.5"/>
</box>
<textElement textAlignment="Center" verticalAlignment="Middle">
<font fontName="SansSerif" isBold="true" pdfFontName="Helvetica-Bold"/>
</textElement>
<text><![CDATA[Sr#]]></text>
</staticText>
<staticText>
<reportElement x="82" y="0" width="67" height="30"/>
<box>
<pen lineWidth="0.5"/>
<topPen lineWidth="0.5"/>
<leftPen lineWidth="0.5"/>
<bottomPen lineWidth="0.5"/>
<rightPen lineWidth="0.5"/>
</box>
<textElement textAlignment="Center" verticalAlignment="Middle">
<font fontName="SansSerif" isBold="true" pdfFontName="Helvetica-Bold"/>
</textElement>
<text><![CDATA[Admit Card No.]]></text>
</staticText>
<staticText>
<reportElement x="149" y="0" width="154" height="30"/>
<box>
<pen lineWidth="0.5"/>
<topPen lineWidth="0.5"/>
<leftPen lineWidth="0.5"/>
<bottomPen lineWidth="0.5"/>
<rightPen lineWidth="0.5"/>
</box>
<textElement verticalAlignment="Middle">
<font fontName="SansSerif" isBold="true" pdfFontName="Helvetica-Bold"/>
</textElement>
<text><![CDATA[ Candidate Name]]></text>
</staticText>
<staticText>
<reportElement x="303" y="0" width="68" height="30"/>
<box>
<pen lineWidth="0.5"/>
<topPen lineWidth="0.5"/>
<leftPen lineWidth="0.5"/>
<bottomPen lineWidth="0.5"/>
<rightPen lineWidth="0.5"/>
</box>
<textElement textAlignment="Center" verticalAlignment="Middle">
<font fontName="SansSerif" isBold="true" pdfFontName="Helvetica-Bold"/>
</textElement>
<text><![CDATA[Contact No.]]></text>
</staticText>
<staticText>
<reportElement x="27" y="0" width="55" height="30"/>
<box>
<pen lineWidth="0.5"/>
<topPen lineWidth="0.5"/>
<leftPen lineWidth="0.5"/>
<bottomPen lineWidth="0.5"/>
<rightPen lineWidth="0.5"/>
</box>
<textElement verticalAlignment="Middle">
<font fontName="SansSerif" isBold="true" pdfFontName="Helvetica-Bold"/>
</textElement>
<text><![CDATA[ Program]]></text>
</staticText>
<staticText>
<reportElement x="371" y="0" width="229" height="30"/>
<box>
<pen lineWidth="0.5"/>
<topPen lineWidth="0.5"/>
<leftPen lineWidth="0.5"/>
<bottomPen lineWidth="0.5"/>
<rightPen lineWidth="0.5"/>
</box>
<textElement verticalAlignment="Middle">
<font fontName="SansSerif" isBold="true" pdfFontName="Helvetica-Bold"/>
</textElement>
<text><![CDATA[ Postal Adress]]></text>
</staticText>
<staticText>
<reportElement x="600" y="0" width="156" height="30"/>
<box>
<pen lineWidth="0.5"/>
<topPen lineWidth="0.5"/>
<leftPen lineWidth="0.5"/>
<bottomPen lineWidth="0.5"/>
<rightPen lineWidth="0.5"/>
</box>
<textElement verticalAlignment="Middle">
<font fontName="SansSerif" isBold="true" pdfFontName="Helvetica-Bold"/>
</textElement>
<text><![CDATA[ Email Adress]]></text>
</staticText>
<staticText>
<reportElement x="756" y="0" width="66" height="30"/>
<box>
<pen lineWidth="0.5"/>
<topPen lineWidth="0.5"/>
<leftPen lineWidth="0.5"/>
<bottomPen lineWidth="0.5"/>
<rightPen lineWidth="0.5"/>
</box>
<textElement textAlignment="Center" verticalAlignment="Middle">
<font fontName="SansSerif" isBold="true" pdfFontName="Helvetica-Bold"/>
</textElement>
<text><![CDATA[Due Date]]></text>
</staticText>
</band>
</columnHeader>
<detail>
<band height="16" splitType="Stretch">
<textField isStretchWithOverflow="true" isBlankWhenNull="true">
<reportElement stretchType="RelativeToTallestObject" x="82" y="0" width="67" height="16"/>
<box>
<pen lineWidth="0.5"/>
<topPen lineWidth="0.5"/>
<leftPen lineWidth="0.5"/>
<bottomPen lineWidth="0.5"/>
<rightPen lineWidth="0.5"/>
</box>
<textElement textAlignment="Center" verticalAlignment="Middle">
<font size="8" isBold="false" isUnderline="false"/>
</textElement>
<textFieldExpression class="java.lang.String"><![CDATA[$F{FORM_NBR}]]></textFieldExpression>
</textField>
<textField isStretchWithOverflow="true" isBlankWhenNull="true">
<reportElement stretchType="RelativeToTallestObject" x="149" y="0" width="154" height="16"/>
<box>
<pen lineWidth="0.5"/>
<topPen lineWidth="0.5"/>
<leftPen lineWidth="0.5"/>
<bottomPen lineWidth="0.5"/>
<rightPen lineWidth="0.5"/>
</box>
<textElement verticalAlignment="Middle">
<font size="8" isBold="false" isUnderline="false"/>
</textElement>
<textFieldExpression class="java.lang.String"><![CDATA[$F{NME}]]></textFieldExpression>
</textField>
<textField isStretchWithOverflow="true" isBlankWhenNull="true">
<reportElement stretchType="RelativeToTallestObject" x="1" y="0" width="26" height="16"/>
<box>
<pen lineWidth="0.5"/>
<topPen lineWidth="0.5"/>
<leftPen lineWidth="0.5"/>
<bottomPen lineWidth="0.5"/>
<rightPen lineWidth="0.5"/>
</box>
<textElement textAlignment="Center" verticalAlignment="Middle">
<font size="8" isBold="false" isUnderline="false"/>
</textElement>
<textFieldExpression class="java.lang.Integer"><![CDATA[$V{Sr#}+1]]></textFieldExpression>
</textField>
<textField isStretchWithOverflow="true" isBlankWhenNull="true">
<reportElement stretchType="RelativeToTallestObject" x="27" y="0" width="55" height="16"/>
<box>
<pen lineWidth="0.5"/>
<topPen lineWidth="0.5"/>
<leftPen lineWidth="0.5"/>
<bottomPen lineWidth="0.5"/>
<rightPen lineWidth="0.5"/>
</box>
<textElement verticalAlignment="Middle">
<font size="8"/>
</textElement>
<textFieldExpression class="java.lang.String"><![CDATA[$F{PROG_ABBREV}]]></textFieldExpression>
</textField>
<textField isStretchWithOverflow="true" isBlankWhenNull="true">
<reportElement stretchType="RelativeToTallestObject" x="303" y="0" width="68" height="16"/>
<box>
<pen lineWidth="0.5"/>
<topPen lineWidth="0.5"/>
<leftPen lineWidth="0.5"/>
<bottomPen lineWidth="0.5"/>
<rightPen lineWidth="0.5"/>
</box>
<textElement textAlignment="Center" verticalAlignment="Middle">
<font size="8"/>
</textElement>
<textFieldExpression class="java.lang.String"><![CDATA[$F{L_PHONE1_NBR}]]></textFieldExpression>
</textField>
<textField isStretchWithOverflow="true" isBlankWhenNull="true">
<reportElement stretchType="RelativeToTallestObject" x="371" y="0" width="229" height="16"/>
<box>
<pen lineWidth="0.5"/>
<topPen lineWidth="0.5"/>
<leftPen lineWidth="0.5"/>
<bottomPen lineWidth="0.5"/>
<rightPen lineWidth="0.5"/>
</box>
<textElement verticalAlignment="Middle">
<font size="8"/>
</textElement>
<textFieldExpression class="java.lang.String"><![CDATA[$F{P_ADDRESS_TXT}]]></textFieldExpression>
</textField>
<textField isStretchWithOverflow="true" isBlankWhenNull="true">
<reportElement stretchType="RelativeToTallestObject" x="600" y="0" width="156" height="16"/>
<box>
<pen lineWidth="0.5"/>
<topPen lineWidth="0.5"/>
<leftPen lineWidth="0.5"/>
<bottomPen lineWidth="0.5"/>
<rightPen lineWidth="0.5"/>
</box>
<textElement verticalAlignment="Middle">
<font size="8"/>
</textElement>
<textFieldExpression class="java.lang.String"><![CDATA[$F{P_EMAIL_TXT}]]></textFieldExpression>
</textField>
<textField isStretchWithOverflow="true" pattern="dd-MMM-yyyy" isBlankWhenNull="true">
<reportElement stretchType="RelativeToTallestObject" x="756" y="0" width="66" height="16"/>
<box>
<pen lineWidth="0.5"/>
<topPen lineWidth="0.5"/>
<leftPen lineWidth="0.5"/>
<bottomPen lineWidth="0.5"/>
<rightPen lineWidth="0.5"/>
</box>
<textElement textAlignment="Center" verticalAlignment="Middle">
<font size="8"/>
</textElement>
<textFieldExpression class="java.sql.Timestamp"><![CDATA[$F{INSTALLMENT_DUE_DTE}]]></textFieldExpression>
</textField>
</band>
</detail>
<pageFooter>
<band height="17" splitType="Stretch">
<line>
<reportElement x="0" y="4" width="822" height="1"/>
</line>
<textField pattern="dd-MM-yyyy">
<reportElement x="0" y="7" width="100" height="10"/>
<textElement>
<font fontName="SansSerif" size="7"/>
</textElement>
<textFieldExpression class="java.util.Date"><![CDATA[new
java.util.Date()]]></textFieldExpression>
</textField>
<textField>
<reportElement x="766" y="6" width="38" height="10"/>
<textElement textAlignment="Right">
<font fontName="SansSerif" size="7"/>
</textElement>
<textFieldExpression class="java.lang.String"><![CDATA["Page "+$V{PAGE_NUMBER}+" of"]]></textFieldExpression>
</textField>
<textField evaluationTime="Report">
<reportElement x="804" y="6" width="18" height="10"/>
<textElement>
<font fontName="SansSerif" size="7"/>
</textElement>
<textFieldExpression class="java.lang.String"><![CDATA[" " + $V{PAGE_NUMBER}]]></textFieldExpression>
</textField>
</band>
</pageFooter>
</jasperReport>