如何使用JasperReports生成密码保护的Excel报告?

我想生成一个简单的密码保护excel报告。如何使用JasperReports生成密码保护的Excel报告?

Java代码:

import java.sql.Connection; 

import java.sql.DriverManager;

import java.sql.SQLException;

import java.util.HashMap;

import net.sf.jasperreports.engine.JRException;

import net.sf.jasperreports.engine.JasperCompileManager;

import net.sf.jasperreports.engine.JasperFillManager;

import net.sf.jasperreports.engine.JasperPrint;

import net.sf.jasperreports.engine.JasperReport;

import net.sf.jasperreports.engine.export.JExcelApiExporter;

import net.sf.jasperreports.engine.export.JExcelApiExporterParameter;

public class Report {

public static void main(String[] args) {

try {

JasperReport jasperReport;

JasperPrint jasperPrint;

Connection connection = establishConnection();

HashMap jasperParameter = new HashMap();

jasperReport = JasperCompileManager.compileReport("D:\\Jasper\\report1.jrxml");

jasperPrint = JasperFillManager.fillReport(jasperReport, jasperParameter, connection);

JExcelApiExporter exporter = new JExcelApiExporter();

exporter.setParameter(JExcelApiExporterParameter.JASPER_PRINT, jasperPrint);

exporter.setParameter(JExcelApiExporterParameter.OUTPUT_FILE_NAME, "D:\\Jasper\\simple_report.xls");

exporter.setParameter(JExcelApiExporterParameter.PASSWORD, "sam");

exporter.exportReport();

} catch (JRException e) {

e.printStackTrace();

}

}

public static Connection establishConnection() {

Connection connection = null;

try {

Class.forName("com.mysql.jdbc.Driver");

String mySQL = "jdbc:mysql://localhost:3306/test";

connection = DriverManager.getConnection(mySQL, "root", "Infy123+");

connection.setAutoCommit(false);

} catch (SQLException | ClassNotFoundException exception) {

exception.printStackTrace();

}

return connection;

}

}

report1.jrxml:

<?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="report1" language="groovy" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="ac32c69e-bb80-4dc5-9139-f9b085ffa739">

<queryString language="SQL">

<![CDATA[SELECT * FROM test.item]]>

</queryString>

<field name="ITEM_NAME" class="java.lang.String">

<fieldDescription><![CDATA[]]></fieldDescription>

</field>

<field name="ITEM_DESCIPTION" class="java.lang.String">

<fieldDescription><![CDATA[]]></fieldDescription>

</field>

<field name="ITEM_AMOUNT" class="java.lang.Integer">

<fieldDescription><![CDATA[]]></fieldDescription>

</field>

<background>

<band splitType="Stretch"/>

</background>

<title>

<band height="36" splitType="Stretch"/>

</title>

<pageHeader>

<band height="35" splitType="Stretch"/>

</pageHeader>

<columnHeader>

<band height="41" splitType="Stretch">

<staticText>

<reportElement x="13" y="11" width="100" height="20" uuid="97ab8c4f-ec9e-47a1-807e-37e2adbf9d36"/>

<text><![CDATA[ITEM NAME]]></text>

</staticText>

</band>

</columnHeader>

<detail>

<band height="43" splitType="Stretch">

<textField>

<reportElement x="13" y="11" width="100" height="20" uuid="0fe760df-5c76-499f-8f79-3308680e9a37"/>

<textFieldExpression><![CDATA[$F{ITEM_NAME}]]> </textFieldExpression>

</textField>

</band>

</detail>

<columnFooter>

<band height="45" splitType="Stretch"/>

</columnFooter>

<pageFooter>

<band height="54" splitType="Stretch"/>

</pageFooter>

<summary>

<band height="42" splitType="Stretch"/>

</summary>

</jasperReport>

是如何产生的文件,但没有密码保护。

我使用JasperReports的-5.5.0的iReport 5.5.0

如何解决此问题?

回答:

您可以使用JasperReport中提供的内置POI API来禁用Excel报表的编辑。但是,excel仍然可以打开。

密码保护仅适用于Excel编辑选项。

我也有兴趣知道是否有办法密码保护excel文件本身..在Apache POI中找不到任何东西。

以上是 如何使用JasperReports生成密码保护的Excel报告? 的全部内容, 来源链接: utcz.com/qa/259233.html

回到顶部