近期做了一个项目,需要导入,导出功能,现在做一下总结。
项目使用springboot+themleaf+layui技术。
pom:
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.9</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.9</version>
</dependency>
<!--导入表格需要的包 -->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
页面:
<form class="layui-form" id="form1" method="post" action="/model/import" enctype="multipart/form-data">
<div class="layui-form-item">
<label class="layui-form-label">导出模板</label>
<div class="layui-input-inline">
<button class="daoc_luyu">
<a href="/model/model1.xls">
</a>
<i class="fa fa-cloud-download"></i> 导出文件
</button>
</div>
</div>
<label class="layui-form-label">导入文件</label>
<div class="layui-input-inline">
<input type="file" id="file_sc" name="file" accept=".xls,.xlsx">
<p style="color: red">导入数据格式要求如下:</p>
</div>
<div class="layui-form-item">
<div class="layui-input-block" style="margin-left:0;" align="center">
<button class="layui-btn" type="submit">导入</button>
</div>
</div>
</form>
<script th:src="@{/layui_v2.4.5/layui.all.js}"></script>
<script th:src="@{/js/jquery-3.3.1.js}"></script>
<script th:src="@{/js/jquery.form.min.js}"></script>
<script>
$(function(){
$("#form1").bind("submit", function(){
var file=$("#file_sc").val();
if(file == ""){
alert("请选择文件!!!");
return false;
}
});
/** 验证文件是否导入成功 */
$("#form1").ajaxForm(function(data){
if(data!=null && data.isSuccess==true){
alert(data.msg);
parent.reloadTable();
var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
parent.layer.close(index); //再执行关闭
}else{
alert(data.msg);
}
});
});
后端:
@Override
public Map<String, Object> insertBuildings(MultipartFile file){
Map<String, Object> result = new HashMap<>();
result.put("isSuccess", false);
List<HtBuilding> buildList = new ArrayList<>();
String fileName = file.getOriginalFilename();
String suffix = fileName.substring(fileName.lastIndexOf(".") + 1);
InputStream ins;
try {
ins = file.getInputStream();
if (suffix.equals("xlsx")) {// 2007
} else if (suffix.equals("xls")) { // 2003
} else {
result.put("msg", "导入文件格式不正确!");
return result;
}
Row row = null;
Workbook wb= null;
try {
wb = WorkbookFactory.create(ins);
} catch (InvalidFormatException e) {
e.printStackTrace();
}
Sheet sheet = wb.getSheetAt(0);
if (null != sheet) { //表格中sheet个数
for (int line = 0; line < wb.getNumberOfSheets(); line++) {
Sheet st = wb.getSheetAt(line);
int rowNum = st.getLastRowNum();//每个sheet的行数
if(rowNum==0) {
result.put("msg", "sheet中无数据!");
return result;
}else if(rowNum>11) {
result.put("msg", "sheet中列数不符合规范!");
return result;
}
for (int rowIndex = 1; rowIndex <= rowNum; rowIndex++) {
row = st.getRow(rowIndex);
Model model= new Model();
if (null == row) {
continue;
}
String cmtyAddr =null;
String bulidingCode =null;
if (null == row.getCell(2)) {
result.put("msg", "第" + rowIndex + "行编号为空!");
return result;
}else {
row.getCell(2).setCellType(Cell.CELL_TYPE_STRING);
bulidingCode = row.getCell(2).getStringCellValue();
if (bulidingCode.length() < 3) {
while (bulidingCode.length() < 3) {
StringBuffer sb = new StringBuffer();
sb.append("0").append(bulidingCode);// 左补0
bulidingCode = sb.toString();
}
}
}
if (null == row.getCell(3)) {
result.put("msg", "第" + rowIndex + "行名称为空!");
return result;
}else {
cmtyAddr = row.getCell(3).getStringCellValue();
}
if(null==row.getCell(9)) {
isStop="1";
}else {
row.getCell(9).setCellType(Cell.CELL_TYPE_STRING);
isStop =row.getCell(9).getStringCellValue();
}
// 状态
if(null==row.getCell(10)) {
states="1";
}else {
row.getCell(10).setCellType(Cell.CELL_TYPE_STRING);
states =row.getCell(10).getStringCellValue();
}
model.setBulidingCode(bulidingCode);
model.setCmtyAddr(cmtyAddr);
int i = Mapper.getListByModel(model);
if (i > 0) {
result.put("msg", "编号" + bulidingCode+ "信息重复,请核对后再次导入!");
return result;
}
buildList.add(building);
}
int h = buildingMapper.addHbBuidings(buildList);
if (h > 0) {
result.put("isSuccess", true);
result.put("msg", "导入成功!");
} else {
result.put("msg", "导入失败!");
}
}
} else {
result.put("msg", "表格中无数据!");
}
} catch (IOException e) {
e.printStackTrace();
}
return result;
}