0
点赞
收藏
分享

微信扫一扫

JAVA 生成二维码并写到EXCEL中导出

菜菜捞捞 2022-03-17 阅读 86

1、引入依赖


        <dependency>
            <groupId>com.google.zxing</groupId>
            <artifactId>core</artifactId>
            <version>3.0.0</version>
        </dependency>

        <dependency>
            <groupId>com.google.zxing</groupId>
            <artifactId>javase</artifactId>
            <version>3.0.0</version>
        </dependency>

2、写注解类

  

@Retention(RetentionPolicy.RUNTIME)  
@Target( { java.lang.annotation.ElementType.FIELD }) 
public @interface ExcelVOAttribute {

	 /** 
     * 导出到Excel中的名字. 
     */  
    public abstract String name();  

    /** 
     * 配置列的名称,对应A,B,C,D.... 
     */  
    public abstract String column();


    public abstract String contentType()   default "";

    /** 
     * 提示信息 
     */  
    public abstract String prompt()  default "";    

    /** 
     * 设置只能选择不能输入的列内容. 
     */  
    public abstract String[] combo() default {};    

    /** 
     * 是否导出,应对需求:有时我们需要导出一份模板,这是标题需要但内容需要用户手工填写. 
     */  
    public abstract boolean isExport() default true;   
    
    /** 
     * 是否应用到导入模板上
     */  
    public abstract boolean isUsedInImport() default true;   
    
    /** 
     * 是否应用到导出上
     */  
    public abstract boolean isUsedInExport() default true;



    
}

3、实体类字段加注解

...
 @TableField(exist = false)
    @ApiModelProperty("分享码链接")
    @ExcelVOAttribute(name = "二维码",column="F",contentType = "QRCODE")
    private String  fxmUrl;
...

4、EXCEL工具类(部分代码)

     // 写入各条记录,每条记录对应excel表中的一行
                for (int i = startNo; i < endNo; i++) {
                    row = sheet.createRow(i + 3 - startNo);
                    T vo = (T) list.get(i); // 得到导出对象.
                    for (int j = 0; j < fields.size(); j++) {
                        Field field = fields.get(j);// 获得field.
                        field.setAccessible(true);// 设置实体类私有属性可访问
                        ExcelVOAttribute attr = field.getAnnotation(ExcelVOAttribute.class);
                        try {
                            // 根据ExcelVOAttribute中设置情况决定是否导出,有些情况需要保持为空,希望用户填写这一列.
                            if (attr.isExport()) {
                                cell = row.createCell(ExcelFragment.getExcelCol(attr.column()));// 创建cell
                                if("QRCODE".equals(attr.contentType())){
                                    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
                                    Map<EncodeHintType, Object> hints = new HashMap<EncodeHintType, Object>();
                                    hints.put(EncodeHintType.MARGIN, 0);
                                    hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
                                    MatrixToImageWriter.writeToStream(
                                            new MultiFormatWriter().encode(String.valueOf(field.get(vo)), BarcodeFormat.QR_CODE, 300, 300, hints), "PNG",
                                            outputStream);
                                    row.setHeight((short) 1000);
                                    HSSFClientAnchor anchor = new HSSFClientAnchor(20, 10, 1003, 245, (short) j, i + 3 - startNo, (short) j, i + 3 - startNo);
                                    patriarch.createPicture(anchor,workbook.addPicture(outputStream.toByteArray(), XSSFWorkbook.PICTURE_TYPE_JPEG));
                                }else{
                                    // 取得类型,并根据对象类型设置值.
                                    Class<?> fieldType = field.getType();
                                    if (String.class == fieldType) {
                                        cell.setCellType(HSSFCell.CELL_TYPE_STRING);
                                        cell.setCellValue(field.get(vo) == null ? "" : String.valueOf(field.get(vo)));// 如果数据存在就填入,不存在填入空格.
                                    } else if ((Integer.TYPE == fieldType)  || (Integer.class == fieldType)) {
                                        cell.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
                                        cell.setCellValue(field.get(vo) == null ? 0 : Integer.parseInt(field.get(vo).toString()));
                                    } else if ((Long.TYPE == fieldType) || (Long.class == fieldType)) {
                                        cell.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
                                        cell.setCellValue(field.get(vo) == null ? 0 : Long.parseLong(field.get(vo).toString()));
                                    } else if ((Float.TYPE == fieldType)  || (Float.class == fieldType)) {
                                        cell.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
                                        cell.setCellValue(field.get(vo) == null ? 0 : Double.parseDouble(field.get(vo).toString()));
                                    } else if ((Short.TYPE == fieldType)   || (Short.class == fieldType)) {

                                    } else if ((Double.TYPE == fieldType)   || (Double.class == fieldType)) {
                                        cell.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
                                        cell.setCellValue(field.get(vo) == null ? 0 : Double.parseDouble(field.get(vo).toString()));
                                    } else if (Character.TYPE == fieldType) {

                                    }
                                }
                            }
                        } catch (IllegalArgumentException e) {
                            e.printStackTrace();
                        } catch (IllegalAccessException e) {
                            e.printStackTrace();
                        }
                    }
                }

 5、调用

 MyExcelUtils<CrmWxUserInfo> myExcelUtils=new MyExcelUtils(CrmWxUserInfo.class);
        JsonResult jsonResult= myExcelUtils.exportExcel("wxuser-"+term.getType()+"-"+term.getId(), list, typestr+"列表");

 

最后成果:

 

举报

相关推荐

Java 生成二维码实战

Java实现二维码生成

用Java生成二维码

JAVA生成二维码-zxing

Pdf生成二维码

0 条评论