使用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. [C#][Report]Cry

    本文来自:https://wiki.scn.sap.com/wiki/display/BOBJ/Crystal+Reports%2C+Developer+for+Visual+Studio+Downl ...

  2. 理一下docker在各平台上的运行机制

    理一下docker在各平台上的运行机制 首先,从内核共享与否 docker在linux上共享内核,无需虚拟化,完全支持native功能(https://docs.docker.com/engine/i ...

  3. 个人知识管理PKM:收集、消化、应用、创新

                                                          个人知识管理PKM:收集.消化.应用.创新 准备工作1.制作知识分类体系(在线博客分类.本地 ...

  4. 3d图像坐标轴及css3属性的讲解

    3d立体坐标轴 如有不知道各种插件的怎么办? 网上查,百度 1.css选择器: 1.id 2.class 3.标签 4.子代 5.后代 6.交集 7.并级 8.通配符 9.结构 10.伪类 11.属性 ...

  5. hadoop集群部署

    1) 安装jdk 下载jdk-6u21-linux-i586.bin 然后修改/etc/profile: export JAVA_HOME=/usr/local/jdk export CLASSPAT ...

  6. [UE4]场景截图

    要点: 一.鼠标在编辑器关卡中点一下,然后按F9键截图,右下角会出现截图后的文件链接,点击后会自动打开截图文件所在的文件夹.截图的大小正好的关卡视图的大小. 编辑器:F9键截图,只截图关卡视图界面 运 ...

  7. RAC集群安装校验输出信息

    RAC集群安装校验输出信息 作者:Eric 微信:loveoracle11g [grid@rac-node1 grid]$ [grid@rac-node1 grid]$ ./runcluvfy.sh ...

  8. WAV和PCM的关系和区别

    什么是WAV和PCM? WAV:wav是一种无损的音频文件格式,WAV符合 PIFF(Resource Interchange File Format)规范.所有的WAV都有一个文件头,这个文件头音频 ...

  9. 生成excel的时候要用双引号。。。。。

    <?php header("Content-type:application/vnd.ms-excel"); header("Content-Disposition ...

  10. 解决 WordPress 4.9 页面模板功能无法正常使用

    WordPress 4.9 有一个重要更新是:在WP后台编辑主题和插件文件时,支持按层级显示所有文件.为了性能考虑,使用了 transient 缓存机制.但是却没有提供任何手动清除缓存的功能,导致有些 ...