jasperreport 导出excel xlsx格式、word docx格式

开发工具

jasperreport5.6.0


基本原理

根据jasperreport的生命周期,jrxml---->jasperreport---->jasperprint---->目标文件,使用API生成文件。

jasperreport 导出excel xlsx格式、word docx格式_第1张图片


我选择的方式,根据jrxml生成文件,生成过程中不产生磁盘文件,文件都在内存中,如果在磁盘上生成中间文件,需要考虑线程安全问题。


导出xlsx文件样例

System.out.println(System.getProperty("user.dir"));
		String sourceFileName = "D:/MyReport.jrxml";
		
		
		//jasper文件
		JasperReport jasperReport = JasperCompileManager.compileReport(sourceFileName);
		
		
		
		Map<String,Object> params  = new HashMap<String,Object>();
		List<ReportVO<MoneyIntoInfo>>  beanCollection = CustomerReportFactoryBean.getMoneyIntoList();
		JRDataSource dataSource = new JRBeanCollectionDataSource(beanCollection, true);
		//print文件
		JasperPrint print =  JasperFillManager.fillReport(jasperReport, params, dataSource);
		
		
		//如果只注明文件名字,默认会生成在user.dir
		String fileName = "asfdsf1.xlsx";
		
		JRXlsxExporter exporter = new JRXlsxExporter();
		//设置输入项
		ExporterInput exporterInput = new SimpleExporterInput(print);
		exporter.setExporterInput(exporterInput);
		
		//设置输出项
		OutputStreamExporterOutput exporterOutput = new SimpleOutputStreamExporterOutput(fileName);
		exporter.setExporterOutput(exporterOutput);
		
		
		exporter.exportReport();



导出docx

导出docx与xlsx非常相似,只要把导出器替换为docx导出器即可

JRDocxExporter exporter=new JRDocxExporter();



web工程,页面中导出文件

日常应用中,需要在页面中导出文件,只需要将导出器的输出源设置为response.getOuputStream(),并且设置响应头的content-type和content-disposition。

注意:文件名字需要进行URL编码以符合application-x-www-form-urlencoded规定。

关健代码

String generateFileName = "我的文件.xlsx";
response.setContentType("application/vnd.ms-excel");
			response.setHeader("Content-disposition", "attachment; filename="
					+ URLEncoder.encode(generateFileName,"utf8"));

	OutputStreamExporterOutput exporterOutput = new SimpleOutputStreamExporterOutput(
						response.getOutputStream());


MyReport.jrxml文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE jasperReport PUBLIC "//JasperReports//DTD Report Design//EN"
"http://jasperreports.sourceforge.net/dtds/jasperreport.dtd">
<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="jasper_report_template" language="groovy" pageWidth="595"
pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20"
topMargin="20" bottomMargin="20">

    <queryString>
	<![CDATA[]]>
    </queryString>
    <field name="country" class="java.lang.String">
        <fieldDescription><![CDATA[country]]></fieldDescription>
    </field>
    <field name="name" class="java.lang.String">
        <fieldDescription><![CDATA[name]]></fieldDescription>
    </field>
    <columnHeader>
     <band height="23">
	<staticText>
	    <reportElement mode="Opaque" x="0" y="3" width="535"
		 height="15" backcolor="#70A9A9" />
	    <box>
	        <bottomPen lineWidth="1.0" lineColor="#CCCCCC" />
	    </box>
	    <textElement />
	    <text><![CDATA[]]> </text>
	</staticText>
	<staticText>
	    <reportElement x="414" y="3" width="121" height="15" />
	    <textElement textAlignment="Center"
	        verticalAlignment="Middle">
		<font isBold="true" />
	    </textElement>
	    <text><![CDATA[Country]]></text>
	</staticText>
	<staticText>
	    <reportElement x="0" y="3" width="136" height="15" />
	    <textElement textAlignment="Center"
	        verticalAlignment="Middle">
		<font isBold="true" />
	    </textElement>
	    <text><![CDATA[Name]]></text>
	</staticText>
     </band>
    </columnHeader>
    <detail>
        <band height="16">
	 <staticText>
	     <reportElement mode="Opaque" x="0" y="0" width="535"
		 height="14" backcolor="#E5ECF9" />
	     <box>
	            <bottomPen lineWidth="0.25" lineColor="#CCCCCC" />
	     </box>
	     <textElement />
	     <text><![CDATA[]]> </text>
	 </staticText>
	 <textField>
	     <reportElement x="414" y="0" width="121" height="15" />
	     <textElement textAlignment="Center"
	         verticalAlignment="Middle">
		 <font size="9" />
	     </textElement>
	     <textFieldExpression class="java.lang.String">
	         <![CDATA[$F{country}]]>
		</textFieldExpression>
	 </textField>
	 <textField>
	     <reportElement x="0" y="0" width="136" height="15" />
	     <textElement textAlignment="Center"
	         verticalAlignment="Middle" />
	         <textFieldExpression class="java.lang.String">
			<![CDATA[$F{name}]]>
	         </textFieldExpression>
	 </textField>
        </band>
    </detail>
</jasperReport>


你可能感兴趣的:(导出Excel,jasperReport,导出XLSX)