当前位置: 首页 > 工具软件 > Luckysheet > 使用案例 >

luckysheet使用——加载后台数据

夏侯航
2023-12-01
  • 后台实体类设计

根据luckysheet的API文档,可以看到luckysheet有提供对应的loadurl方法来加载后台数据。另外,也提供了可供luckysheet加载的数据结构,如下:

[	
	//status为1的sheet页,重点是需要提供初始化的数据celldata
	{
		"name": "Cell",
		"index": "sheet_01",
		"order":  0,
		"status": 1,
		"celldata": [{"r":0,"c":0,"v":{"v":1,"m":"1","ct":{"fa":"General","t":"n"}}}]
	}
]	

我们可以通过分析,建立与之对应的实体类:
最外层对应的sheet页对象:

public class SheetOption {
    private String name;   //工作表名称
    private String color;  //颜色
    private int status;     //状态
    private String index;      //索引
    private int hide;       //是否隐藏
    private List<Celldata> celldata = new ArrayList<Celldata>();      //数据

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }

    public int getStatus() {
        return status;
    }

    public void setStatus(int status) {
        this.status = status;
    }

    public String getIndex() {
        return index;
    }

    public void setIndex(String index) {
        this.index = index;
    }

    public int getHide() {
        return hide;
    }

    public void setHide(int hide) {
        this.hide = hide;
    }

    public List<Celldata> getCelldata() {
        return celldata;
    }

    public void setCelldata(List<Celldata> celldata) {
        this.celldata = celldata;
    }

    @Override
    public String toString() {
        return "SheetOption{" +
                "name='" + name + '\'' +
                ", color='" + color + '\'' +
                ", status=" + status +
                ", index=" + index +
                ", hide=" + hide +
                ", celldata=" + celldata +
                '}';
    }
}

然后是celldata对象:

public class Celldata {
		//r、c是行列值,从(0,0)开始
        private String r;
        private String c;
        private V v = new V();

        public Celldata() {
        }

        public Celldata(String r, String c, String m,String v) {
            this.r = r;
            this.c = c;
            this.v.setM(m);
            this.v.setV(v);

        }

        public String getR() {
            return r;
        }

        public void setR(String r) {
            this.r = r;
        }

        public String getC() {
            return c;
        }

        public void setC(String c) {
            this.c = c;
        }

        public V getV() {
            return v;
        }

        public void setV(V v) {
            this.v = v;
        }
    }

实体类V 是包含了数据及数据格式的类:

public class V {
		
        private String m;
        private String v;
        private String fc;
        private String bg;
        private CT ct = new CT();

        public String getM() {
            return m;
        }

        public void setM(String m) {
            this.m = m;
        }

        public String getV() {
            return v;
        }

        public void setV(String v) {
            this.v = v;
        }

        public CT getCt() {
            return ct;
        }

        public void setCt(CT ct) {
            this.ct = ct;
        }

    public String getFc() {
        return fc;
    }

    public void setFc(String fc) {
        this.fc = fc;
    }

    public String getBg() {
        return bg;
    }

    public void setBg(String bg) {
        this.bg = bg;
    }
}

CT类是表明此单元格的数据类型的:

public class CT {
        private String fa;
        private String t;

        public String getFa() {
            return fa;
        }

        public void setFa(String fa) {
            this.fa = fa;
        }

        public String getT() {
            return t;
        }

        public void setT(String t) {
            this.t = t;
        }
    }
  • 使用

    页面

    <div id="luckysheet" style="margin:0px;padding:0px;position:absolute;width:100%;height:100%;left: 0px;top: 0px;"></div>
    
    

    前端js

        var options = {
                        container: 'luckysheet', //luckysheet为容器id
                        title: "在线excel", // 设定表格名称
                        lang: 'zh', // 设定表格语言
                        gridKey: "excel001",
                        data: eval(returnValue.result),
                        loadUrl: "/excel/loadurl"
                    }
                    luckysheet.create(options);
    

    后台代码

     @PostMapping("/loadurl")
    @ResponseBody
    public String loadurl(HttpServletRequest request, HttpServletResponse response) {
    
        List<SheetOption> list = new ArrayList<>();
        Map keys = new HashMap();
        SheetOption stop = new SheetOption();
        //设置sheet页名
        stop.setName("options");
        //设置sheet页索引
        stop.setIndex("1");
        stop.setStatus(1);
        stop.setHide(0);
        for (int i = 0; i < 2; i++) {
            for (int j = 0; j < 2; j++) {
                //随机生成点数据
                Celldata celldata = new Celldata(i + "", j + "", i + j + "", new Random().nextInt(500) + "");
                stop.getCelldata().add(celldata);
            }
        }
    
        list.add(stop);
        return JSONArray.toJSONString(list);
    }
    
 类似资料: