java实现excel导入excel包含数据与图片并将导入过程中不符合要求的数据封装到excel
最近在开发中需要一个excel上传的功能,其中,excel就包含了数据和图片,经过查询与了解,代码实现如下,图片与数据需要单独获取。
始兴网站制作公司哪家好,找创新互联公司!从网页设计、网站建设、微信开发、APP开发、成都响应式网站建设等网站项目制作,到程序开发,运营维护。创新互联公司2013年开创至今到现在10年的时间,我们拥有了丰富的建站经验和运维经验,来保证我们的工作的顺利进行。专注于网站建设就选创新互联公司。
1.利用下面两个方法获取图片
/**
获取图片和位置 (xls)
- @param sheet
- @return
@throws IOException
*/
public MapgetPictures1(HSSFSheet sheet) throws IOException {
Mapmap = new HashMap ();
Listlist = sheet.getDrawingPatriarch().getChildren();
for (HSSFShape shape : list) {
if (shape instanceof HSSFPicture) {
HSSFPicture picture = (HSSFPicture) shape;
HSSFClientAnchor cAnchor = (HSSFClientAnchor) picture.getAnchor();
PictureData pdata = picture.getPictureData();
String key = cAnchor.getRow1() + "-" + cAnchor.getCol1(); // 行号-列号
map.put(key, pdata);
logger.info("图片类型" + picture.getPictureData().suggestFileExtension());
}
}
return map;
}
xls格式的获取图片,如果excel中不存在图片的话会报空指针异常,需判断excel中有无图片再调用此方法,Listpictures = (List ) wookbook.getAllPictures();通过判断list是否为空,判断excel中有无图片 /**
- 获取图片和位置 (xlsx)
- @param sheet
- @return
- @throws IOException
*/
public MapgetPictures2(XSSFSheet sheet) throws IOException {
Mapmap = new HashMap ();
Listlist = sheet.getRelations();
for (POIXMLDocumentPart part : list) {
if (part instanceof XSSFDrawing) {
XSSFDrawing drawing = (XSSFDrawing) part;
Listshapes = drawing.getShapes();
for (XSSFShape shape : shapes) {
XSSFPicture picture = (XSSFPicture) shape;
XSSFClientAnchor anchor = picture.getPreferredSize();
CTMarker marker = anchor.getFrom();
String key = marker.getRow() + "-" + marker.getCol();
map.put(key, picture.getPictureData());
logger.info("图片类型" + picture.getPictureData().suggestFileExtension());
}
}
}
return map;
}
2,获取数据和图片返回
/**
* 获取excel数据 包括图片
*
* @param file
* @return
* @throws IOException
*/
public Map getDataFromExcel(MultipartFile file) throws IOException {
//文件名
String fileName = null;
//文件类型
String fileType = null;
//文件输入流
InputStream fis = null;
Workbook wookbook = null;
Sheet sheet = null;
Map result = null;
//图片map
List
上面的方法就已经将图片和数据分开获取到了,分别为Map类型maplist的图片数据 和 List类型的非图片数据
下面的方法实现获取数据的处理已经数据图片验证并将不合要求的数据封装起来。
@Override
@Transactional
public HashMap excelCreateLeaseBroker(MultipartFile file, UserVo userVo) {
Map data = null;
List> brokerList = null;
Map>> picMaps = null;
Map sheetList = null;
List leasingBrokerStaffs = null;
HashMap picResult = null;
try {
//获取导入的数据
data = importExcelPictureUtil.getDataFromExcel(file);
if (data == null) {
return CommonUtil.ToResultHashMap(MessageConstant.ERROR_CODE, "导入数据为空", null);
}
//获取非图片数据
brokerList = (List) data.get("list");
if (brokerList == null || brokerList.size() <= 0) {
return CommonUtil.ToResultHashMap(MessageConstant.ERROR_CODE, "导入数据为空", null);
}
logger.info("brokerListSize:" + brokerList.size());
//获取图片数据
sheetList = (Map) data.get("piclist");
if (sheetList == null) {
return CommonUtil.ToResultHashMap(MessageConstant.ERROR_CODE, "个人照片为空/不合规", null);
}
logger.info("picList:" + sheetList);
leasingBrokerStaffs = new ArrayList<>();
//遍历每一行数据
for (int i = 0; i < brokerList.size(); i++) {
//封装每一行数据
LeasingBrokerStaffVo leasingBrokerStaff = this.addLeasingBrokerStaff(brokerList.get(i), sheetList, i);
//验证每一行数据
HashMap result = this.vaildExcelParam(leasingBrokerStaff);
logger.info(result.toString());
//封装问题数据
if ("0".equals(String.valueOf(result.get("status")))) {
leasingBrokerStaff.setMessage(String.valueOf(result.get("message")));
leasingBrokerStaffs.add(leasingBrokerStaff);
}
//执行添加没问题数据
else {
}
}
String path = "";
if (leasingBrokerStaffs != null && leasingBrokerStaffs.size() > 0) {
path = this.importExcel(leasingBrokerStaffs);
}
} catch (Exception e) {
e.printStackTrace();
logger.info("批量导入异常" + e.getMessage());
TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
return CommonUtil.ToResultHashMap(MessageConstant.ERROR_CODE, "批量导入失败", null);
}
return CommonUtil.ToResultHashMap(MessageConstant.SUCCESS_CODE, "成功" + (brokerList.size() - leasingBrokerStaffs.size()) + "条,失败" + leasingBrokerStaffs.size() + "条", null);
}
/**
* 封装excel数据
*
* @param brokerList
* @return
*/
public LeasingBrokerStaffVo addLeasingBrokerStaff(List brokerList, Map sheetList, int count) throws IOException {
LeasingBrokerStaffVo leasingBrokerStaff = new LeasingBrokerStaffVo();
for (int i = 0; i < brokerList.size(); i++) {
//获取每一行数据
leasingBrokerStaff.setPeopleName(String.valueOf(brokerList.get(0)));
if ("男".equals(String.valueOf(brokerList.get(1)))) {
leasingBrokerStaff.setSex(1);
} else if ("女".equals(String.valueOf(brokerList.get(1)))) {
leasingBrokerStaff.setSex(2);
} else {
leasingBrokerStaff.setSex(0);
}
leasingBrokerStaff.setIdNo(String.valueOf(brokerList.get(2)).toUpperCase());
leasingBrokerStaff.setDegree(String.valueOf(brokerList.get(3)));
leasingBrokerStaff.setMobile(String.valueOf(brokerList.get(4)));
leasingBrokerStaff.setWorkOn(String.valueOf(brokerList.get(5)) == null ? null : DateUtil.convert2Date(String.valueOf(brokerList.get(5)), "yyyy-MM-dd"));
leasingBrokerStaff.setWorkStatus(1);
leasingBrokerStaff.setStatus(1);
Object key[] = sheetList.keySet().toArray();
//封装图片 这里是 6 7 8 列的为图片其中file idNoPicP idNoPicC为PictureData类型
for (int j = 0; j < sheetList.size(); j++) {
String picName = key[j].toString();
String[] keyName = picName.split("-");
logger.info("行数:" + (j + 1) + "图片行数:" + keyName[0]);
if (String.valueOf(count + 1).equals(keyName[0])) {
if ("6".equals(keyName[1])) {
leasingBrokerStaff.setFile(sheetList.get(key[j]));
} else if ("7".equals(keyName[1])) {
leasingBrokerStaff.setIdNoPicP(sheetList.get(key[j]));
} else if ("8".equals(keyName[1])) {
leasingBrokerStaff.setIdNoPicC(sheetList.get(key[j]));
}
}
}
}
return leasingBrokerStaff;
}
/**
* 批量导入失败数据上传
*
* @param leasingBrokerStaffs
* @return
* @throws Exception
*/
public String importExcel(List leasingBrokerStaffs) throws Exception {
String exportDataPath = "";
String[] rowsName = {"姓名", "性别", "×××号", "学历", "手机号", "从业日期", "个人照片", "×××正面", "×××反面", " "};
List dataList = new ArrayList();
Object[] objs = null;
OutputStream out = null;
for (int i = 0; i < leasingBrokerStaffs.size(); i++) {
objs = new Object[rowsName.length];
objs[0] = leasingBrokerStaffs.get(i).getPeopleName();
objs[1] = leasingBrokerStaffs.get(i).getSex() == 1 ? "男" : "女";
objs[2] = leasingBrokerStaffs.get(i).getIdNo();
objs[3] = leasingBrokerStaffs.get(i).getDegree();
objs[4] = leasingBrokerStaffs.get(i).getMobile();
objs[5] = DateUtil.convert2String(leasingBrokerStaffs.get(i).getWorkOn(), "yyyy-MM-dd");
objs[6] = leasingBrokerStaffs.get(i).getFile();
objs[7] = leasingBrokerStaffs.get(i).getIdNoPicP();
objs[8] = leasingBrokerStaffs.get(i).getIdNoPicC();
objs[9] = leasingBrokerStaffs.get(i).getMessage();
dataList.add(objs);
}
exportDataPath = exportExcelUtil.export6(out, "操作记录" + DateUtil.convert2String(new Date(), "yyyyMMddHHmmss"), rowsName, dataList);
return exportDataPath;
}
public String export6(OutputStream out, String title, String[] rowName, List dataList) throws Exception {
String exportDataPath = "";
File file = null;
try {
int rowaccess = 1000;//内存中缓存记录行数
/*keep 100 rowsin memory,exceeding rows will be flushed to disk*/
SXSSFWorkbook workbook = new SXSSFWorkbook(rowaccess);
workbook.setCompressTempFiles(true);
// XSSFWorkbook workbook = new XSSFWorkbook(); // 创建工作簿对象
SXSSFSheet sheet = (SXSSFSheet) workbook.createSheet(title); // 创建工作表
// 产生表格标题行
// HSSFRow rowm = sheet.createRow(0);
// HSSFCell cellTiltle = rowm.createCell(0);
// sheet样式定义【getColumnTopStyle()/getStyle()均为自定义方法 - 在下面 - 可扩展】
CellStyle columnTopStyle = this.getColumnTopStyle(workbook);// 获取列头样式对象
CellStyle style = this.getStyle(workbook); // 单元格样式对象
/*
* sheet.addMergedRegion(new
* CellRangeAddress(0,dataList.get(0).length-1 , 0,
* (rowName.length-1)));
*/// 合并单元格
// sheet.addMergedRegion(new CellRangeAddress(0, 1, 0,
// dataList.get(0).length - 1));// 列行
// cellTiltle.setCellStyle(style);
// cellTiltle.setCellValue(title);
// 定义所需列数
int columnNum = rowName.length;
SXSSFRow rowRowName = (SXSSFRow) sheet.createRow(0); // 在索引2的位置创建行(最顶端的行开始的第二行)
// 将列头设置到sheet的单元格中
for (int n = 0; n < columnNum; n++) {
SXSSFCell cellRowName = (SXSSFCell) rowRowName.createCell(n); // 创建列头对应个数的单元格
cellRowName.setCellType(XSSFCell.CELL_TYPE_STRING); // 设置列头单元格的数据类型
XSSFRichTextString text = new XSSFRichTextString(rowName[n]);
cellRowName.setCellValue(text); // 设置列头单元格的值
cellRowName.setCellStyle(columnTopStyle); // 设置列头单元格样式
}
// 将查询出的数据设置到sheet对应的单元格中
for (int i = 0; i < dataList.size(); i++) {
Object[] obj = dataList.get(i);// 遍历每个对象
SXSSFRow row = (SXSSFRow) sheet.createRow(i + 1);// 创建所需的行数(从第三行开始写数据)
row.setHeight((short)( 5 * 256));
for (int j = 0; j
当前标题:java实现excel导入excel包含数据与图片并将导入过程中不符合要求的数据封装到excel
新闻来源:http://pwwzsj.com/article/gspoge.html