效果图:
思路:前台 使用bootstrap-fileInput插件,将excel文件上传到服务器对应地址,然后后台java读取服务器对应地址的这个excel文件,然后将数据上传到数据库(使用MyBatis框架)
html代码:
bootstrap-fileInput初始化js代码:
其中uploadUrl参数是后台controller的方法地址
initFileUpload('uploadFile');
function initFileUpload(idName){
$("#" + idName + "").fileinput({
language: 'zh', //设置语言
uploadUrl: '../../gis/poi/upload', //上传的地址
allowedFileExtensions : ['xls', 'xlsx'],
uploadAsync : false,
browseOnZoneClick: true,
dropZoneEnabled : true,
showPreview : true,
enctype: 'multipart/form-data',
showCaption: true,//是否显示标题
browseClass: "btn btn-primary", //按钮样式
dropZoneEnabled: false,
showPreview: true,
uploadExtraData: function(type) { //额外参数的关键点
var obj = {};
obj.type = $('input:radio[name="type"]:checked').val();
return obj;
},
previewFileIcon: "",
})
}
点击上传按钮后 执行的js代码
//出发fileInput上传方法
$('#uploadFile').fileinput('upload');
//上传成功回调方法
$('#uploadFile').on('filebatchuploadsuccess', function(event, filestack, extraData) {
var data = filestack.response;
if (data.code == "200") {
alert("上传成功")
}
});
controller层:
@PostMapping("/upload")
@ResponseBody
public Map upload(@RequestParam("file") MultipartFile file,HttpServletRequest request) {
Map map = new HashMap<>();
gisPoiService.importData(request,file);
map.put("code",200);
return map;
}
service实现层:
/**
* 导入数据
*
* @param id
* @return
*/
@Override
public R importData(HttpServletRequest request, MultipartFile file) {
List lst = this.upLoadFile(request, file);
this.insertData(request, lst);
return CommonUtils.msg("导入完成");
}
// 上传文件到服务器
private List upLoadFile(HttpServletRequest request, MultipartFile file) {
List lst = new ArrayList();
String fileName = file.getOriginalFilename();
String[] sArray = fileName.split("\\.");
Date dNow = new Date();
SimpleDateFormat ft = new SimpleDateFormat("yyyyMMddHHmmss");
String dt = ft.format(dNow);
String uploadFileName = sArray[0] + "_" + dt + "." + sArray[1];
String filePath = request.getServletContext().getRealPath("poiUploadedFile/") + uploadFileName;
File dest = new File(filePath);
try {
file.transferTo(dest);
} catch (IOException e) {
}
lst.add(filePath);
lst.add(dt);
return lst;
}
// 插入数据
// 1.新建
// 0.追加
private void insertData(HttpServletRequest request, List lst) {
String strTime = lst.get(1);
String strServerFilePath = lst.get(0);
String type = request.getParameter("type");
String tableName1 = this.getLatestVerisonName();
this.batchSaveGisPoi(tableName1, strServerFilePath);
}
/**
* 批量新增
*
* @param gisPoi
* @return
*/
public R batchSaveGisPoi(String strTableName, String filePath) {
int count = 0;
List lstGisPoiEntity = this.readExcel(filePath);
for (int i = 0; i < lstGisPoiEntity.size(); i++) {
GisPoiEntity gispoi = lstGisPoiEntity.get(i);
Map args = new HashMap();
args.put("tabName", strTableName);
args.put("name", addyinhao(gispoi.getName()));
args.put("x", addyinhao(gispoi.getX()));
args.put("y", addyinhao(gispoi.getY()));
args.put("xzqdm", addyinhao(gispoi.getXzqdm()));
args.put("xzqmc", addyinhao(gispoi.getXzqmc()));
args.put("address", addyinhao(gispoi.getAddress()));
args.put("city", addyinhao(gispoi.getCity()));
args.put("typeName", addyinhao(gispoi.getTypeName()));
count += gisPoiMapper.saveFromTable(args);
}
return CommonUtils.msg(count);
}
实体entity层:
实体层,我就不贴了, 贴出来的代码的目的是为了给大家有个参考,需要大家改一改,才能在自己的项目上使用的。
mapper层:
mapper.xml:
INSERT INTO ${tabName} (
name,
x,
y,
address,
city,
xzqmc,
xzqdm,
type
)
VALUES (
${name},
${x},
${y},
${address},
${city},
${xzqmc},
${xzqdm},
(select macro_id from sys_macro where sys_macro.name=${typeName})
)