private List<Map<String, Object>> check(Workbook workBook) {
List<Map<String, Object>> excelValueList = null;
// 待导入的数据
int import_wb_sheetNos = workBook.getNumberOfSheets();
if (import_wb_sheetNos == 0) {
throw new ServiceException("NumberOfSheets 数据为0,无数据可导入");
}
excelValueList = new ArrayList<>();
// 创建人工号
String EMP_CODE = UserUtil.getCurrentUser().getEmpCode();
// 查询网点信息
List<UmUserBranch> umUserBranchList = umUserBranchDao
.queryUmUserBranchByCorpCode(null, EMP_CODE);
if (umUserBranchList == null || umUserBranchList.size() == 0) {
throw new ServiceException("没有网点权限");
}
UmUserBranch umUserBranch = umUserBranchList.get(0);
for (int i = 0; i < import_wb_sheetNos; i++) {
// 获取sheet
Sheet import_sheet = workBook.getSheetAt(i);
for (int heigth = 1, count = import_sheet.getLastRowNum() + 1; heigth < count; heigth++) {
Row row = import_sheet.getRow(heigth);
int FACE_AMT = 0;
Cell FACE_AMT_CELL = row.getCell(0);
try {
FACE_AMT_CELL.setCellType(Cell.CELL_TYPE_STRING);
FACE_AMT = Integer.valueOf(FACE_AMT_CELL.getStringCellValue());
} catch (Exception e) {
String errorMessage = workBook.getSheetName(i) + "的第"
+ heigth + "行的面额不是数字";
throw new ServiceException(errorMessage);
}
if(FACE_AMT <= 0) {
String errorMessage = workBook.getSheetName(i) + "的第"
+ heigth + "行的面额小于等于0";
throw new ServiceException(errorMessage);
}
int BEN = 0;
Cell BEN_CELL = row.getCell(1);
try {
BEN_CELL.setCellType(Cell.CELL_TYPE_STRING);
BEN = Integer.valueOf(BEN_CELL.getStringCellValue());
} catch (Exception e) {
String errorMessage = workBook.getSheetName(i) + "的第"
+ heigth + "行的数量不是数字";
throw new ServiceException(errorMessage);
}
int SPEC = 0;
Cell SPEC_CELL = row.getCell(2);
try {
SPEC_CELL.setCellType(Cell.CELL_TYPE_STRING);
SPEC = Integer.valueOf(SPEC_CELL.getStringCellValue());
} catch (Exception e) {
String errorMessage = workBook.getSheetName(i) + "的第"
+ heigth + "行的规格不是数字";
throw new ServiceException(errorMessage);
}
if(!validateSpec(String.valueOf(SPEC))) {
String errorMessage = workBook.getSheetName(i) + "的第"
+ heigth + "行的规格不存在";
throw new ServiceException(errorMessage);
}
Cell APPLYER_ID_CELL = row.getCell(3);
String APPLYER_ID = null;
if(null == APPLYER_ID_CELL) {
String errorMessage = workBook.getSheetName(i) + "的第"
+ heigth + "行的申请人不能为空";
throw new ServiceException(errorMessage);
}else{
APPLYER_ID_CELL.setCellType(Cell.CELL_TYPE_STRING);
if (StringUtils.isBlank(APPLYER_ID = APPLYER_ID_CELL.getStringCellValue())) {
String errorMessage = workBook.getSheetName(i) + "的第"
+ heigth + "行的申请人不能为空";
throw new ServiceException(errorMessage);
}
}
//新增申请人姓名
Cell APPLYER_NAME_CELL = row.getCell(4);
String APPLYER_NAME = null;
if(null == APPLYER_NAME_CELL) {
String errorMessage = workBook.getSheetName(i) + "的第"
+ heigth + "行的申请人姓名不能为空";
throw new ServiceException(errorMessage);
}else{
APPLYER_NAME_CELL.setCellType(Cell.CELL_TYPE_STRING);
if (StringUtils.isBlank(APPLYER_NAME = row.getCell(4).getStringCellValue())) {
String errorMessage = workBook.getSheetName(i) + "的第"
+ heigth + "行的申请人姓名不能为空";
throw new ServiceException(errorMessage);
}
}
Map<String, Object> value = new HashMap<>();
value.put("FACE_AMT", FACE_AMT);
value.put("NEED_BEN", BEN);
value.put("SPEC", SPEC);
value.put("APPLYER_ID", APPLYER_ID);
value.put("APPLYER_NAME", APPLYER_NAME);
value.put("createId", EMP_CODE);
value.put("BUKRS", umUserBranch.getCorpCode());
value.put("TOTAL_AMT", FACE_AMT * BEN * SPEC);
value.put("CREATE_ID", EMP_CODE);
value.put("BRANCH_CODE", umUserBranch.getBranchCode());
value.put("BRANCH_NAME", umUserBranch.getBranchName());
value.put("ZDISNUM", umUserBranch.getZDISNUM());
// 待汇总状态
value.put("EXTEND_STATUS",
InvoiceDtlApplyExtendStatusEnum.WAIT_COLLECT.getValue());
// 收派员待网点分配状态
value.put("STATUS",
InvoiceApplyStatusEnum.PEOPLE_WAIT_BRANCH_ALLOT
.getValue());
value.put("TYPE", InvoiceInputTypeEnum.FIXED_INVOICE.getValue());
excelValueList.add(value);
}
}
return excelValueList;
}
package com.sf.novatar.tpl.util;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.POIXMLDocument;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.struts2.dispatcher.multipart.MultiPartRequestWrapper;
import com.sf.novatar.tpl.enums.FileType;
import com.sf.novatar.tpl.servlet.HttpServletHolder;
import com.sfpay.framework.base.exception.ServiceException;
/**
* 描述:
*/
public abstract class AbstractExcelReader {
public abstract void doParse(Workbook workbook);
public void Reader() throws Exception {
Reader((InputStream inputStream) -> {
String fileType = getType(inputStream);
if (StringUtils.isBlank(fileType)
|| (!FileType.XLSX.getValue().equals(fileType)
&& !FileType.XLS_DOC.getValue().equals(fileType) && !FileType.XLSX_DOCX
.getValue().equals(fileType))) {
throw new ServiceException("上传失败,请选择Excel类型文件! ");
}
});
}
public void Reader(Validate validate) throws Exception {
MultiPartRequestWrapper mpRequest = (MultiPartRequestWrapper) HttpServletHolder
.getCurrentRequest();
// 导入的文件
File files = mpRequest.getFiles("excel")[0];
if (null == files) {
throw new FileNotFoundException("解析的文件不存在");
}
try (InputStream fileInputStream = new FileInputStream(files);) {
validate.doValidate(fileInputStream);
if (!fileInputStream.markSupported()) {
try (BufferedInputStream bufferedInputStream = new BufferedInputStream(
fileInputStream);) {
Workbook workbook = null;
if (POIFSFileSystem.hasPOIFSHeader(bufferedInputStream)) {
workbook = new HSSFWorkbook(bufferedInputStream);
} else if (POIXMLDocument
.hasOOXMLHeader(bufferedInputStream)) {
workbook = new XSSFWorkbook(
OPCPackage.open(bufferedInputStream));
} else {
throw new ServiceException("没有获得相应的workbook");
}
doParse(workbook);
}
}
}
}
/**
* * 判断文件类型
*
* @param inputStream
*
* @return 文件类型
*
*/
private static String getType(InputStream inputStream) throws IOException {
if (inputStream == null) {
return null;
}
String fileHead = getFileContent(inputStream);
if (fileHead == null || fileHead.length() == 0) {
return null;
}
fileHead = fileHead.toUpperCase();
FileType[] fileTypes = FileType.values();
for (FileType type : fileTypes) {
if (fileHead.startsWith(type.getValue())) {
return type.getValue();
}
}
throw new ServiceException("没有获取到响应的文件类型");
}
/**
* 得到文件头
*
* @param filePath
* 文件路径
* @return 文件头
* @throws IOException
*/
private static String getFileContent(InputStream inputStream)
throws IOException {
int length = 28;
byte[] b = null;
if (inputStream != null) {
inputStream.read(b = new byte[length], 0, length);
inputStream.skip(-length);
}
return FileTypeJudge.bytesToHexString(b);
}
public interface Validate {
public void doValidate(InputStream inputStream) throws Exception;
}
}
分享到:
相关推荐
Excel上传解析组件,可以直接拿来调用,很有技术含量,值得学习
本资源对应我的一片文章《基于Java语言SpringMVC框架的Excel上传解析以及下载教程》提供的jar依据,其中内容仅供参考,建议阅读那篇文章
上传本地文件到服务器进行解析,通过poi继续操作数据,易懂简单。
Excel上传并解析java对象Excel上传并解析java对象Excel上传并解析java对象
3 设置header和解析完成后上传方法: importExcel ref="import_excel" :import-methods="handleYcExcelImport" :max="100" :fields="fields" 注: fields : 解析数组的key import-methods:点击上传按钮时执行的...
Java上传下载excel、解析Excel、生成Excel的问题.pdf
Excel文件上传,获取Excel文件并解析
best20182022-10-17 14:55:20原证springboot整合easyexcel实现excel文件上传解析,下载导出简单实例 项目描述 springboot整合easyexcel,上传解析Excel,下载导出Excel 运行环境 jdk8+tomcat9+IntelliJ IDEA+...
java版excel解析,并上传至mysql数据库。已包含jar包,数据库sql语句等,eclipse直接运行client.java
NULL 博文链接:https://yuhao224.iteye.com/blog/930580
《JavaWeb---简单应用---练习JDBC,JSTL》---上传Excel文件,利用工具包解析,将数据储存到数据库中,可以查询删除相关源码
主要介绍了springboot实现上传并解析Excel过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
jxl JAVA Excel解析,主要用于修改、上传、下载Excel文件。
js实现excel文件上传解析,返回json并显示到页面 支持两种格式
前端js解析/读取excel文件,完整解析,简单操作,官网地址 http://oss.sheetjs.com/js-xlsx/
java 解析Excel 并判断解析的数据类型__Eclipse工程,纯手工,无复制
jsp页面可以批量上传多个excel文件,后台解析入库。并将信息展现在jsp页面表格里,勾选多个数据,可以导出excel文件。很方便,很实用!(每一行数据,就相当于表格里的某张表的一条信息,后期只要改下表结构,数据库...
使用struts和POI进行简单的Excel上传和解析,最后把Excel中的数据转化成类Person的实体,这是一个简单的案例,没有hibernate操作,相信只要知道怎么把Excel转换成实体并添加到List中,后面的都不是问题,本案例可以...
解析excel文件.zip