Maven
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.17</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</dependency>
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
</dependency>
@Excel
package com.cy.excel;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.math.BigDecimal;
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface Excel
{
public int sort() default Integer.MAX_VALUE;
public String name() default "";
public String dateFormat() default "";
public String dictType() default "";
public String readConverterExp() default "";
public String separator() default ",";
public int scale() default -1;
public int roundingMode() default BigDecimal.ROUND_HALF_EVEN;
public ColumnType cellType() default ColumnType.STRING;
public double height() default 14;
public double width() default 16;
public String suffix() default "";
public String defaultValue() default "";
public String prompt() default "";
public String[] combo() default {};
public boolean isExport() default true;
public String targetAttr() default "";
public boolean isStatistics() default false;
Type type() default Type.ALL;
public enum Type
{
ALL(0), EXPORT(1), IMPORT(2);
private final int value;
Type(int value)
{
this.value = value;
}
public int value()
{
return this.value;
}
}
public enum ColumnType
{
NUMERIC(0), STRING(1);
private final int value;
ColumnType(int value)
{
this.value = value;
}
public int value()
{
return this.value;
}
}
}
前端页面
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Excel表格上传</title>
<h2>文件上传</h2>
<script src="../static/js/jquery-3.2.1.min.js" type="text/javascript"></script>
<script src="../static/js/fileinput.js" type="text/javascript"></script>
<script src="../static/js/locales/zh.js" type="text/javascript"></script>
<link href="../static/css/fileinput.css" rel="stylesheet" type="text/css"/>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css">
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.5.0/css/all.css">
<script type="text/javascript">
$(function () {
initFileInput("input-file");
})
function initFileInput(ctrlName) {
var imags = new Array();
var control = $('#' + ctrlName);
control.fileinput({
language: 'zh',
uploadUrl: "/excel/userExcel",
allowedFileExtensions: ['xlsx', 'gif', 'png'],
uploadAsync: true,
showUpload: true,
showRemove : true,
showPreview : true,
showCaption: false,
browseClass: "btn btn-primary",
enctype: 'multipart/form-data',
validateInitialCount:true,
previewFileIcon: "<i class='glyphicon glyphicon-king'></i>",
msgFilesTooMany: "选择上传的文件数量({n}) 超过允许的最大数值{m}!",
}).on('filepreupload', function(event, data, previewId, index) {
var form = data.form, files = data.files, extra = data.extra,
response = data.response, reader = data.reader;
console.log('文件正在上传');
}).on("fileuploaded", function (event, data, previewId, index) {
var response = data.response;
imags.push(response.filePath)
$("#images").val(imags);
console.log('文件上传成功!'+data.id);
}).on('fileerror', function(event, data, msg) {
console.log('文件上传失败!'+data.id);
})
}
</script>
</head>
<body>
<form enctype="multipart/form-data">
<div class="file-loading">
<input id="uploadFile" name="uploadFile" type="file" multiple>
</div>
</form>
<hr>
<div id="img_show"></div>
<div class="form-group">
<label class="col-sm-2 control-label">图片上传</label>
<div class="file-loading">
<input id="input-file" name="file" multiple type="file" data-show-aption="true">
</div>
</div>
</body>
</html>