1.jsp
<body>
<div class="mini-fit" id="enterpriseinformationimport-form">
<br>
<a href="${contextPath}/resources/temp/enterpriseinfoimport.xlsx">模板下载</a> (需要你自己也一个模板放在文件夹下)
<br>
<br>
<form name="form" action="${contextPath}/enterprisemanagementfunction-enterprisemanagementfunction/enterprisemanagementfunction-import-save.html"
method="post" enctype="multipart/form-data">
导入:
<input type="file" name="file" id="file" />
<button type="button" id="submit">上传文件</button>
</form>
</div>
</body>
2.引入js (jquery.form.js)
博客不能放文件,需要的留言
3.
var EnterpriseManagementFunctionimport = {
importForm: null,
attachmentUploadData: null,
init: function() {
var me = this;
mini.parse();
me.courseScheduleImportSetEvents();
},
courseScheduleImportSetEvents: function() {
var me = this;
$('#enterpriseinformationimport-form').delegate('#submit', 'click', function() {
me.submit();
});
},
submit: function() {
var form = $("form[name=form]");
var options = {
dataType: 'json',
success: function(data) {
if (data.success) {
mini.alert(data.message, '提示', function() {
window.CloseOwnerWindow();
window.location.reload();
});
} else {
alert(data.message);
}
}
};
form.ajaxSubmit(options);
},
};
$(function() {
EnterpriseManagementFunctionimport.init();
});
4.action
/*
* 导入
*/
@Action("/enterprisemanagementfunction-enterprisemanagementfunction/enterprisemanagementfunction-import")
public String enterpriseManagementFunctionimport() {
return "enterprisemanagementfunction-import";
}
@Action("/enterprisemanagementfunction-enterprisemanagementfunction/enterprisemanagementfunction-import-save")
public void importCourseschedule() throws FileUploadException, IOException {
try {
String msg = this.enterpriseManagementFunctionService.saveEnterpriseInformationImport(file);
sendMsg(true, "", msg);
} catch (final Throwable e) {
LOGGER.error("Action:EnterpriseManagementFunctionAction Method:importCourseschedule error ", e);
sendFailMsg("", "查询失败!");
}
}
@Override
public String saveEnterpriseInformationImport(final File file) {
try {
final UserSession userSession = UserThreadLocal.getUserSession();
final FileInputStream io = new FileInputStream(file);
XSSFWorkbook workbook = new XSSFWorkbook(io);
XSSFSheet sheet = workbook.getSheetAt(0);
final Iterator<Row> rowIterator = sheet.iterator();
int i = 0;
String msg = "";
while (rowIterator.hasNext()) {
i++;
final Row row = rowIterator.next();
final String email = genCellStringValue(row.getCell(7));
if (row.getRowNum() < 1) {
continue;
}
if (StringUtils.isBlank(email)) {
msg += "第" + i + "行,邮箱为空。";
continue;
}
final BaseEnterpriseManagementFunctionPojo enterpriseManagementFunction = rowToEntity(row);
enterpriseManagementFunction.setCreateUserId(userSession.getUserId());
enterpriseManagementFunction.setCreateUserName(userSession.getUserName());
enterpriseManagementFunction.setCreateTime(new Date());
enterpriseManagementFunction.setDictDeletedValue(DictDeletedType.NOT_DELTEED.getId());
enterpriseManagementFunction.setDictDeletedName(DictDeletedType.NOT_DELTEED.getNameCn());
if (!UserOperation.loginNameCheck(enterpriseManagementFunction.getEmail())) {
Long successMark = UserOperation.saveUser(enterpriseManagementFunction.getEmail(),
Constants.DEFAULT_PWD, "", "2");
if (null != successMark) {
this.save(enterpriseManagementFunction);
} else {
// 创建用户失败
msg += "第" + i + "行,创建用户失败。";
}
} else {
// 用户名重复
msg += "第" + i + "行,用户名重复。";
}
}
io.close();
return StringUtils.isNotEmpty(msg) ? msg : "导入成功";
} catch (final Exception e) {
e.printStackTrace();
}
return "导入失败";
}
private BaseEnterpriseManagementFunctionPojo rowToEntity(final Row row) {
final BaseEnterpriseManagementFunctionPojo baseEnterpriseManagementFunctionPojo =
new BaseEnterpriseManagementFunctionPojo();
baseEnterpriseManagementFunctionPojo.setAnEnterpriseName(genCellStringValue(row.getCell(0)));
baseEnterpriseManagementFunctionPojo.setAddress(genCellStringValue(row.getCell(1)));
baseEnterpriseManagementFunctionPojo.setSocialCreditCode(genCellStringValue(row.getCell(2)));
baseEnterpriseManagementFunctionPojo.setFkUnitOfNatureName(genCellStringValue(row.getCell(3)));
baseEnterpriseManagementFunctionPojo.setFkUnitOfIndustryName(genCellStringValue(row.getCell(4)));
baseEnterpriseManagementFunctionPojo
.setTheRegisteredCapital(String.valueOf(row.getCell(5).getStringCellValue()));
baseEnterpriseManagementFunctionPojo
.setUndergraduateAndGraduate(String.valueOf(row.getCell(6).getStringCellValue()));
baseEnterpriseManagementFunctionPojo.setEmail(String.valueOf(row.getCell(7).getStringCellValue()));
return baseEnterpriseManagementFunctionPojo;
}
private String genCellStringValue(final Cell cell) {
if (cell.getCellType() == Cell.CELL_TYPE_BOOLEAN) {
return String.valueOf(cell.getBooleanCellValue());
} else if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) {
final DecimalFormat df = new DecimalFormat("#");
return String.valueOf(df.format(cell.getNumericCellValue()));
} else if (cell.getCellType() == Cell.CELL_TYPE_STRING) {
return String.valueOf(cell.getStringCellValue());
} else
return String.valueOf(cell.getStringCellValue());
}