最近項目經理安排任務學習jasperreport報表的設計與製作,根據提供的資料以及在網上的學習,自己實現了一個可以通過外部傳入數據下載excel的報表,
因爲還在初學階段,數據源暫時是在程序中傳入,還沒通過數據庫或者javabean傳入,接下來的時間裏慢慢深入。
首先是在IReport軟件裏設計好自己的報表圖,我設計了一個很簡單的報表,其實報表設計最重要的是其核心的幾個過程
設計報表,數據導入,輸出報表
第一個程序是service裏的下載代碼
@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("/jasper") +
File.separator + "reportTest.jrxml";
File designFile = new File(designFilePath);
if(designFile.exists()){
DataReportProcess reportProcess = new DataReportProcess();
reportProcess.process(req, res, designFile);
}
return "success";
}
}
這段代碼裏涉及DataReportProcess類
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<String, Object> dataMap = new HashMap<String, Object>();
dataMap.put("name", "張三");
Collection<Map<String, ?>> dataMapList = new ArrayList<Map<String,?>>();
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
主要是把JasperPrint對象(相關的數據)導出到pdf.xml等文件中去
最後一個時輸出報表函數
@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();
}
//這裏只要是爲輸出的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就這樣完成了,接下來會實現更通用有針對性的報表設計了