package com.action;
import java.io.OutputStream;
import java.sql.Connection;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.sf.jasperreports.engine.JRAbstractExporter;
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.JRCsvExporter;
import net.sf.jasperreports.engine.export.JRHtmlExporter;
import net.sf.jasperreports.engine.export.JRHtmlExporterParameter;
import net.sf.jasperreports.engine.export.JRPdfExporter;
import net.sf.jasperreports.engine.export.JRRtfExporter;
import net.sf.jasperreports.engine.export.JRTextExporter;
import net.sf.jasperreports.engine.export.JRTextExporterParameter;
import net.sf.jasperreports.engine.export.JRXlsAbstractExporterParameter;
import net.sf.jasperreports.engine.util.JRLoader;
import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.util.StringUtils;
import com.opensymphony.xwork2.ActionSupport;
public class HelloAction extends ActionSupport{
/**
*
*/
private static final long serialVersionUID = 1L;
@Autowired
private JdbcTemplate jdbcTemplate = null;
/**
* 导出xls
* @return
* @throws Exception
*/
public String exportToXls(JasperPrint jasperPrint,HttpServletResponse response) throws Exception {
OutputStream output = null;
output=response.getOutputStream();
JRAbstractExporter exporter = new JExcelApiExporter();
exporter.setParameter(JRExporterParameter.JASPER_PRINT,jasperPrint);
exporter.setParameter(JRExporterParameter.OUTPUT_STREAM,output);
exporter.setParameter(JRXlsAbstractExporterParameter.IS_ONE_PAGE_PER_SHEET,Boolean.FALSE);
// 不显示边框
exporter.setParameter(JRXlsAbstractExporterParameter.IS_WHITE_PAGE_BACKGROUND,Boolean.TRUE);
// 删除记录最下面的空行
exporter.setParameter(JRXlsAbstractExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS,Boolean.TRUE);
exporter.exportReport();
output.flush();
return null;
}
/**
* 导出pdf
* @return
* @throws Exception
*/
public String exportToPdf(JasperPrint jasperPrint,HttpServletResponse response) throws Exception {
OutputStream output = null;
output=response.getOutputStream();
JRAbstractExporter exporter = new JRPdfExporter();
exporter.setParameter(JRExporterParameter.JASPER_PRINT,jasperPrint);
exporter.setParameter(JRExporterParameter.OUTPUT_STREAM,output);
exporter.exportReport();
output.flush();
return null;
}
/**
* 导出html
* @return
* @throws Exception
*/
public String exportToHtml(JasperPrint jasperPrint,HttpServletResponse response) throws Exception {
OutputStream output = null;
output=response.getOutputStream();
JRAbstractExporter exporter = new JRHtmlExporter();
exporter.setParameter(JRExporterParameter.JASPER_PRINT,jasperPrint);
exporter.setParameter(JRExporterParameter.OUTPUT_STREAM,output);
exporter.setParameter(JRHtmlExporterParameter.IS_USING_IMAGES_TO_ALIGN,false);
exporter.setParameter(JRExporterParameter.CHARACTER_ENCODING,"UTF-8");
exporter.exportReport();
output.flush();
return null;
}
/**
* 导出word
* @return
* @throws Exception
*/
public String exportToWord(JasperPrint jasperPrint,HttpServletResponse response) throws Exception {
JRAbstractExporter exporter = new JRRtfExporter();
exporter.setParameter(JRExporterParameter.JASPER_PRINT,jasperPrint);
OutputStream output = null;
output=response.getOutputStream();
exporter.setParameter(JRExporterParameter.OUTPUT_STREAM,output);
exporter.exportReport();
output.flush();
return null;
}
/**
* 导出csv
* @return
* @throws Exception
*/
public String exportToCsv(JasperPrint jasperPrint,HttpServletResponse response) throws Exception {
JRAbstractExporter exporter = new JRCsvExporter();
exporter.setParameter(JRExporterParameter.JASPER_PRINT,jasperPrint);
OutputStream output = null;
output=response.getOutputStream();
exporter.setParameter(JRExporterParameter.OUTPUT_STREAM,output);
// exporter.setParameter(JRCsvExporterParameter.FIELD_DELIMITER,",");
// exporter.setParameter(JRCsvExporterParameter.RECORD_DELIMITER,"\n");
exporter.exportReport();
output.flush();
return null;
}
/**
* 导出txt
* @return
* @throws Exception
*/
public String exportToTxt(JasperPrint jasperPrint,HttpServletResponse response) throws Exception {
JRAbstractExporter exporter = new JRTextExporter();
exporter.setParameter(JRExporterParameter.JASPER_PRINT,jasperPrint);
OutputStream output = null;
output=response.getOutputStream();
exporter.setParameter(JRExporterParameter.OUTPUT_STREAM,output);
exporter.setParameter(JRTextExporterParameter.CHARACTER_WIDTH,new Float(5));
exporter.setParameter(JRTextExporterParameter.CHARACTER_HEIGHT,new Float(35));
exporter.exportReport();
output.flush();
return null;
}
public String exportReport()throws Exception {
HttpServletRequest request = ServletActionContext.getRequest();
HttpServletResponse response = ServletActionContext.getResponse();
String id= request.getParameter("id");
String name= request.getParameter("name");
String type= request.getParameter("type");
Connection con = jdbcTemplate.getDataSource().getConnection();
String realPath = request.getRealPath("/report/report3.jasper");
StringBuilder sb = new StringBuilder("select * from student where 1=1");
if (StringUtils.hasLength(id)) {
sb.append(" and id="+id);
}
if (!StringUtils.hasLength(name)) {
name = new String(("学生基本信息统计表."+type).getBytes("utf-8"),"ISO8859-1");
}
response.setContentType("application/octet-stream;charset=gbk");
response.setHeader("Content-disposition","attachment;filename=\""+name+"\"");
Map<String,String> parameters = new HashMap<String,String>();
parameters.put("strSql",sb.toString());
JasperReport jasperReport = (JasperReport)JRLoader.loadObject(realPath);
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, con);
if ("xls".equals(type)) {
this.exportToXls(jasperPrint, response);
}else if("pdf".equals(type)){
this.exportToPdf(jasperPrint, response);
}else if("html".equals(type)){
this.exportToHtml(jasperPrint, response);
}else if("doc".equals(type)){
this.exportToWord(jasperPrint, response);
}else if("csv".equals(type)){
this.exportToCsv(jasperPrint, response);
}else if("txt".equals(type)){
this.exportToTxt(jasperPrint, response);
}
return null;
}
}
import java.io.OutputStream;
import java.sql.Connection;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.sf.jasperreports.engine.JRAbstractExporter;
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.JRCsvExporter;
import net.sf.jasperreports.engine.export.JRHtmlExporter;
import net.sf.jasperreports.engine.export.JRHtmlExporterParameter;
import net.sf.jasperreports.engine.export.JRPdfExporter;
import net.sf.jasperreports.engine.export.JRRtfExporter;
import net.sf.jasperreports.engine.export.JRTextExporter;
import net.sf.jasperreports.engine.export.JRTextExporterParameter;
import net.sf.jasperreports.engine.export.JRXlsAbstractExporterParameter;
import net.sf.jasperreports.engine.util.JRLoader;
import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.util.StringUtils;
import com.opensymphony.xwork2.ActionSupport;
public class HelloAction extends ActionSupport{
/**
*
*/
private static final long serialVersionUID = 1L;
@Autowired
private JdbcTemplate jdbcTemplate = null;
/**
* 导出xls
* @return
* @throws Exception
*/
public String exportToXls(JasperPrint jasperPrint,HttpServletResponse response) throws Exception {
OutputStream output = null;
output=response.getOutputStream();
JRAbstractExporter exporter = new JExcelApiExporter();
exporter.setParameter(JRExporterParameter.JASPER_PRINT,jasperPrint);
exporter.setParameter(JRExporterParameter.OUTPUT_STREAM,output);
exporter.setParameter(JRXlsAbstractExporterParameter.IS_ONE_PAGE_PER_SHEET,Boolean.FALSE);
// 不显示边框
exporter.setParameter(JRXlsAbstractExporterParameter.IS_WHITE_PAGE_BACKGROUND,Boolean.TRUE);
// 删除记录最下面的空行
exporter.setParameter(JRXlsAbstractExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS,Boolean.TRUE);
exporter.exportReport();
output.flush();
return null;
}
/**
* 导出pdf
* @return
* @throws Exception
*/
public String exportToPdf(JasperPrint jasperPrint,HttpServletResponse response) throws Exception {
OutputStream output = null;
output=response.getOutputStream();
JRAbstractExporter exporter = new JRPdfExporter();
exporter.setParameter(JRExporterParameter.JASPER_PRINT,jasperPrint);
exporter.setParameter(JRExporterParameter.OUTPUT_STREAM,output);
exporter.exportReport();
output.flush();
return null;
}
/**
* 导出html
* @return
* @throws Exception
*/
public String exportToHtml(JasperPrint jasperPrint,HttpServletResponse response) throws Exception {
OutputStream output = null;
output=response.getOutputStream();
JRAbstractExporter exporter = new JRHtmlExporter();
exporter.setParameter(JRExporterParameter.JASPER_PRINT,jasperPrint);
exporter.setParameter(JRExporterParameter.OUTPUT_STREAM,output);
exporter.setParameter(JRHtmlExporterParameter.IS_USING_IMAGES_TO_ALIGN,false);
exporter.setParameter(JRExporterParameter.CHARACTER_ENCODING,"UTF-8");
exporter.exportReport();
output.flush();
return null;
}
/**
* 导出word
* @return
* @throws Exception
*/
public String exportToWord(JasperPrint jasperPrint,HttpServletResponse response) throws Exception {
JRAbstractExporter exporter = new JRRtfExporter();
exporter.setParameter(JRExporterParameter.JASPER_PRINT,jasperPrint);
OutputStream output = null;
output=response.getOutputStream();
exporter.setParameter(JRExporterParameter.OUTPUT_STREAM,output);
exporter.exportReport();
output.flush();
return null;
}
/**
* 导出csv
* @return
* @throws Exception
*/
public String exportToCsv(JasperPrint jasperPrint,HttpServletResponse response) throws Exception {
JRAbstractExporter exporter = new JRCsvExporter();
exporter.setParameter(JRExporterParameter.JASPER_PRINT,jasperPrint);
OutputStream output = null;
output=response.getOutputStream();
exporter.setParameter(JRExporterParameter.OUTPUT_STREAM,output);
// exporter.setParameter(JRCsvExporterParameter.FIELD_DELIMITER,",");
// exporter.setParameter(JRCsvExporterParameter.RECORD_DELIMITER,"\n");
exporter.exportReport();
output.flush();
return null;
}
/**
* 导出txt
* @return
* @throws Exception
*/
public String exportToTxt(JasperPrint jasperPrint,HttpServletResponse response) throws Exception {
JRAbstractExporter exporter = new JRTextExporter();
exporter.setParameter(JRExporterParameter.JASPER_PRINT,jasperPrint);
OutputStream output = null;
output=response.getOutputStream();
exporter.setParameter(JRExporterParameter.OUTPUT_STREAM,output);
exporter.setParameter(JRTextExporterParameter.CHARACTER_WIDTH,new Float(5));
exporter.setParameter(JRTextExporterParameter.CHARACTER_HEIGHT,new Float(35));
exporter.exportReport();
output.flush();
return null;
}
public String exportReport()throws Exception {
HttpServletRequest request = ServletActionContext.getRequest();
HttpServletResponse response = ServletActionContext.getResponse();
String id= request.getParameter("id");
String name= request.getParameter("name");
String type= request.getParameter("type");
Connection con = jdbcTemplate.getDataSource().getConnection();
String realPath = request.getRealPath("/report/report3.jasper");
StringBuilder sb = new StringBuilder("select * from student where 1=1");
if (StringUtils.hasLength(id)) {
sb.append(" and id="+id);
}
if (!StringUtils.hasLength(name)) {
name = new String(("学生基本信息统计表."+type).getBytes("utf-8"),"ISO8859-1");
}
response.setContentType("application/octet-stream;charset=gbk");
response.setHeader("Content-disposition","attachment;filename=\""+name+"\"");
Map<String,String> parameters = new HashMap<String,String>();
parameters.put("strSql",sb.toString());
JasperReport jasperReport = (JasperReport)JRLoader.loadObject(realPath);
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, con);
if ("xls".equals(type)) {
this.exportToXls(jasperPrint, response);
}else if("pdf".equals(type)){
this.exportToPdf(jasperPrint, response);
}else if("html".equals(type)){
this.exportToHtml(jasperPrint, response);
}else if("doc".equals(type)){
this.exportToWord(jasperPrint, response);
}else if("csv".equals(type)){
this.exportToCsv(jasperPrint, response);
}else if("txt".equals(type)){
this.exportToTxt(jasperPrint, response);
}
return null;
}
}