十八、jasperreport导出word,excel,pdf
·
十八、jasperreport导出word,excel,pdf
package output;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRExporter;
import net.sf.jasperreports.engine.JRExporterParameter;
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.JRPdfExporter;
import net.sf.jasperreports.engine.export.JRRtfExporter;
import net.sf.jasperreports.engine.export.JRXlsExporterParameter;
import net.sf.jasperreports.engine.export.ooxml.JRDocxExporter;
import net.sf.jasperreports.engine.util.JRLoader;
public class JasperReportOutput {
public static void main(String[] args) {
try {
JasperReportOutput testReport = new JasperReportOutput();
testReport.outputWord();
testReport.outputExcel();
testReport.outputPdf();
} catch(Exception ex) {
ex.printStackTrace();
}
}
/**
* 导出word
*
* @throws ClassNotFoundException
* @throws JRException
* @throws SQLException
* @throws FileNotFoundException
*/
@SuppressWarnings({ "unchecked", "rawtypes" })
public void outputWord() throws ClassNotFoundException, JRException, SQLException, FileNotFoundException {
JasperReport report = (JasperReport)JRLoader.loadObject(new File("C:\\Users\\A67512\\Desktop\\iReport\\Data\\21_JSP中显示jasperreport报表\\21_jsp_view.jasper"));
Map parameters = new HashMap();
parameters.put("para1", "AAA");
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/ireport", "root", "123456");
System.out.println("Connection1 Successful!");
JasperPrint jasperPrint = JasperFillManager.fillReport(report, parameters, conn);
OutputStream ouputStream = new FileOutputStream(new File("C:\\Users\\A67512\\Desktop\\19_output.doc"));
JRDocxExporter exporter = new JRDocxExporter();
exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, ouputStream);
exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
exporter.setParameter(JRXlsExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS, Boolean.TRUE); // 删除记录最下面的空行
exporter.setParameter(JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET, Boolean.FALSE);// 删除多余的ColumnHeader
exporter.setParameter(JRXlsExporterParameter.IS_WHITE_PAGE_BACKGROUND, Boolean.FALSE);// 显示边框
exporter.setParameter(JRExporterParameter.CHARACTER_ENCODING, "GB2312");
exporter.exportReport();
conn.close();
}
/**
* 导出Excel
*
* @throws ClassNotFoundException
* @throws JRException
* @throws SQLException
* @throws FileNotFoundException
*/
@SuppressWarnings({ "unchecked", "rawtypes" })
public void outputExcel() throws ClassNotFoundException, SQLException, FileNotFoundException, JRException {
JasperReport report = (JasperReport)JRLoader.loadObject(new File("C:\\Users\\A67512\\Desktop\\iReport\\Data\\21_JSP中显示jasperreport报表\\21_jsp_view.jasper"));
Map parameters = new HashMap();
parameters.put("para1", "AAA");
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/ireport", "root", "123456");
System.out.println("Connection1 Successful!");
JasperPrint jasperPrint = JasperFillManager.fillReport(report, parameters, conn);
OutputStream ouputStream = new FileOutputStream(new File("C:\\Users\\A67512\\Desktop\\19_output.xls"));
JExcelApiExporter exporter = new JExcelApiExporter();
exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, ouputStream);
exporter.setParameter(JRXlsExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS, Boolean.TRUE); // 删除记录最下面的空行
exporter.setParameter(JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET, Boolean.FALSE);// 删除多余的ColumnHeader
exporter.setParameter(JRXlsExporterParameter.IS_WHITE_PAGE_BACKGROUND, Boolean.FALSE);// 显示边框
exporter.exportReport();
conn.close();
}
/**
* 导出Pdf
*
* @throws ClassNotFoundException
* @throws JRException
* @throws SQLException
* @throws FileNotFoundException
*/
@SuppressWarnings({ "unchecked", "rawtypes" })
public void outputPdf() throws ClassNotFoundException, SQLException, FileNotFoundException, JRException {
JasperReport report = (JasperReport)JRLoader.loadObject(new File("C:\\Users\\A67512\\Desktop\\iReport\\Data\\21_JSP中显示jasperreport报表\\21_jsp_view.jasper"));
Map parameters = new HashMap();
parameters.put("para1", "AAA");
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/ireport", "root", "123456");
System.out.println("Connection1 Successful!");
JasperPrint jasperPrint = JasperFillManager.fillReport(report, parameters, conn);
OutputStream ouputStream = new FileOutputStream(new File("C:\\Users\\A67512\\Desktop\\19_output.pdf"));
JRPdfExporter exporter = new JRPdfExporter();
exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, ouputStream);
exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
exporter.setParameter(JRXlsExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS, Boolean.TRUE); // 删除记录最下面的空行
exporter.setParameter(JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET, Boolean.FALSE);// 删除多余的ColumnHeader
exporter.setParameter(JRXlsExporterParameter.IS_WHITE_PAGE_BACKGROUND, Boolean.FALSE);// 显示边框
exporter.exportReport();
conn.close();
}
}
PDF中文字体必须设置参数
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-iDe1cwnj-1611106608019)(F:\IReport.assets\QQ截图20210120093449.png)]](https://i-blog.csdnimg.cn/blog_migrate/4292417fb9df7c4d653721c390749724.png#pic_center)
更多推荐
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-ufVE2Fti-1611106608016)(F:\IReport.assets\image-20210120091336248.png)]](https://i-blog.csdnimg.cn/blog_migrate/bca51f090597cb3baeba49f48849b773.png#pic_center)


所有评论(0)