EXCEL文件上传,内容格式验证(POI)
2017-10-25 16:01
483 查看
jsp页面标签
<!-- 上传走行信息的Excel -->
<form id="runInfoForm" class="collection_form" action="${basePath}/cs/uploadRunInfo" enctype="multipart/form-data" target="hiddenIFrame" method="post">
<!-- 选择按钮 -->
<div class="file">
<input type="file" name="file" class="files" id="file"/>
</div>
<!-- 上传按钮 -->
<a href="javascript:void(0)" class="templateDownload" id="btn">上传</a>
<!-- 模板下载 -->
<a href="${basePath }/fileDisk/cs/服务器上文件名.xlsx" class="templateDownload">模板下载</a>
</form>
<iframe id='hiddenIFrame' name='hiddenIFrame' style="display:none;"></iframe>
/**
* controller走行xml上传
* @param file
* @param request
* @param model
* @return
* @throws Exception
*/
@RequestMapping(value="/uploadRunInfo",produces="text/html;charset=UTF-8")
@ResponseBody
public String upload(MultipartFile file,HttpServletRequest request,Model model) throws Exception{
if(null == file || (null != file && 0 == file.getSize())){
return "请选择一个文件!";
}
String fileName = file.getOriginalFilename();
//校验文件名
boolean flag = csRunInfoService.validateName(fileName);
if(false == flag){
return "文件名错误,请核对文件名!";
}
boolean validateDate = csRunInfoService.validateDate(fileName);
if(false == validateDate){
return "上传失败,只能上传当月走行记录!";
}
//校验文件是否已经存在
4000
String path = request.getSession().getServletContext()
.getRealPath("/fileDisk/cs");
List<String> list = csRunInfoService.fileNameList(path);
boolean hasFile = list.contains(fileName);
logger.debug("@@@@@ 服务器中是否已经有文件:" + hasFile);
File newFile = new File(path + "/" + fileName);
FileUtils.copyInputStreamToFile(file.getInputStream(), newFile);
if(!newFile.exists()){
return "上传失败!";
}else{
//解析走行记录xml
String newPath = path + "/" + fileName;
String information = csRunInfoService.parseRunInfoXml(newPath, fileName);
return information;
}
}
/**
*service 解析走行xml
*/
@Override
public String parseRunInfoXml(String newPath,String fileName) throws Exception {
/**
* 类型
CELL_TYPE_BLANK 空值(cell不为空)
CELL_TYPE_BOOLEAN 布尔类型
CELL_TYPE_FORMULA 公式类型
CELL_TYPE_STRING 字符串类型
CELL_TYPE_NUMERIC 数值类型
CELL_TYPE_ERROR 错误类型
*/
String information="上传成功";
FileInputStream is = new FileInputStream(new File(newPath));
XSSFWorkbook xwb = new XSSFWorkbook(is);
XSSFSheet sheet = xwb.getSheetAt(0);
XSSFRow row = null;
String cell;
List<CsRunInfo> list = new ArrayList<CsRunInfo>();
Date d = new Date();
Calendar c = Calendar.getInstance();
SimpleDateFormat yearFormat = new SimpleDateFormat("yyyy");
String year = yearFormat.format(c.getTime());
/*String year = yearFormat.format(d); */
SimpleDateFormat monthFormat = new SimpleDateFormat("MM");
c.add(Calendar.MONTH, -1);
String month = monthFormat.format(c.getTime());
/*String month = monthFormat.format(d);*/
String month2 = month.substring(1,2);
row = sheet.getRow(0);
String stringCellValue = row.getCell(0).getStringCellValue();
String cutYear = stringCellValue.substring(0,4);
int yearNum = stringCellValue.indexOf("年");
int monthNum = stringCellValue.indexOf("月");
if(yearNum>=0 && monthNum>=0){
String cutMonth = stringCellValue.substring(stringCellValue.indexOf("年")+1,stringCellValue.indexOf("月"));
if(cutMonth.length()==1){
if(!(cutYear.equals(year)&&month2.equals(cutMonth))){
information="表头日期录入有误";
return information;
}
}
if(cutMonth.length()==2){
if(!(cutYear.equals(year)&&cutMonth.equals(month))){
information="表头日期录入有误";
return information;
}
}
}else{
information="表头信息录入有误,请参照模板";
return information;
}
for (int i = 4; i < sheet.getPhysicalNumberOfRows(); i++) {
row = sheet.getRow(i);
CsRunInfo csRunInfo = new CsRunInfo();
//以下是非空判断
if(row.getCell(1)==null||row.getCell(1).equals("")){
information="车型不能为空";
return information;
}else if(row.getCell(2)==null||row.getCell(2).equals("")){
information="车号不能为空";
return information;
}else if(row.getCell(3)==null||row.getCell(3).equals("")){
information="配属段不能为空";
return information;
}else if(!(row.getCell(4).getCellType()==0)){
information="新造到段日期填写错误";
return information;
}else if(row.getCell(5)==null||row.getCell(5).equals("")){
information="修程不能为空";
return information;
}else if(!(row.getCell(7).getCellType()==0)){
information="入厂日期填写错误";
return information;
}else if(!(row.getCell(9).getCellType()==0)){
information="回段日期填写错误";
return information;
}else if(row.getCell(11)==null||row.getCell(11).equals("")){
information="当月走行不能为空";
return information;
}else if(row.getCell(12)==null||row.getCell(12).equals("")){
information="总走行公里不能为空";
return information;
}else{
SysDatabase s=new SysDatabase();
s.setName(row.getCell(1).getStringCellValue());
s.setTypeCode("CX");
if(null!=sysDatabaseMapper.getIdByNameAndTypeCode(s)){
csRunInfo.setCarType(sysDatabaseMapper.getIdByNameAndTypeCode(s));
}else{
information="车型错误";
return information;
}
//csRunInfo.setCarType(row.getCell(1).getStringCellValue());
String num = row.getCell(2).getNumericCellValue()+"";
String substring = num.substring(0, num.indexOf("."));
s.setName(substring);
s.setTypeCode("CH");
String cx=row.getCell(1).getStringCellValue();
if(sysDatabaseMapper.selectByParentANDName(cx,substring)==0){
information="车型车号不匹配";
return information;
}
if(row.getCell(2).getCellType()==1){
if(null!=sysDatabaseMapper.getIdByNameAndTypeCode(s)){
csRunInfo.setCarNumber(sysDatabaseMapper.getIdByParentANDName(cx,substring));
}else{
information="车号错误";
return information;
}
//csRunInfo.setCarNumber(row.getCell(2).getStringCellValue());
}else{
if(null!=sysDatabaseMapper.getIdByNameAndTypeCode(s)){
csRunInfo.setCarNumber(sysDatabaseMapper.getIdByParentANDName(cx,substring));
}else{
information="车号错误";
return information;
}
/*cell = String.valueOf(row.getCell(2).getNumericCellValue());
csRunInfo.setCarNumber(cell.substring(0, cell.indexOf(".")));*/
}
s.setName(row.getCell(3).getStringCellValue());
s.setTypeCode("JWD");
if(null!=sysDatabaseMapper.getIdByNameAndTypeCode(s)){
csRunInfo.setBelong(sysDatabaseMapper.getIdByNameAndTypeCode(s));
}else{
information="配属段错误";
return information;
}
//csRunInfo.setBelong(row.getCell(3).getStringCellValue());
if(!(row.getCell(4)==null||row.getCell(4).equals(""))){
csRunInfo.setArriveDate(row.getCell(4).getDateCellValue());
}
if(!(row.getCell(5)==null||row.getCell(5).equals(""))){
if(!(row.getCell(5).getStringCellValue().equalsIgnoreCase("C4")||row.getCell(5).getStringCellValue().equalsIgnoreCase("C5"))){
information="修程错误";
return information;
}else{
csRunInfo.setBureau(row.getCell(5).getStringCellValue().toUpperCase());
}
}
if(!(row.getCell(6)==null||row.getCell(6).equals(""))){
csRunInfo.setC4RepairUnit(row.getCell(6).getStringCellValue());
}
if(!(row.getCell(7)==null||row.getCell(7).equals(""))){
csRunInfo.setC4IncomeTime(row.getCell(7).getDateCellValue());
}
if(!(row.getCell(8)==null||row.getCell(8).equals(""))){
if(row.getCell(8).getCellType()==1){
csRunInfo.setC4PreRunInfo(row.getCell(8).getStringCellValue());
}else{
cell = String.valueOf(row.getCell(8).getNumericCellValue());
csRunInfo.setC4PreRunInfo(cell.substring(0, cell.indexOf(".")));
}
}
if(!(row.getCell(9)==null||row.getCell(9).equals(""))){
csRunInfo.setC4ReturnTime(row.getCell(9).getDateCellValue());
}
if(!(row.getCell(10)==null||row.getCell(10).equals(""))){
if(row.getCell(10).getCellType()==1){
csRunInfo.setC4AfterRunInfo(row.getCell(10).getStringCellValue());
}else{
cell = String.valueOf(row.getCell(10).getNumericCellValue());
csRunInfo.setC4AfterRunInfo(cell.substring(0, cell.indexOf(".")));
}
}
if(row.getCell(11).getCellType()==1){
csRunInfo.setMonthRun(row.getCell(11).getStringCellValue());
}else{
cell = String.valueOf(row.getCell(11).getNumericCellValue());
csRunInfo.setMonthRun(cell.substring(0, cell.indexOf(".")));
}
if(row.getCell(12).getCellType()==1){
csRunInfo.setTotalRun(row.getCell(12).getStringCellValue());
}else{
cell = String.valueOf(row.getCell(12).getNumericCellValue());
csRunInfo.setTotalRun(cell.substring(0, cell.indexOf(".")));
}
if(!(row.getCell(13) == null ||row.getCell(13).equals(""))){
if(row.getCell(13).getCellType()==1){
csRunInfo.setNote(row.getCell(13).getStringCellValue());
}else{
cell = String.valueOf(row.getCell(13).getNumericCellValue());
csRunInfo.setNote(cell.substring(0, cell.indexOf(".")));
}
}
csRunInfo.setFileName(fileName);
csRunInfo.setCreateDate(new Date());
list.add(csRunInfo);
}
}
batchDelete(fileName);
for (CsRunInfo csRunInfo : list) {
/*csRunInfoMapper.insertSelective(csRunInfo);*/
csRunInfoMapper.insertReasonBureau(csRunInfo);
}
return information;
}
<!-- 上传走行信息的Excel -->
<form id="runInfoForm" class="collection_form" action="${basePath}/cs/uploadRunInfo" enctype="multipart/form-data" target="hiddenIFrame" method="post">
<!-- 选择按钮 -->
<div class="file">
<input type="file" name="file" class="files" id="file"/>
</div>
<!-- 上传按钮 -->
<a href="javascript:void(0)" class="templateDownload" id="btn">上传</a>
<!-- 模板下载 -->
<a href="${basePath }/fileDisk/cs/服务器上文件名.xlsx" class="templateDownload">模板下载</a>
</form>
<iframe id='hiddenIFrame' name='hiddenIFrame' style="display:none;"></iframe>
/**
* controller走行xml上传
* @param file
* @param request
* @param model
* @return
* @throws Exception
*/
@RequestMapping(value="/uploadRunInfo",produces="text/html;charset=UTF-8")
@ResponseBody
public String upload(MultipartFile file,HttpServletRequest request,Model model) throws Exception{
if(null == file || (null != file && 0 == file.getSize())){
return "请选择一个文件!";
}
String fileName = file.getOriginalFilename();
//校验文件名
boolean flag = csRunInfoService.validateName(fileName);
if(false == flag){
return "文件名错误,请核对文件名!";
}
boolean validateDate = csRunInfoService.validateDate(fileName);
if(false == validateDate){
return "上传失败,只能上传当月走行记录!";
}
//校验文件是否已经存在
4000
String path = request.getSession().getServletContext()
.getRealPath("/fileDisk/cs");
List<String> list = csRunInfoService.fileNameList(path);
boolean hasFile = list.contains(fileName);
logger.debug("@@@@@ 服务器中是否已经有文件:" + hasFile);
File newFile = new File(path + "/" + fileName);
FileUtils.copyInputStreamToFile(file.getInputStream(), newFile);
if(!newFile.exists()){
return "上传失败!";
}else{
//解析走行记录xml
String newPath = path + "/" + fileName;
String information = csRunInfoService.parseRunInfoXml(newPath, fileName);
return information;
}
}
/**
*service 解析走行xml
*/
@Override
public String parseRunInfoXml(String newPath,String fileName) throws Exception {
/**
* 类型
CELL_TYPE_BLANK 空值(cell不为空)
CELL_TYPE_BOOLEAN 布尔类型
CELL_TYPE_FORMULA 公式类型
CELL_TYPE_STRING 字符串类型
CELL_TYPE_NUMERIC 数值类型
CELL_TYPE_ERROR 错误类型
*/
String information="上传成功";
FileInputStream is = new FileInputStream(new File(newPath));
XSSFWorkbook xwb = new XSSFWorkbook(is);
XSSFSheet sheet = xwb.getSheetAt(0);
XSSFRow row = null;
String cell;
List<CsRunInfo> list = new ArrayList<CsRunInfo>();
Date d = new Date();
Calendar c = Calendar.getInstance();
SimpleDateFormat yearFormat = new SimpleDateFormat("yyyy");
String year = yearFormat.format(c.getTime());
/*String year = yearFormat.format(d); */
SimpleDateFormat monthFormat = new SimpleDateFormat("MM");
c.add(Calendar.MONTH, -1);
String month = monthFormat.format(c.getTime());
/*String month = monthFormat.format(d);*/
String month2 = month.substring(1,2);
row = sheet.getRow(0);
String stringCellValue = row.getCell(0).getStringCellValue();
String cutYear = stringCellValue.substring(0,4);
int yearNum = stringCellValue.indexOf("年");
int monthNum = stringCellValue.indexOf("月");
if(yearNum>=0 && monthNum>=0){
String cutMonth = stringCellValue.substring(stringCellValue.indexOf("年")+1,stringCellValue.indexOf("月"));
if(cutMonth.length()==1){
if(!(cutYear.equals(year)&&month2.equals(cutMonth))){
information="表头日期录入有误";
return information;
}
}
if(cutMonth.length()==2){
if(!(cutYear.equals(year)&&cutMonth.equals(month))){
information="表头日期录入有误";
return information;
}
}
}else{
information="表头信息录入有误,请参照模板";
return information;
}
for (int i = 4; i < sheet.getPhysicalNumberOfRows(); i++) {
row = sheet.getRow(i);
CsRunInfo csRunInfo = new CsRunInfo();
//以下是非空判断
if(row.getCell(1)==null||row.getCell(1).equals("")){
information="车型不能为空";
return information;
}else if(row.getCell(2)==null||row.getCell(2).equals("")){
information="车号不能为空";
return information;
}else if(row.getCell(3)==null||row.getCell(3).equals("")){
information="配属段不能为空";
return information;
}else if(!(row.getCell(4).getCellType()==0)){
information="新造到段日期填写错误";
return information;
}else if(row.getCell(5)==null||row.getCell(5).equals("")){
information="修程不能为空";
return information;
}else if(!(row.getCell(7).getCellType()==0)){
information="入厂日期填写错误";
return information;
}else if(!(row.getCell(9).getCellType()==0)){
information="回段日期填写错误";
return information;
}else if(row.getCell(11)==null||row.getCell(11).equals("")){
information="当月走行不能为空";
return information;
}else if(row.getCell(12)==null||row.getCell(12).equals("")){
information="总走行公里不能为空";
return information;
}else{
SysDatabase s=new SysDatabase();
s.setName(row.getCell(1).getStringCellValue());
s.setTypeCode("CX");
if(null!=sysDatabaseMapper.getIdByNameAndTypeCode(s)){
csRunInfo.setCarType(sysDatabaseMapper.getIdByNameAndTypeCode(s));
}else{
information="车型错误";
return information;
}
//csRunInfo.setCarType(row.getCell(1).getStringCellValue());
String num = row.getCell(2).getNumericCellValue()+"";
String substring = num.substring(0, num.indexOf("."));
s.setName(substring);
s.setTypeCode("CH");
String cx=row.getCell(1).getStringCellValue();
if(sysDatabaseMapper.selectByParentANDName(cx,substring)==0){
information="车型车号不匹配";
return information;
}
if(row.getCell(2).getCellType()==1){
if(null!=sysDatabaseMapper.getIdByNameAndTypeCode(s)){
csRunInfo.setCarNumber(sysDatabaseMapper.getIdByParentANDName(cx,substring));
}else{
information="车号错误";
return information;
}
//csRunInfo.setCarNumber(row.getCell(2).getStringCellValue());
}else{
if(null!=sysDatabaseMapper.getIdByNameAndTypeCode(s)){
csRunInfo.setCarNumber(sysDatabaseMapper.getIdByParentANDName(cx,substring));
}else{
information="车号错误";
return information;
}
/*cell = String.valueOf(row.getCell(2).getNumericCellValue());
csRunInfo.setCarNumber(cell.substring(0, cell.indexOf(".")));*/
}
s.setName(row.getCell(3).getStringCellValue());
s.setTypeCode("JWD");
if(null!=sysDatabaseMapper.getIdByNameAndTypeCode(s)){
csRunInfo.setBelong(sysDatabaseMapper.getIdByNameAndTypeCode(s));
}else{
information="配属段错误";
return information;
}
//csRunInfo.setBelong(row.getCell(3).getStringCellValue());
if(!(row.getCell(4)==null||row.getCell(4).equals(""))){
csRunInfo.setArriveDate(row.getCell(4).getDateCellValue());
}
if(!(row.getCell(5)==null||row.getCell(5).equals(""))){
if(!(row.getCell(5).getStringCellValue().equalsIgnoreCase("C4")||row.getCell(5).getStringCellValue().equalsIgnoreCase("C5"))){
information="修程错误";
return information;
}else{
csRunInfo.setBureau(row.getCell(5).getStringCellValue().toUpperCase());
}
}
if(!(row.getCell(6)==null||row.getCell(6).equals(""))){
csRunInfo.setC4RepairUnit(row.getCell(6).getStringCellValue());
}
if(!(row.getCell(7)==null||row.getCell(7).equals(""))){
csRunInfo.setC4IncomeTime(row.getCell(7).getDateCellValue());
}
if(!(row.getCell(8)==null||row.getCell(8).equals(""))){
if(row.getCell(8).getCellType()==1){
csRunInfo.setC4PreRunInfo(row.getCell(8).getStringCellValue());
}else{
cell = String.valueOf(row.getCell(8).getNumericCellValue());
csRunInfo.setC4PreRunInfo(cell.substring(0, cell.indexOf(".")));
}
}
if(!(row.getCell(9)==null||row.getCell(9).equals(""))){
csRunInfo.setC4ReturnTime(row.getCell(9).getDateCellValue());
}
if(!(row.getCell(10)==null||row.getCell(10).equals(""))){
if(row.getCell(10).getCellType()==1){
csRunInfo.setC4AfterRunInfo(row.getCell(10).getStringCellValue());
}else{
cell = String.valueOf(row.getCell(10).getNumericCellValue());
csRunInfo.setC4AfterRunInfo(cell.substring(0, cell.indexOf(".")));
}
}
if(row.getCell(11).getCellType()==1){
csRunInfo.setMonthRun(row.getCell(11).getStringCellValue());
}else{
cell = String.valueOf(row.getCell(11).getNumericCellValue());
csRunInfo.setMonthRun(cell.substring(0, cell.indexOf(".")));
}
if(row.getCell(12).getCellType()==1){
csRunInfo.setTotalRun(row.getCell(12).getStringCellValue());
}else{
cell = String.valueOf(row.getCell(12).getNumericCellValue());
csRunInfo.setTotalRun(cell.substring(0, cell.indexOf(".")));
}
if(!(row.getCell(13) == null ||row.getCell(13).equals(""))){
if(row.getCell(13).getCellType()==1){
csRunInfo.setNote(row.getCell(13).getStringCellValue());
}else{
cell = String.valueOf(row.getCell(13).getNumericCellValue());
csRunInfo.setNote(cell.substring(0, cell.indexOf(".")));
}
}
csRunInfo.setFileName(fileName);
csRunInfo.setCreateDate(new Date());
list.add(csRunInfo);
}
}
batchDelete(fileName);
for (CsRunInfo csRunInfo : list) {
/*csRunInfoMapper.insertSelective(csRunInfo);*/
csRunInfoMapper.insertReasonBureau(csRunInfo);
}
return information;
}
相关文章推荐
- java poi 打开 excel 文件 保留格式 修改内容 另存为新文件
- java上传excel文件,并判断内容大小,文件格式
- Java SpringMVC POI上传excel并读取文件内容
- ASP.NET MVC4上传文件,并将excel内容导入数据库,导入后删除文件
- ASP.NET上传图片,服务器端验证,宽度,高度,文件类型,尺寸,文件大小,图片上传,图片格式检查
- 使用POI读取excel文件内容
- 使用POI读取excel文件内容
- DataGrid内容导出标准的Excel格式文件
- 使用POI读取excel文件内容
- Java+Flex+POI 解析Excel文件(读取文件中想要的内容)
- poi导入导出excel文件,兼容.xls和.xlsx两种格式
- 用Excel打开csv文件时,如何处理数字内容展现会自动转换格式的问题
- 将数据控件(如GridView)的内容转化成Excel格式文件
- java poi技术将Excel文件内容导入数据库
- Java入门开发POI读取导入Excel文件及验证
- POI使用SAX处理大量数据的xlsx格式的Excel文件
- Java poi excel 此文件中某些文本格式可能已经更改
- asp.net 文件上传验证是否是真正的图片格式
- EXCEL文件显示:您尝试打开的文件的格式与文件扩展名指定的格式不一致,打开前请验证文件示被损坏且来源可
- 修复打开Excel提示,“您尝试打开的文件的格式与文件扩展名指定的格式不一致,打开前请验证文件示被损坏且来源可信,是否立即打开该文件?”