1、导入依赖
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.6</version>
</dependency>
2、ServiceImpl
@Override
public int ExcelBunk(MultipartFile file) throws Exception {
try {
String fileName = file.getOriginalFilename();
String suffix = fileName.substring(fileName.lastIndexOf(".") + 1);
InputStream ins = file.getInputStream();
Workbook wb = null;
if (suffix.equals("xlsx")) {
wb = new XSSFWorkbook(ins);
} else {
wb = new HSSFWorkbook(ins);
}
Sheet sheet = wb.getSheetAt(0);
List<CellRangeAddress> combineCell = getCombineCell(sheet);
String bunkLibraryId = null;
if (null != sheet) {
for (int line = 6; line <= sheet.getLastRowNum(); line++) {
Row row = sheet.getRow(line);
if (null == row) {
continue;
}
String stg;
String stg2;
String stg3;
String cellValue = getCellValue(row.getCell(0));
if (cellValue != null) {
Cell cell1 = row.getCell(0);
stg = getCellValue(cell1);
Cell cell2 = row.getCell(2);
stg2 = getCellValue(cell2);
if (isMergedRegion(sheet, line, 3) == true) {
stg3 = isCombineCell(combineCell, row.getCell(3), sheet);
} else {
Cell cell3 = row.getCell(3);
stg3 = getCellValue(cell3);
}
if (stg3 != null && !stg3.equals("") && stg != null && !stg.equals("")) {
if (bunkLibraryId == null) {
bunkLibraryId = stg3 + "," + stg + "," + stg2;
} else {
bunkLibraryId += "、" + stg3 + "," + stg + "," + stg2;
}
}
}
}
}
String s = RoomArrangement(bunkLibraryId);
if (s == "ok") {
return 1;
}
} catch (Exception e) {
logger.error("service方法:ExcelBunk()", e);
e.printStackTrace();
}
return 0;
}
public String RoomArrangement(String bunkLibraryId) {
try {
} catch (Exception e) {
logger.error("service方法:RoomArrangement", e);
e.printStackTrace();
}
return "ok";
}
private boolean isMergedRegion(Sheet sheet, int row, int column) {
int sheetMergeCount = sheet.getNumMergedRegions();
for (int i = 0; i < sheetMergeCount; i++) {
CellRangeAddress range = sheet.getMergedRegion(i);
int firstColumn = range.getFirstColumn();
int lastColumn = range.getLastColumn();
int firstRow = range.getFirstRow();
int lastRow = range.getLastRow();
if (row >= firstRow && row <= lastRow) {
if (column >= firstColumn && column <= lastColumn) {
return true;
}
}
}
return false;
}
public String isCombineCell(List<CellRangeAddress> listCombineCell, Cell cell, Sheet sheet)
throws Exception {
int firstC = 0;
int lastC = 0;
int firstR = 0;
int lastR = 0;
String cellValue = null;
for (CellRangeAddress ca : listCombineCell) {
firstC = ca.getFirstColumn();
lastC = ca.getLastColumn();
firstR = ca.getFirstRow();
lastR = ca.getLastRow();
if (cell.getRowIndex() >= firstR && cell.getRowIndex() <= lastR) {
if (cell.getColumnIndex() >= firstC && cell.getColumnIndex() <= lastC) {
Row fRow = sheet.getRow(firstR);
Cell fCell = fRow.getCell(firstC);
cellValue = getCellValue(fCell);
break;
}
} else {
cellValue = "";
}
}
return cellValue;
}
public List<CellRangeAddress> getCombineCell(Sheet sheet) {
List<CellRangeAddress> list = new ArrayList<>();
int sheetmergerCount = sheet.getNumMergedRegions();
for (int i = 0; i < sheetmergerCount; i++) {
CellRangeAddress ca = sheet.getMergedRegion(i);
list.add(ca);
}
return list;
}
private String getCellValue(Cell cell) {
String st = null;
if (cell == null || cell.equals("")) {
st = null;
} else if (cell.getCellType() == Cell.CELL_TYPE_FORMULA) {
st = cell.getStringCellValue();
} else if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) {
st = cell.getNumericCellValue() + "";
st = st.substring(0, st.lastIndexOf("."));
} else if (cell.getCellType() == Cell.CELL_TYPE_STRING) {
st = cell.getStringCellValue();
} else if (cell.getCellType() == Cell.CELL_TYPE_BLANK) {
st = cell.getStringCellValue();
}
return st;
}
3、Controller
@RequestMapping(value = "/import")
public CommonResponse excelImport(MultipartFile file) {
CommonResponse responseResult = new CommonResponse();
try {
int aa = excelService.ExcelBunk(file);
if (aa == 1) {
responseResult.setCode(200);
responseResult.setMsg("提交成功!");
} else {
responseResult.setCode(400);
responseResult.setMsg("Excel导入出错!请联系管理员。");
}
} catch (Exception e) {
logger.error("controller方法:excelImport()",e);
e.printStackTrace();
}
return responseResult;
}
4、前端(用的是Layui框架)
<button type="button" class="layui-btn" id="bedExcel"><i class="layui-icon"></i>导入Excel</button>
<script>
$(function () {
layui.use(['table', "form", "layer", 'upload'], function () {
var upload = layui.upload;
upload.render({
elem: '#bedExcel'
, url: '自己的URL路径'
, method: 'get'
, accept: 'file'
,before: function () {
upload_coverage = layer.load(2);
}
, done: function (res) {
if (res.code == 200) {
layer.close(upload_coverage);
layer.alert("绑定成功!", {closeBtn: 0}, function () {
layer.closeAll();
window.location.href = "/roomnumber/pagebunklibrary";
});
} else {
layer.close(upload_coverage);
layer.alert("Excel导入出错,请联系管理人员!", {closeBtn: 0}, function () {
layer.closeAll();
window.location.href = "/roomnumber/pagebunklibrary";
});
}
}
,error: function(){
layer.close(upload_coverage);
layer.alert("未找到服务器!!", {closeBtn: 0}, function () {
layer.closeAll();
});
}
});
}
<script/>