使用apache的poi包可以对excel进行操作读取和写入。

因excel分为xls的2003版和xlsx的2007版,poi在创建workbook时使用不同的类创建,因此需要注意区分xls。

Workbook workbook = null;
String fileExtension=FilenameUtils.getExtension(file.getOriginalFilename());
if(".xls".equals(fileExtension)){
workbook = new HSSFWorkbook(file.getInputStream()); //2003 xls
}else{
workbook = new XSSFWorkbook(file.getInputStream()); //2007 xlsx
}

※注意如果引入poi后找不到XSSFWorkbook,则可能没有引入poi-ooxml.jar

Sheet sheetWorkInfo = workbook.getSheet([sheetname]);

以下为读取excel内容装入到list<bean>中的实例:

/**
* ExcelUtils 读取信息
* @author DennyZhao
*
*/
public class ExcelUtils { /**
* 获取workbook
* @param file
* @return workbook
* @throws IOException
* @throws FileNotFoundException
*/
public static Workbook getWorkBook(String filepath) throws FileNotFoundException, IOException {
Workbook workbook = null;
File file = new File(filepath);
/**
* 文件是否存在,是否为可用文件
*/
if(!file.exists() || file.isDirectory()) {
System.out.println("file is not exists or is a directory...");
return null;
}
/**
* 文件是否可读
*/
if(!file.canRead()) {
System.out.println("the file can not be readed, please confirm if you have the authority to read it.");
return null;
}
/**
* 是否为excel文件
*/
if(!FilenameUtils.getExtension(filepath).contains("xls")) {
System.out.println("i'm so sorry..we just support the file of type which is excel..");
return null;
} String fileExtension=FilenameUtils.getExtension(file.getName());
if("xls".equals(fileExtension)){
workbook = new HSSFWorkbook(new FileInputStream(file)); //2003 xls
}else{
workbook = new XSSFWorkbook(new FileInputStream(file)); //2007 xlsx
}
return workbook;
} /**
* 读取数据返回对象一览
* @param filepath
* @param sheetName
* @return
* @throws IOException
* @throws FileNotFoundException
* @throws InvocationTargetException
* @throws IllegalAccessException
* @throws InstantiationException
*/
public static <T> List<T> getListObj(String filepath, String sheetName, Class<T> class1) throws FileNotFoundException, IOException, IllegalAccessException, InvocationTargetException, InstantiationException{
Workbook work = getWorkBook(filepath);
Sheet sheet = work.getSheet(sheetName);
int rowCount = sheet.getPhysicalNumberOfRows();
int colCount = sheet.getRow(0).getPhysicalNumberOfCells();
List<T> listResult = new ArrayList<T>(); for(int i=1;i < rowCount; i++) {
Row row = sheet.getRow(i);
Map<String, Object> map = new HashMap<String, Object>();
// 判断是否已经读取完毕,第一格不能为空
if(row.getCell(0)== null) {
break;
} for(int j=0; j < colCount; j++) {
Cell cell = row.getCell(j);
String key = sheet.getRow(0).getCell(j).getStringCellValue(); Object cellValue = getCellValue(cell);
map.put(key, cellValue);
}
T t = class1.newInstance();
BeanUtils.copyProperties(t, map);
listResult.add(t);
}
work.close();
return listResult;
} /**
* 获取cellValue
* @param cell
*/
private static Object getCellValue(Cell cell) {
if(cell == null) {
return "";
}
CellType cellType = cell.getCellTypeEnum();
Object obj = null;
switch(cellType) {
case NUMERIC:
obj = cell.getNumericCellValue();
break;
default:
obj = cell.getStringCellValue();
}
return obj;
}

poi excel的更多相关文章

  1. poi 升级至4.x 的问题总结(POI Excel 单元格内容类型判断并取值)

    POI Excel 单元格内容类型判断并取值 以前用 cell.getCachedFormulaResultType() 得到 type 升级到4后获取不到了 换为:cell.getCellType( ...

  2. POI Excel 合并数据相同的行

    import java.io.Serializable; /** * POI Excel报表导出,列合并实体<br> * * @author WQ * */ public class Po ...

  3. poi excel导出,下载

    poi.jar包 public void downExcel(HttpServletResponse response,Page<ShopApply> page) throws Excep ...

  4. poi excel导入

    poi.jar包 import java.io.File;import java.io.FileInputStream;import java.io.IOException; import org.a ...

  5. java, poi, excel

    工作需要用java操作Excel,现在网上搜索了一下,决定选取POI包来操作.pom内容如下: <dependency> <groupId>org.apache.poi< ...

  6. POI/Excel/HTML单元格公式问题

    一.问题描述 使用MyBatis从数据库中获取数据,然后用POI把数据填充到Excel模板中,生成最终的xls文件.把最终的xls文件转换为html文件,并返回给前台显示在Panel中. Excel模 ...

  7. POI excel导出

    ******************************* excel表格导出,使用POI实现 ******************************* 实现导出步骤 --配置导出excel ...

  8. poi excel超出65536行数限制自动扩展Invalid row number (65536) outside allow

    1.xls一个sheet只能装65536行,多余则报错 poi包导出或写入excel超出65536报错: java.lang.IllegalArgumentException: Invalid row ...

  9. poi excel 合并单元格

    结论:final CellRangeAddress cra = new CellRangeAddress(rowId, rowId + rowSkip,        colId, colId + c ...

  10. poi excel 设置边框字体行高行宽

     final HSSFSheet sheet = wb.createSheet(sheetName + "_" + n);   System.out.println("s ...

随机推荐

  1. Flume 安装和配置

    安装步骤 1.安装jdk,1.6版本以上 2.上传flume的安装包 3.解压安装 4.在conf目录下,创建一个配置文件,比如:template.conf(名字可以不固定,后缀也可以不固定) 5.配 ...

  2. [转]SQL数据库查询到的汉字字段是乱码

    使用英文版SQL数据库查询到的汉字字段是乱码的解决方案 2007-12-04 14:55:45 标签:函数 SQL 数据库 乱码 排序规则 原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出 ...

  3. Centos7.2/7.3集群安装Kubernetes 1.8.4 + Dashboard(转)

    原文https://www.cnblogs.com/burningTheStar/p/7865998.html 1.环境配置 结点数量:3 结点系统:CentOS 7.2 / 7.3 2.效果展示 3 ...

  4. 在docker宿主机上查找指定容器内运行的所有进程的PID

    转载 https://www.cnblogs.com/keithtt/p/7591097.html 找到指定容器的所有进程的PID可以更方便的对容器进程进行管理,特别是在某些容器卡住无法连接的场景. ...

  5. 初识MapReduce

    MapReduce是Google的一项重要技术,它首先是一个编程模型,用以进行大数据量的计算.对于大数据量的计算,通常采用的处理手法就是并行计算.但对许多开发者来说,自己完完全全实现一个并行计算程序难 ...

  6. docker镜像创建redis5.0.3容器集群

    拉取redis5.0.3镜像 # docker pull daocloud.io/library/redis:5.0.3 [root@localhost ~]# docker pull daoclou ...

  7. windows10如何查看wifi密码

    1.首先,在你的电脑的右下角的WiFi的图标,右击它,选择"网络和internet设置"或者选择打开设置 :如下图 点击"更改适配器选项" 选择 WLAN选项, ...

  8. 把SAS批提交添加到鼠标右键

    下载注册表管理工具:RegSeeker Portable v2.57 中文绿色便携版 在RegSeeker中搜索:batch

  9. OpenStack Nova虚拟机创建流程解析

    https://yikun.github.io/2017/09/27/OpenStack-Nova%E8%99%9A%E6%8B%9F%E6%9C%BA%E5%88%9B%E5%BB%BA%E6%B5 ...

  10. redis参数改进建议

    1.修改stop-writes-on-bgsave-error为no当前配置为yes,分别修改redis.conf和当前实例#redis.confstop-writes-on-bgsave-error ...