[java]<prename"code"class"java"><prename"code"class"java"><spanstyle"font-family:Arial,Helvetica,san
[java]
最近项目经理安排任务学习jasperreport报表的设计与制作,根据提供的资料以及在网上的学习,自己实现了一个可以通过外部传入数据下载excel的报表,
因为还在初学阶段,数据源暂时是在程序中传入,还没通过数据库或者javabean传入,接下来的时间里慢慢深入。
首先是在IReport软件里设计好自己的报表图,我设计了一个很简单的报表,其实报表设计最重要的是其核心的几个过程
[java]
第一个程序是service里的下载代码
[java]
@Service("reportService")
@Path("/report")
public class ReportService {
@Autowired
private ICommonDao dao;
@Path("download")
@Produces(MediaType.TEXT_PLAIN)
@Transactional
public String downloadReport(@Context HttpServletRequest req , @Context HttpServletResponse res) throws Exception{
String designFilePath = req.getSession().getServletContext().getRealPath("/j
asper") +
File.separator + "reportTest.jrxml";
File designFile = new File(designFilePath);
if(designFile.exists()){
DataReportProcess reportProcess = new DataReportProcess();
reportProcess.process(req, res, designFile);
}
return "success";
}
}
这段代码里涉及DataReportProcess类
[java]
public class DataReportProcess extends XLSReportProcess{
/**
*从模板文件编译获得jasperReport对象
*@return JasperReport对象 jasperReport
* @throws JRException
*/
private JasperReport getJasperReport(File designFile) throws Exception{
JasperReport jasperReport = null;
JasperDesign design = JRXmlLoader.load(designFile);
jasperReport = JasperCompileManager.compileReport(design);
return jasperReport;
}
public void process(HttpServletRequest req , HttpServletResponse res, File designFile) throws Exception{
String outputFileName = "dataReport.xlsx";
Map dataMap = new HashMap();
dataMap.put("name", "张三");
Collection
dataMapList.add(dataMap);
JRMapCollectionDataSource dataSource = new JRMapCollectionDataSource(dataMapList);
JasperReport jasperReport = this.getJasperReport(designFile);
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport,null,dataSource);
this.exportWebReport(ReportProcess.Type.xlsx, res, jasperPrint , outputFileName);
}
}
这里面的数据源接口用的是JRMapCollectionDataSource,现在是写死的key value。这个类中主要是jasperrepor中涉及到的一些API
JasperReport核心API
1. JRXmlLoader(xml加载器)
里面有load方法用来加载*.jrxml文件 返回jasperDesign对象
2.JRcompile(接口)
里面定义了方法接受参数返回 jasperReport对象
3.JasperCompileManager(编译管理器)
提供了一些方法用来编译Report成文件的
JasperReport jasperReport = JasperCompileManager.compileReport(design);
4.JasperFillManager(填充管理器)
主要用来把report填充到文件里面
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, reportParams, resultSetDataSource);
5.JasperPrintManager(打印管理)
主要是把JasperPrint对象(相关的数据)打印到pdf.xml等文件中去
6.JasperExportManager(导出管理器)
主要是把JasperPrint对象(相关的数据)导出到pdf.xml等文件中去
最后一个时输出报表函数
[java]
@Override
public void exportWebReport(Type type, HttpServletResponse res, JasperPrint print, String outputFileName) throws Exception{
if(type!=null&&type.equals(ReportProcess.Type.xlsx)){
//2007 excel以上
res.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
}else{
res.setContentType("application/vnd.ms-excel");
}
//解决中文文件名并设置文件名
if(outputFileName!=null && !outputFileName.isEmpty()){
res.setHeader("charset","ISO8859-1");
res.setHeader("Content-Disposition", "attachment;filename=\"" + new String(outputFileName.getBytes(), "ISO8859-1") + "\"");
}
BufferedOutputStream outputStream =null;
outputStream = new BufferedOutputStream(res.getOutputStream());
this.exportFile(res,print,outputFileName,outputStream);
outputStream.flush();
outputStream.close();
}
[java]
//这里只要是为输出的excel文件设置各种高属性,文件名 sheetname 。。。
public void exportFile(HttpServletResponse res , JasperPrint print , String outputFileName,OutputStream outputStream) throws JRException{
JRAbstractExporter jrExporter = new JRXlsxExporter();
jrExporter.setParameter(JRExporterParameter.JASPER_PRINT, print);
jrExporter.setParameter(JRExporterParameter.OUTPUT_STREAM, outputStream);
jrExporter.setParameter(JRXlsExporterParameter.IS_DETECT_CELL_TYPE, Boolean.TRUE);
jrExporter.setParameter(JRXlsExporterParameter.IS_COLLAPSE_ROW_SPAN, Boolean.TRUE);
jrExporter.setParameter(JRXlsExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS, Boolean.TRUE);
jrExporter.setParameter(JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET, Boolean.FALSE);
jrExporter.setParameter(JRXlsExporterParameter.IS_WHITE_PAGE_BACKGROUND, Boolean.FALSE);
if(outputFileName!=null && !outputFileName.isEmpty()){
jrExporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, outputFileName);
}
System.out.println("exportFile:outputFileName outputStream " + outputFileName + " " + outputStream);
jrExporter.setParameter(JRXlsExporterParameter.SHEET_NAMES, new String[]{"信息"});
jrExporter.exportReport();
}
最后的效果是输入service的链接 会自动下载文件名为dataReport.xlsx的excel表格,里面内容很简单就是
姓名
张三
ok,很简易的demo就这样完成了,接下来会实现更通用有针对性的报表设计了