使用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. centos7 firewall-cmd 用活firewalld防火墙中的zone

    原文:http://www.excelib.com/article/290/show/ firewalld中zone的含义学生前面已经给大家介绍过了,说白了一个zone就是一套规则集.可是什么时候该用 ...

  2. ARM Cortex-A9 (tiny 4412)

    要求 移植linux增加系统调用并烧写至开发板 详细步骤 一.搭建linux编译环境 1.GCC 编译器的安装: tar xzvf arm-linux-gcc-4.5.1-v6-vfp-2012030 ...

  3. [UE4]GameMode和GameInstance

    1.GameMode与场景的生命周期是相同的.使用OpenLevel切换到另外一个场景,第一个场景的GameMode就会被销毁,然后场景第二个场景的GameMode 2.GameInstance与进程 ...

  4. vue 非父子组件传值

    /*非父子组件传值 1.新建一个js文件 然后引入vue 实例化vue 最后暴露这个实例 2.在要广播的地方引入刚才定义的实例 3.通过 VueEmit.$emit('名称','数据') 4.在接收收 ...

  5. c#day05

    using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ccc ...

  6. springboot+mybatis整合(单元测试,异常处理,日志管理,AOP)

    我用的事IDEA,jdk版本是1.7.新建项目的时候这个地方的选择需要注意一下,springboot版本是1.5的,否则不支持1.7的jdk pom.xml <dependency> &l ...

  7. OpenJudge 由中根顺序和后根序列重建二叉树

    题目内容: 我们知道如何按照三种深度优先次序来周游一棵二叉树,来得到中根序列.前根序列和后根序列.反过来,如果给定二叉树的中根序列和后根序列,或者给定中根序列和前根序列,可以重建一二叉树.本题输入一棵 ...

  8. Windows向虚拟机Linux传输文件方法

    在Windows中装了个centOS,进行文件操作时,把mv写成了rm,然后就悲剧了.. 赶紧从网上找来文件的具体内容,然后由Windows向Linux挂载共享文件夹. 具体做法: 在Windows中 ...

  9. WPF Stake

    WPF中的StackPanel.WrapPanel.DockPanel 转:http://blog.sina.com.cn/s/blog_6c81891701017a34.html StackPane ...

  10. angularjs探秘<五> 举足轻重的scope

    scope在angular中的作用可谓举足轻重,不理解scope就不会angular: scope是应用在 HTML (view) 和 JavaScript (controller)之间的纽带. sc ...