lswx2016 Posted March 16, 2017 Share Posted March 16, 2017 Recently,i worked on the jasperreport in springmvc.I have successfully exported the excel file.But When i opened the file, it poped up a warning:File format and extension mismatch。Andbody came across the same situation?Thx for any help.The Code is here:public void generateReportEXCEL (List<JasperPrint> jasperPrintList, HttpServletRequest req, HttpServletResponse resp) throws IOException, JRException {resp.reset();resp.resetBuffer();resp.setContentType("application/vnd.ms-excel");resp.setHeader("Content-Disposition", "attachment;filename="test.xls"");ServletOutputStream outputStream = resp.getOutputStream(); JRXlsxExporter exporter = new JRXlsxExporter();exporter.setExporterInput(SimpleExporterInput.getInstance(jasperPrintList));exporter.setExporterOutput(new SimpleOutputStreamExporterOutput(outputStream));SimpleXlsxReportConfiguration configuration = new SimpleXlsxReportConfiguration(); configuration.setDetectCellType(true);//Set configuration as you like it!!configuration.setCollapseRowSpan(false);configuration.setWhitePageBackground(false); exporter.setConfiguration(configuration);exporter.exportReport();outputStream.flush();outputStream.close();}The pom.xml relatived:<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>SpringJasper</groupId> <artifactId>SpringJasperMaven</artifactId> <packaging>war</packaging> <version>0.0.1-SNAPSHOT</version> <name>SpringJasperMaven Maven Webapp</name> <url>http://maven.apache.org</url> <dependencies> <dependency><groupId>org.springframework</groupId><artifactId>spring-core</artifactId><version>4.1.4.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-web</artifactId><version>4.1.4.RELEASE</version></dependency> <dependency><groupId>org.springframework</groupId><artifactId>spring-tx</artifactId><version>4.1.4.RELEASE</version></dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>4.1.4.RELEASE</version> </dependency> <dependency><groupId>net.sf.jasperreports</groupId><artifactId>jasperreports</artifactId><version>6.3.0</version></dependency> <dependency><groupId>org.apache.poi</groupId><artifactId>poi</artifactId><version>3.9</version></dependency> <dependency><groupId>tomcat</groupId><artifactId>jasper-compiler-jdt</artifactId><version>5.5.23</version></dependency> <dependency><groupId>org.hibernate</groupId><artifactId>hibernate-validator</artifactId><version>4.3.1.Final</version></dependency> <dependency><groupId>org.hibernate</groupId><artifactId>hibernate-annotations</artifactId><version>3.2.1.ga</version></dependency> <dependency><groupId>javax.validation</groupId><artifactId>validation-api</artifactId><version>1.0.0.GA</version></dependency> <dependency> <groupId>javax.transaction</groupId> <artifactId>jta</artifactId> <version>1.1</version></dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> <scope>provided</scope> </dependency> <dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>5.1.6</version></dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> </dependencies> <build> <finalName>SpringJasperMaven</finalName> </build></project> Link to comment Share on other sites More sharing options...
Solution szaharia Posted March 16, 2017 Solution Share Posted March 16, 2017 The JRXlsxExporter provides XLSX output documents that should be saved with the ".xlsx" extension. In your example the Excel document is saved as "test.xls", therefore the ".xls" file extension does not properly match the file content. Try to export it as "test.xlsx" instead:resp.setHeader("Content-Disposition", "attachment;filename="test.xlsx""); Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now