我正在使用JasperReports API以Excel格式导出报告。
我的Java代码是:
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();
我在控制台上收到以下异常,并且 excel 文件显示为空
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
iReport中显示的我的 xml 文件的第一行表示UTF-8不是问题,如下所示:
<?xml version="1.0" encoding="UTF-8"?>
为什么我会收到此错误?
我很想得到帮助。