码迷,mamicode.com
首页 > 编程语言 > 详细

springboot+JasperReport 代码方式实现定制样式报表

时间:2020-06-13 13:19:30      阅读:97      评论:0      收藏:0      [点我收藏+]

标签:esper   dep   rac   nop   rar   except   pre   servlet   span   

目标

      打印档案盒标签,实现如下样式:

技术图片

 

 

 步骤:

一、Controller

 1     @GetMapping(value = {"/printlabel"})
 2 //    @RequiresPermissions("biz:container:print")
 3     public void printlabel(@RequestParam(value = "ids") String ids, HttpServletResponse response) throws Exception {
 4         try {
 5             List list=new ArrayList();
 6             Long[] idArr= Convert.toLongArray(ids);
 7             for(long id:idArr){
 8                 BizContainer container = containerService.selectContainerById(id);
 9                 if (container.getContainerType().equalsIgnoreCase("B")){
10                     if (container.getPrintContent()!=null&&container.getPrintContent().indexOf("/")>0){
11                         String[] strArr=StringUtils.split(container.getPrintContent(),"/");
12                         String strs="";
13                         for(String str:strArr){
14                             strs+=str+"\r\n\r\n";
15                         }
16                         container.setPrintContent(strs);
17                     }
18                     list.add(container);
19                 }
20             }
21 
22             //输出到文件
23 //            File f = new File("d://test.pdf");
24 //            OutputStream out = new FileOutputStream(f);
25 //            preview(columns, list, out);
26 //            out.close();
27 //            logger.info("成功~");
28 
29             //输出到前端
30             response.setContentType("application/pdf");
31             response.setHeader("Content-Disposition", "inline;");
32             final OutputStream outputStream = response.getOutputStream();
33             preview(list, outputStream);
34         } catch (Exception e) {
35             // TODO Auto-generated catch block
36             e.printStackTrace();
37         }
38     }

二、报表代码部分

1、

   /**
     * PDF打印
     *
     * @param list    数据来源
     * @param out     輸出流
     * @throws Exception
     */
    public static void preview(List<Object[]> list,
                               OutputStream out) throws Exception {
        JasperReport jp = getJasperReport();

        Map<String, Object> parameters = new HashMap<String, Object>();
        JasperPrint jasperPrint = JasperFillManager.fillReport(jp, parameters,
                new JRBeanCollectionDataSource(list));

        JasperExportManager.exportReportToPdfStream(jasperPrint, out);
    }

2、

    /**
     * 生成JasperReport对象
     *
     * @return
     * @throws JRException
     */
    @SuppressWarnings("deprecation")
    private static JasperReport getJasperReport() throws JRException {
        /**
         * 定义尺寸
         */

        final int pageWidth=595;//页面宽度
        final int pageHeight=842;//页面高度
        final int fontSize=30;//字体大小
        final int rectHeight = new Double(cmsToPixel(17.5, 72)).intValue();//外框高度
        final int rectWidth = new Double(cmsToPixel(4, 72)).intValue();//外框宽度
        final int textWidth=rectWidth;//文字宽度
        final int textHeight=fontSize+5;//文字高度
        final int X=(pageWidth-rectWidth)/2;//横坐标
        final int Y=(pageHeight-rectHeight)/2;//纵坐标
        final float lineWidth=0;//.5f;

        JasperDesign design = new JasperDesign();
        // name="statistics"
        design.setName("statistics");
        // columnCount="1"
        // printOrder="Vertical"
        design.setPrintOrder(PrintOrderEnum.VERTICAL);
        // orientation="Portrait"
        design.setOrientation(OrientationEnum.PORTRAIT);
        // pageWidth="595"
        design.setPageWidth(pageWidth);
        // pageHeight="842"
        design.setPageHeight(pageHeight);

        // columnSpacing="0"
        design.setColumnSpacing(0);
        // leftMargin="30"
        design.setLeftMargin(0);
        // rightMargin="30"
        design.setRightMargin(0);
        // topMargin="20"
        design.setTopMargin(0);
        // bottomMargin="20"
        design.setBottomMargin(0);
        // whenNoDataType="NoPages"
        design.setWhenNoDataType(WhenNoDataTypeEnum.BLANK_PAGE);
        // isTitleNewPage="false"
        design.setTitleNewPage(false);
        // isSummaryNewPage="false"
        design.setSummaryNewPage(false);

        JRDesignBand band = new JRDesignBand();
        band.setHeight(pageHeight);
        //创建rectangle

        JRDesignRectangle rectangle = new JRDesignRectangle();
        rectangle.setHeight(rectHeight);
        rectangle.setWidth(rectWidth);
        rectangle.setX(X);
        rectangle.setY(Y);
        rectangle.getLinePen().setLineColor(new Color(-16777216));
        rectangle.getLinePen().setLineStyle(LineStyleEnum.SOLID);
        band.addElement(rectangle);

        //创建printYear
        JRDesignField fieldPrintYear = new JRDesignField();
        fieldPrintYear.setName("printYear");
        fieldPrintYear.setValueClass(Integer.class);
        design.addField(fieldPrintYear);
        JRDesignTextField textFieldPrintYear = new JRDesignTextField();
        JRDesignExpression printYearExpression = new JRDesignExpression();
        printYearExpression.setText("$F{printYear}");
        printYearExpression.setValueClass(String.class);
        textFieldPrintYear.setExpression(printYearExpression);
        textFieldPrintYear.setHeight(textHeight+50);
        textFieldPrintYear.setWidth(rectWidth);
        textFieldPrintYear.setX(X);
        textFieldPrintYear.setY(Y);
        textFieldPrintYear.setHorizontalTextAlign(HorizontalTextAlignEnum.CENTER);
        textFieldPrintYear.getLineBox().getLeftPen().setLineWidth(lineWidth);
        textFieldPrintYear.getLineBox().getTopPen().setLineWidth(lineWidth);
        textFieldPrintYear.getLineBox().getRightPen().setLineWidth(lineWidth);
        textFieldPrintYear.getLineBox().getBottomPen().setLineWidth(lineWidth);
        band.addElement(textFieldPrintYear);

        //创建printContent
        JRDesignField fieldPrintContent = new JRDesignField();
        fieldPrintContent.setName("printContent");
        fieldPrintContent.setValueClass(String.class);
        design.addField(fieldPrintContent);
        JRDesignTextField textFieldPrintContent= new JRDesignTextField();
        JRDesignExpression printContentExpression = new JRDesignExpression();
        printContentExpression.setText("$F{printContent}");
        printContentExpression.setValueClass(String.class);
        textFieldPrintContent.setExpression(printContentExpression);
        textFieldPrintContent.setHeight(textHeight*4);
        textFieldPrintContent.setWidth(textWidth);
        textFieldPrintContent.setX(X);
        textFieldPrintContent.setY(textFieldPrintYear.getY()+textFieldPrintYear.getHeight());
        textFieldPrintContent.setHorizontalTextAlign(HorizontalTextAlignEnum.CENTER);
        textFieldPrintContent.getLineBox().getLeftPen().setLineWidth(lineWidth);
        textFieldPrintContent.getLineBox().getTopPen().setLineWidth(lineWidth);
        textFieldPrintContent.getLineBox().getRightPen().setLineWidth(lineWidth);
        textFieldPrintContent.getLineBox().getBottomPen().setLineWidth(lineWidth);
        band.addElement(textFieldPrintContent);

        //创建printNo
        JRDesignField fieldPrintNo = new JRDesignField();
        fieldPrintNo.setName("printNo");
        fieldPrintNo.setValueClass(String.class);
        design.addField(fieldPrintNo);
        JRDesignTextField textFieldPrintNo= new JRDesignTextField();
        JRDesignExpression printNoExpression = new JRDesignExpression();
        printNoExpression.setText("$F{printNo}");
        printNoExpression.setValueClass(String.class);
        textFieldPrintNo.setExpression(printNoExpression);
        textFieldPrintNo.setHeight(textHeight);
        textFieldPrintNo.setWidth(textWidth);
        textFieldPrintNo.setX(X);
        textFieldPrintNo.setY(textFieldPrintContent.getY()+textFieldPrintContent.getHeight());
        textFieldPrintNo.setHorizontalTextAlign(HorizontalTextAlignEnum.CENTER);
        textFieldPrintNo.getLineBox().getLeftPen().setLineWidth(lineWidth);
        textFieldPrintNo.getLineBox().getTopPen().setLineWidth(lineWidth);
        textFieldPrintNo.getLineBox().getRightPen().setLineWidth(lineWidth);
        textFieldPrintNo.getLineBox().getBottomPen().setLineWidth(lineWidth);
        band.addElement(textFieldPrintNo);

        ((JRDesignSection) design.getDetailSection()).addBand(band);

        JRDesignStyle normalStyle = new JRDesignStyle();
        normalStyle.setName("Sans_Normal");
        normalStyle.setDefault(true);
        normalStyle.setFontName("宋体");
        normalStyle.setFontSize(fontSize);
        normalStyle.setBold(true);
        normalStyle.setPdfFontName("STSong-Light");
        normalStyle.setPdfEncoding("UniGB-UCS2-H");
        normalStyle.setPdfEmbedded(false);

        design.addStyle(normalStyle);

        return JasperCompileManager.compileReport(design);
    }

 

springboot+JasperReport 代码方式实现定制样式报表

标签:esper   dep   rac   nop   rar   except   pre   servlet   span   

原文地址:https://www.cnblogs.com/simon-xie/p/13112307.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!