#I17PPP 导出数据库表增加excel格式导出

This commit is contained in:
暮光:城中城
2020-01-02 22:37:38 +08:00
parent 5300ad8d12
commit 6b54a87470
8 changed files with 140 additions and 67 deletions

View File

@@ -66,6 +66,11 @@
<artifactId>json</artifactId>
<version>20180813</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>2.1.4</version>
</dependency>
</dependencies>
<properties>

View File

@@ -2,6 +2,9 @@ package com.zyplayer.doc.db.controller;
import cn.hutool.core.io.IoUtil;
import cn.hutool.core.util.ZipUtil;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zyplayer.doc.core.annotation.AuthMan;
@@ -40,6 +43,8 @@ import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.*;
import java.util.stream.Collectors;
@@ -92,6 +97,7 @@ public class DatabaseDocController {
/**
* 获取编辑器所需的所有信息,用于自动补全
* 此接口会返回所有库表结构,介意的话请自己手动屏蔽调此接口
*
* @param sourceId
* @return
*/
@@ -242,7 +248,7 @@ public class DatabaseDocController {
}
@GetMapping(value = "/exportDatabase")
public ResponseJson exportDatabase(HttpServletResponse response, Long sourceId, String dbName, String tableNames) {
public ResponseJson exportDatabase(HttpServletResponse response, Long sourceId, String dbName, String tableNames, Integer exportType) {
this.judgeAuth(sourceId, DbAuthType.VIEW.getName(), "没有查看该库表信息的权限");
if (StringUtils.isBlank(tableNames)) {
return DocDbResponseJson.warn("请选择需要导出的表");
@@ -267,15 +273,34 @@ public class DatabaseDocController {
exportVo.setTableList(tableList);
String content = JSON.toJSONString(exportVo);
content = "var docDbDatabase = " + content;
response.setContentType("application/octet-stream");
response.addHeader("Content-Disposition", "attachment;filename=database.js");
response.setCharacterEncoding("utf-8");
// 将文件输入流写入response的输出流中
try {
IoUtil.write(response.getOutputStream(), "utf-8", true, content);
} catch (Exception e) {
e.printStackTrace();
if (Objects.equals(exportType, 1)) {
response.setContentType("application/octet-stream");
response.addHeader("Content-Disposition", "attachment;filename=database.js");
response.setCharacterEncoding("utf-8");
// 将文件输入流写入response的输出流中
try {
IoUtil.write(response.getOutputStream(), "utf-8", true, content);
} catch (Exception e) {
e.printStackTrace();
}
} else {
try {
response.setContentType("application/vnd.ms-excel");
response.setCharacterEncoding("utf-8");
String fileName = URLEncoder.encode("数据库表导出", "UTF-8");
response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
ExcelWriter excelWriter = EasyExcel.write(response.getOutputStream()).build();
WriteSheet writeSheet = EasyExcel.writerSheet(0, "表信息").head(TableInfoVo.class).build();
excelWriter.write(tableList, writeSheet);
int index = 1;
for (Map.Entry<String, List<TableColumnDescDto>> entry : columnList.entrySet()) {
writeSheet = EasyExcel.writerSheet(index++, entry.getKey()).head(TableColumnDescDto.class).build();
excelWriter.write(entry.getValue(), writeSheet);
}
excelWriter.finish();
} catch (IOException e) {
e.printStackTrace();
}
}
return DocDbResponseJson.ok();
}
@@ -315,6 +340,7 @@ public class DatabaseDocController {
/**
* 权限判断
*
* @author 暮光:城中城
*/
private void judgeAuth(Long sourceId, String authName, String noAuthInfo) {
@@ -326,6 +352,7 @@ public class DatabaseDocController {
/**
* 获取BaseMapper
*
* @author 暮光:城中城
*/
private BaseMapper getBaseMapper(Long sourceId) {
@@ -338,6 +365,7 @@ public class DatabaseDocController {
/**
* 判断查看权和获取BaseMapper
*
* @author 暮光:城中城
*/
private BaseMapper getViewAuthBaseMapper(Long sourceId) {

View File

@@ -1,50 +1,58 @@
package com.zyplayer.doc.db.controller.vo;
import java.util.List;
import com.zyplayer.doc.db.framework.db.dto.TableColumnDescDto;
public class TableColumnVo {
private List<TableColumnDescDto> columnList;
private TableInfoVo tableInfo;
public static class TableInfoVo {
private String tableName;
private String description;
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getTableName() {
return tableName;
}
public void setTableName(String tableName) {
this.tableName = tableName;
}
}
public List<TableColumnDescDto> getColumnList() {
return columnList;
}
public void setColumnList(List<TableColumnDescDto> columnList) {
this.columnList = columnList;
}
public TableInfoVo getTableInfo() {
return tableInfo;
}
public void setTableInfo(TableInfoVo tableInfo) {
this.tableInfo = tableInfo;
}
}
package com.zyplayer.doc.db.controller.vo;
import java.util.List;
import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.annotation.write.style.ColumnWidth;
import com.zyplayer.doc.db.framework.db.dto.TableColumnDescDto;
public class TableColumnVo {
private List<TableColumnDescDto> columnList;
private TableInfoVo tableInfo;
public static class TableInfoVo {
@ColumnWidth(20)
@ExcelProperty("表名")
private String tableName;
@ColumnWidth(80)
@ExcelProperty("表注释")
private String description;
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getTableName() {
return tableName;
}
public void setTableName(String tableName) {
this.tableName = tableName;
}
}
public List<TableColumnDescDto> getColumnList() {
return columnList;
}
public void setColumnList(List<TableColumnDescDto> columnList) {
this.columnList = columnList;
}
public TableInfoVo getTableInfo() {
return tableInfo;
}
public void setTableInfo(TableInfoVo tableInfo) {
this.tableInfo = tableInfo;
}
}

View File

@@ -1,13 +1,39 @@
package com.zyplayer.doc.db.framework.db.dto;
import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.annotation.write.style.ColumnWidth;
public class TableColumnDescDto {
@ColumnWidth(20)
@ExcelProperty("表名")
private String tableName;
@ColumnWidth(20)
@ExcelProperty("字段名")
private String name;
@ColumnWidth(15)
@ExcelProperty("是否自增")
private String isidenity;
@ColumnWidth(20)
@ExcelProperty("类型")
private String type;
@ColumnWidth(10)
@ExcelProperty("NULL")
private String nullable;
@ColumnWidth(10)
@ExcelProperty("长度")
private String length;
@ColumnWidth(10)
@ExcelProperty("主键")
private String ispramary;
@ColumnWidth(80)
@ExcelProperty("注释")
private String description;
public String getName() {

File diff suppressed because one or more lines are too long

View File

@@ -1,2 +1,2 @@
!function(e){function n(r){if(t[r])return t[r].exports;var o=t[r]={i:r,l:!1,exports:{}};return e[r].call(o.exports,o,o.exports,n),o.l=!0,o.exports}var r=window.webpackJsonp;window.webpackJsonp=function(t,c,u){for(var i,a,f,l=0,s=[];l<t.length;l++)a=t[l],o[a]&&s.push(o[a][0]),o[a]=0;for(i in c)Object.prototype.hasOwnProperty.call(c,i)&&(e[i]=c[i]);for(r&&r(t,c,u);s.length;)s.shift()();if(u)for(l=0;l<u.length;l++)f=n(n.s=u[l]);return f};var t={},o={2:0};n.e=function(e){function r(){i.onerror=i.onload=null,clearTimeout(a);var n=o[e];0!==n&&(n&&n[1](new Error("Loading chunk "+e+" failed.")),o[e]=void 0)}var t=o[e];if(0===t)return new Promise(function(e){e()});if(t)return t[2];var c=new Promise(function(n,r){t=o[e]=[n,r]});t[2]=c;var u=document.getElementsByTagName("head")[0],i=document.createElement("script");i.type="text/javascript",i.charset="utf-8",i.async=!0,i.timeout=12e4,n.nc&&i.setAttribute("nonce",n.nc),i.src=n.p+""+e+".js?"+{0:"c0948e5d62e2fc0666b8",1:"de31d3ffbceab902272c"}[e];var a=setTimeout(r,12e4);return i.onerror=i.onload=r,u.appendChild(i),c},n.m=e,n.c=t,n.i=function(e){return e},n.d=function(e,r,t){n.o(e,r)||Object.defineProperty(e,r,{configurable:!1,enumerable:!0,get:t})},n.n=function(e){var r=e&&e.__esModule?function(){return e.default}:function(){return e};return n.d(r,"a",r),r},n.o=function(e,n){return Object.prototype.hasOwnProperty.call(e,n)},n.p="",n.oe=function(e){throw console.error(e),e}}([]);
//# sourceMappingURL=doc-db-manifest.js.map?35f85ba5e6806d48eb6a
!function(e){function n(r){if(t[r])return t[r].exports;var o=t[r]={i:r,l:!1,exports:{}};return e[r].call(o.exports,o,o.exports,n),o.l=!0,o.exports}var r=window.webpackJsonp;window.webpackJsonp=function(t,c,u){for(var i,a,f,l=0,s=[];l<t.length;l++)a=t[l],o[a]&&s.push(o[a][0]),o[a]=0;for(i in c)Object.prototype.hasOwnProperty.call(c,i)&&(e[i]=c[i]);for(r&&r(t,c,u);s.length;)s.shift()();if(u)for(l=0;l<u.length;l++)f=n(n.s=u[l]);return f};var t={},o={2:0};n.e=function(e){function r(){i.onerror=i.onload=null,clearTimeout(a);var n=o[e];0!==n&&(n&&n[1](new Error("Loading chunk "+e+" failed.")),o[e]=void 0)}var t=o[e];if(0===t)return new Promise(function(e){e()});if(t)return t[2];var c=new Promise(function(n,r){t=o[e]=[n,r]});t[2]=c;var u=document.getElementsByTagName("head")[0],i=document.createElement("script");i.type="text/javascript",i.charset="utf-8",i.async=!0,i.timeout=12e4,n.nc&&i.setAttribute("nonce",n.nc),i.src=n.p+""+e+".js?"+{0:"39c623e12cb25cc93e4e",1:"de31d3ffbceab902272c"}[e];var a=setTimeout(r,12e4);return i.onerror=i.onload=r,u.appendChild(i),c},n.m=e,n.c=t,n.i=function(e){return e},n.d=function(e,r,t){n.o(e,r)||Object.defineProperty(e,r,{configurable:!1,enumerable:!0,get:t})},n.n=function(e){var r=e&&e.__esModule?function(){return e.default}:function(){return e};return n.d(r,"a",r),r},n.o=function(e,n){return Object.prototype.hasOwnProperty.call(e,n)},n.p="",n.oe=function(e){throw console.error(e),e}}([]);
//# sourceMappingURL=doc-db-manifest.js.map?a1c17aba1de38c50fb58

View File

@@ -8,7 +8,7 @@
<body>
<div id="app"></div>
<script type="text/javascript" src="doc-db-manifest.js?35f85ba5e6806d48eb6a"></script><script type="text/javascript" src="doc-db-vendor.js?de31d3ffbceab902272c"></script><script type="text/javascript" src="doc-db-index.js?c0948e5d62e2fc0666b8"></script></body>
<script type="text/javascript" src="doc-db-manifest.js?a1c17aba1de38c50fb58"></script><script type="text/javascript" src="doc-db-vendor.js?de31d3ffbceab902272c"></script><script type="text/javascript" src="doc-db-index.js?39c623e12cb25cc93e4e"></script></body>
</html>

View File

@@ -12,6 +12,10 @@
<el-select v-model="choiceDatabase" @change="databaseChangeEvents" filterable placeholder="请选择数据库">
<el-option v-for="item in databaseList" :key="item.dbName" :label="item.dbName" :value="item.dbName"></el-option>
</el-select>
<el-radio-group v-model="exportType">
<el-radio :label="1">HTML格式</el-radio>
<el-radio :label="2">Excel格式</el-radio>
</el-radio-group>
<el-button v-on:click="exportChoiceTable" type="primary" style="margin-left: 20px;">导出选中的表</el-button>
<a target="_blank" title="点击查看如何使用" href="http://doc.zyplayer.com/zyplayer-doc-manage/open-wiki.html?pageId=117&space=23f3f59a60824d21af9f7c3bbc9bc3cb"><i class="el-icon-info" style="color: #999;"></i></a>
</div>
@@ -37,6 +41,7 @@
choiceDatasourceId: "",
choiceDatabase: "",
choiceTable: "",
exportType: 1,
// 页面展示相关
nowDatasourceShow: {},
databaseList: [],
@@ -69,6 +74,7 @@
tableNames += this.selectTables[i].tableName;
}
window.open("zyplayer-doc-db/doc-db/exportDatabase?sourceId=" + this.choiceDatasourceId
+ "&exportType=" + this.exportType
+ "&dbName=" + this.choiceDatabase
+ "&tableNames=" + tableNames);
},