使用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. 峰Redis学习(9)Redis 集群(概述)

    第一节:Redis 集群概述 redis cluster是去中心化,去中间件的,也就是说,集群中的每个节点都是平等的关系,都是对等的,每个节点都保存各自的数据和整个集群的状态.每个节点都和其他所有节点 ...

  2. Python——函数,模块,简单文件读写(python programming)

    函数(function)定义原则: 最大化代码重用,最小化代码冗余,流程符合思维逻辑,少用递归; 函数的定义方法: def function_name(param_1, param_2): ..... ...

  3. [转][CentOS]VI编辑器使用

    参考:https://blog.csdn.net/qq_34160679/article/details/79800584 参考:https://www.cnblogs.com/mondol/p/vi ...

  4. 还在用慢的要死的百度网盘?来试试这款12.5M下载速度的免费网盘吧!

    我们都知道云存储,如谷歌云端硬盘,苹果的icloud, 微软的OneDrive. 它们是用于数据备份和与多设备同步的云存储. 虽然它被广泛使用,但是还是有一些缺点,以谷歌云端硬盘为例: 1. 如果你需 ...

  5. linux下开启某个端口的方法:可用于SQL

  6. Sep 10th 2018

    今天是教师节,祝家里的两位‘老师’节日快乐.一位是幼儿园的保健医,另一位是驾校的教练.不能说是真正的老师,但作的也是传道授业之工作.今天看到新闻,马云要在明年的今天辞去现任阿里巴巴主席一职,继续投身他 ...

  7. [电脑知识点]win10家庭版怎么显示桌面图标

    控制面板-------->外观和个性化-------->个性化-------->更改桌面图标-------->然后你就能看到你想要的东西了,勾选上就可以了

  8. javasript-for循环

    先来个for循环的例子: var i=0,j=0; for(;i<10,j<6;i++,j++){ k=i+j; } console.log(k) 想知道会输出什么,首先得知道完整循环了多 ...

  9. 实用JS代码

    浏览器功能 1.0 浏览器判断 如下代码判断是手机还是电脑访问的网站 function IsPC () { var userAgentInfo = navigator.userAgent var Ag ...

  10. vue2.0-组件传值

    父组件给子组件传值,子组件用props接收 例子:两个组件,一个是父组件标签名是parent,一个是子组件标签名是child,并且child组件嵌套在父组件parent里,大概的需求是:我们子组件里需 ...