如果项目是个网站的话,可以先用 JS SDK 调用 LeanCloud 服务来拉取数据,然后用 Papa Parse 这样的库将数据转换成 CSV 供用户下载。完整示例如下:
<!DOCTYPE html>
<html>
<body>
<script src="https://cdn.jsdelivr.net/npm/vue@3.2.45/dist/vue.global.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/leancloud-storage@4.14.0/dist/av.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/papaparse@5.3.2/papaparse.min.js"></script>
<div id="app">
<a
:href="`data:text/plain;charset=utf-8,${encodeURIComponent(getCsv(this.todos))}`"
download="file.csv"
>
Export as CSV
</a>
</div>
<script>
AV.init({
appId: "YOUR_APP_ID",
appKey: "YOUR_APP_KEY",
serverURL: "YOUR_SERVER_URL",
});
const { createApp } = Vue;
createApp({
data() {
return {
todos: [],
};
},
mounted() {
const query = new AV.Query("Todo");
query.find().then((todos) => {
this.todos = todos.map((todo) => [
todo.get("title"),
todo.get("content"),
]);
});
},
methods: {
getCsv(data) {
return Papa.unparse(data);
},
},
}).mount("#app");
</script>
</body>
</html>