learner
learner

Reputation: 757

java.io.UTFDataFormatException: Invalid byte 1 of 1-byte UTF-8 sequence while exporting to excel

I'm using JasperReports API to export report in Excel format.

My Java code is:

Connection connection;
FacesContext facesContext = FacesContext.getCurrentInstance();
HttpServletResponse response = (HttpServletResponse)facesContext.getExternalContext().getResponse();
InputStream reportStream = facesContext.getExternalContext().getResourceAsStream("C:/summaryOfEmployees.jasper");
ServletOutputStream servletOutputStream = response.getOutputStream();
Class.forName("oracle.jdbc.driver.OracleDriver");
Session hibernateSession = null;
hibernateSession = HibernateUtils.currentSession();
connection = hibernateSession.connection();
facesContext.responseComplete();

HashMap parameterMap = new HashMap();

JExcelApiExporter exporterXLS = new JExcelApiExporter();

JasperReport jasperReport = JasperCompileManager.compileReport("C:/summaryOfEmployees.jasper");

JasperPrint jasperPrint = JasperFillManager.fillReport("C:/summaryOfEmployees.jasper", parameterMap, connection);

exporterXLS.setParameter(JRXlsExporterParameter.JASPER_PRINT,jasperPrint);
exporterXLS.setParameter(JRXlsExporterParameter.IS_DETECT_CELL_TYPE,Boolean.TRUE);
exporterXLS.setParameter(JRXlsExporterParameter.IS_WHITE_PAGE_BACKGROUND,Boolean.FALSE);
exporterXLS.setParameter(JRXlsExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS,Boolean.TRUE);
exporterXLS.setParameter(JRXlsExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_COLUMNS,Boolean.TRUE);
exporterXLS.setParameter(JRXlsExporterParameter.OUTPUT_STREAM,servletOutputStream);
exporterXLS.exportReport();

connection.close();
servletOutputStream.flush();
servletOutputStream.close();

I get the following exception on console and the excel file appears empty

    net.sf.jasperreports.engine.JRException: java.io.UTFDataFormatException: Invalid  byte 1 of 1-byte UTF-8 sequence....
.................................
Caused by: java.io.UTFDataFormatException: Invalid byte 1 of 1-byte UTF-8 sequence.
at org.apache.xerces.impl.io.UTF8Reader.invalidByte(Unknown Source)
at org.apache.xerces.impl.io.UTF8Reader.read(Unknown Source)
at org.apache.xerces.impl.XMLEntityScanner.load(Unknown Source)
at org.apache.xerces.impl.XMLEntityScanner.skipString(Unknown Source)
at org.apache.xerces.impl.XMLVersionDetector.determineDocVersion(Unknown Source)
at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
at org.apache.xerces.parsers.AbstractSAXParser.parse(Unknown Source)
at org.apache.commons.digester.Digester.parse(Digester.java:1745)
at net.sf.jasperreports.engine.xml.JRXmlLoader.loadXML(JRXmlLoader.java:243)
... 44 more

The first line of my xml file as displayed in iReport says UTF-8 is not the issue as follows:

   <?xml version="1.0" encoding="UTF-8"?>    

Why do i get this error?

I'm desperate to be helped out.

Upvotes: 0

Views: 9680

Answers (2)

Sqeezer
Sqeezer

Reputation: 1277

If you get a similar exception while processing an html file, you can add the following line in the html head:

<meta http-equiv="Content-Type" content="text/html; charset=utl-8;" pageEncoding="utf-8" />

And this will fix the problem.

Upvotes: 1

Laurent LA RIZZA
Laurent LA RIZZA

Reputation: 2965

According to what you say, the problem seems to be in your XML file: it is declaring its encoding as UTF-8, but when Xerces tries to read your file as UTF-8, it reads an invalid sequence.

Judging by the error message, it says that you have one invalid byte in a 1-byte sequence.

UTF-8 recognises 4 (in theory 6) byte sequences: (represented as bit-fields)

  • 0xxxxxxx
  • 110xxxxx 10xxxxxx
  • 1110xxxx 10xxxxxx 10xxxxxx
  • 111110xx 10xxxxxx 10xxxxxx 10xxxxxx

So, the only case that can produce this error is that it finds a 10xxxxxx character following a 0xxxxxxx character.

So one of two cases:

  • Either your file is supposed to be UTF-8 and it is corrupted (because you edited it with notepad, or whatever)
  • Either your file is "iso-8859-1" (which uses all 256 1-byte charachers) and should be declared as such in the XML header.

Upvotes: 1

Related Questions