工具类

package com.dp.util;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
/**
* Excel通用处理器
* @author jkxydp
*
*/
public class AllPurposeExcelProcesser {
/**
* 通过调用POI读Excel的较为通用的方式,其中约定:excel的第一行中每列的内容为字符串,并且与你定义的model中的字段相对应,你的类遵循JavaBean标准
* @param <T> 你希望读取后组装的数据结构定义
* @param excel 你要读取的文件
* @param modelType 你希望组装的数据的定义的字节码,该字节码描述中必须包含一个无参构造器
* @param sheetName 你要读取的文件中的工作表的名称
* @return 一个装载了读取后获得对象的List
* @throws FileNotFoundException
* @throws IOException
* @throws IllegalArgumentException
* @throws SecurityException
* @throws InstantiationException
* @throws IllegalAccessException
* @throws InvocationTargetException
* @throws NoSuchMethodException
*/
public static <T> List<T> read(File excel,Class<T> modelType,String sheetName) throws FileNotFoundException, IOException, IllegalArgumentException, SecurityException, InstantiationException, IllegalAccessException, InvocationTargetException, NoSuchMethodException{
Method[] methods = modelType.getMethods();
Workbook wb = getWorkBook(excel);
if(wb == null)
return null;
List<T> models = new ArrayList<T>(); //创建容器,用于装置所有读取出来的model
Sheet sheet = wb.getSheet(sheetName); //根据提供的sheet名称拿到整张表
int rows = sheet.getPhysicalNumberOfRows();
Row zeroRow = sheet.getRow(0); //拿到第零行表格,解析列与字段之间的对应关系
Map<Integer,Method> colMark = new HashMap<Integer, Method>(); //存储方法与字段的对应关系
for(short i = 0; i < zeroRow.getLastCellNum(); i ++){
Cell tip = zeroRow.getCell(i);
if(null != tip && tip.getCellType() == Cell.CELL_TYPE_STRING) {
String curTipName = tip.getStringCellValue();
for(int j = 0; j < methods.length; j ++) {
if(("set" + curTipName.toUpperCase().charAt(0) + curTipName.substring(1)).equals(methods[j].getName())) {
colMark.put((int)i, methods[j]);
}
}
}
}
Cell curCell = null;
boolean flag = true;
for(int i = 1; i < rows; i ++) {
Row aModel = sheet.getRow(i);
if(null != aModel) {
T model = modelType.getConstructor().newInstance();
for(int col = 0; col < colMark.size(); col ++) {
curCell = aModel.getCell(col);
if(null != curCell){
switch (curCell.getCellType()) {
case Cell.CELL_TYPE_STRING:
case Cell.CELL_TYPE_FORMULA:
case Cell.CELL_TYPE_BLANK:
if(colMark.get(col).getParameterTypes()[0] == String.class) {
colMark.get(col).invoke(model, curCell.getStringCellValue());
} else {
flag = false;
}
break;
case Cell.CELL_TYPE_NUMERIC:
if(colMark.get(col).getParameterTypes()[0] == int.class || colMark.get(col).getParameterTypes()[0] == Integer.class) {
colMark.get(col).invoke(model, (int)Math.round(curCell.getNumericCellValue()));
} else if (colMark.get(col).getParameterTypes()[0] == Double.class || colMark.get(col).getParameterTypes()[0] == double.class) {
colMark.get(col).invoke(model, curCell.getNumericCellValue());
} else if(colMark.get(col).getParameterTypes()[0] == Float.class || colMark.get(col).getParameterTypes()[0] == float.class) {
colMark.get(col).invoke(model, (float)curCell.getNumericCellValue());
} else if(colMark.get(col).getParameterTypes()[0] == Long.class || colMark.get(col).getParameterTypes()[0] == long.class) {
colMark.get(col).invoke(model, Math.round(curCell.getNumericCellValue()));
} else if(colMark.get(col).getParameterTypes()[0] == Short.class || colMark.get(col).getParameterTypes()[0] == short.class) {
colMark.get(col).invoke(model, (short)Math.round(curCell.getNumericCellValue()));
} else if(colMark.get(col).getParameterTypes()[0] == Byte.class || colMark.get(col).getParameterTypes()[0] == byte.class) {
colMark.get(col).invoke(model, (byte)Math.round(curCell.getNumericCellValue()));
} else if(colMark.get(col).getParameterTypes()[0] == Date.class) {
colMark.get(col).invoke(model, curCell.getDateCellValue());
}else {
flag = false;
}
break;
case Cell.CELL_TYPE_BOOLEAN:
if(colMark.get(col).getParameterTypes()[0] == Boolean.class || colMark.get(col).getParameterTypes()[0] == boolean.class) {
colMark.get(col).invoke(model, curCell.getBooleanCellValue());
} else {
flag = false;
}
break;
default:
break;
}
if(!flag) break;
}
}
if(flag) models.add(model);
flag = true;
}
}
return models;
}
/**
* Excel处理2003与2007差异
*/
private static Workbook getWorkBook(File excel) throws FileNotFoundException, IOException {
return excel.getName().endsWith("xls") ?
new HSSFWorkbook(new BufferedInputStream(new FileInputStream(excel))) :
excel.getName().endsWith("xlsx") ?
new XSSFWorkbook(new BufferedInputStream(new FileInputStream(excel))):null;
}
}

这是今天上班的时候工作做完了,无聊时写的一个相对通用的基于POI3.6的读取Excel的类,今后还会把它完善。
今后无聊就写点工具类,试着实现一个C/S模式分布式的MVC框架,希望我的理想不是梦!
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
目标检测(Object Detection)是计算机视觉领域的一个核心问题,其主要任务是找出图像中所有感兴趣的目标(物体),并确定它们的类别和位置。以下是对目标检测的详细阐述: 一、基本概念 目标检测的任务是解决“在哪里?是什么?”的问题,即定位出图像中目标的位置并识别出目标的类别。由于各类物体具有不同的外观、形状和姿态,加上成像时光照、遮挡等因素的干扰,目标检测一直是计算机视觉领域最具挑战性的任务之一。 二、核心问题 目标检测涉及以下几个核心问题: 分类问题:判断图像中的目标属于哪个类别。 定位问题:确定目标在图像中的具体位置。 大小问题:目标可能具有不同的大小。 形状问题:目标可能具有不同的形状。 三、算法分类 基于深度学习的目标检测算法主要分为两大类: Two-stage算法:先进行区域生成(Region Proposal),生成有可能包含待检物体的预选框(Region Proposal),再通过卷积神经网络进行样本分类。常见的Two-stage算法包括R-CNN、Fast R-CNN、Faster R-CNN等。 One-stage算法:不用生成区域提议,直接在网络中提取特征来预测物体分类和位置。常见的One-stage算法包括YOLO系列(YOLOv1、YOLOv2、YOLOv3、YOLOv4、YOLOv5等)、SSD和RetinaNet等。 四、算法原理 以YOLO系列为例,YOLO将目标检测视为回归问题,将输入图像一次性划分为多个区域,直接在输出层预测边界框和类别概率。YOLO采用卷积网络来提取特征,使用全连接层来得到预测值。其网络结构通常包含多个卷积层和全连接层,通过卷积层提取图像特征,通过全连接层输出预测结果。 五、应用领域 目标检测技术已经广泛应用于各个领域,为人们的生活带来了极大的便利。以下是一些主要的应用领域: 安全监控:在商场、银行
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值